Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1121615
  • 博文数量: 241
  • 博客积分: 4385
  • 博客等级: 上校
  • 技术积分: 2383
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-07 23:13
文章分类

全部博文(241)

文章存档

2013年(1)

2012年(8)

2011年(62)

2010年(109)

2009年(61)

分类: C/C++

2012-09-19 16:50:35

点击(此处)折叠或打开

  1. // SListTest.h
  2. #ifndef _SLIST_TEST_H_
  3. #define _SLIST_TEST_H_

  4. struct SNode
  5. {
  6.     int data;
  7.     SNode *next;
  8. };

  9. class SListTest
  10. {
  11. public:
  12.         SListTest(void);
  13.         ~SListTest(void);

  14.         bool isEmpty(void);
  15.         int length(void);
  16.         SNode * find(int pos);
  17.         
  18.         /* insert before pos */
  19.         bool insert(int pos, int newItem);
  20.         bool remove(int pos);

  21. private:
  22.         int len;
  23.         SNode *head;
  24. };

  25. #endif // _SLIST_TEST_H_

点击(此处)折叠或打开

  1. // SListTest.cpp
  2. #include <stdio.h>
  3. #include "SListTest.h"

  4. SListTest::SListTest(void)
  5. {
  6.     len = 0;
  7.     head = new SNode();
  8. }

  9. SListTest::~SListTest(void)
  10. {
  11.     while(!isEmpty())
  12.     {
  13.         remove(0);
  14.     }
  15. }

  16. bool SListTest::isEmpty(void)
  17. {
  18.     bool ret = false;

  19.     if(len <= 0)
  20.     {
  21.         ret = true;
  22.     }

  23.     return ret;
  24. }

  25. int SListTest::length(void)
  26. {
  27.     return len;
  28. }

  29. SNode * SListTest::find(int pos)
  30. {
  31.     SNode *ret = NULL;
  32.     SNode *now = NULL;

  33.     if(pos < 1 || pos > length())
  34.     {
  35.         return ret;
  36.     }

  37.     now = head;
  38.     while(pos > 0)
  39.     {
  40.         now = now->next;
  41.         --pos;
  42.     }
  43.     ret = now;

  44.     return ret;
  45. }

  46. /* insert before pos */
  47. bool SListTest::insert(int pos, int newItem)
  48. {
  49.     bool ret = false;

  50.     if(pos < 1 || pos > length() + 1)
  51.     {
  52.         return ret;
  53.     }

  54.     SNode * node = new SNode();
  55.     node->data = newItem;

  56.     if(pos == 1)
  57.     {
  58.         node->next = head->next;
  59.         head->next= node;
  60.     }
  61.     else
  62.     {
  63.         SNode *now = find(pos - 1);
  64.         if(now)
  65.         {
  66.             node->next = now->next;
  67.             now->next = node;
  68.         }
  69.     }

  70.     ++len;
  71.     ret = true;

  72.     return ret;
  73. }

  74. bool SListTest::remove(int pos)
  75. {
  76.     bool ret = false;

  77.     if(pos < 1 || pos > length())
  78.     {
  79.         return ret;
  80.     }

  81.     SNode *now = find(pos);
  82.     if(now)
  83.     {
  84.         SNode *prev = find(pos - 1);
  85.         prev->next = now->next;
  86.         delete now;
  87.     }

  88.     --len;
  89.     ret = true;

  90.     return ret;
  91. }

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include "SListTest.h"

  3. int main()
  4. {
  5.     SListTest *list = new SListTest();

  6.     /* insert before pos */
  7.     list->insert(1, 123); // head -> 123
  8.     list->insert(1, 456); // head -> 456 -> 123
  9.     list->insert(2, 789); // head -> 456 -> 789 -> 123

  10.     list->remove(3); // head -> 456 -> 789

  11.     return 0;
  12. }

阅读(1576) | 评论(0) | 转发(2) |
0

上一篇:Fibonacci Sequence

下一篇:虚函数示例一则

给主人留下些什么吧!~~