线性表的链式存储及其接口函数C++类实现

来源:互联网 发布:mm下载软件 编辑:程序博客网 时间:2024/04/20 12:15

http://blog.csdn.net/hongkangwl/article/details/21883231

首先通过结构体建立每个节点

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size: 18px;"struct node //链表节点  
  2. {  
  3.     node* ptrnext;  
  4.     int data;  
  5.   
  6. };</span>  
在线性表的类中,定义了一些对线性表的常用操作

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size: 18px;">class linklist //链表类  
  2. {  
  3. private:  
  4.     static int length; //链表长度  
  5.     static int capacity;//链表容量  
  6.   
  7. public:  
  8.     node* ptr_node;//节点指针  
  9.     node* rootnode;//链表根节点,没有元素,指向position为0的节点  
  10.     linklist()//构造函数  
  11.     {  
  12.         length = 0;  
  13.         capacity = 0;  
  14.         ptr_node = NULL;  
  15.         rootnode->ptrnext = NULL;  
  16.     }  
  17.     linklist(int capacity_num)//带参数的构造函数  
  18.     {  
  19.         capacity = capacity_num;  
  20.         length = 0;  
  21.         ptr_node = NULL;  
  22.         rootnode = new node;  
  23.         rootnode->ptrnext = NULL;  
  24.     }  
  25.       
  26.     int linklist_insert(linklist* ptr,int pos,int member);//插入元素  
  27.     int linklist_erase(linklist* ptr,int  pos);//删除元素  
  28.     int linklist_getlength(linklist* ptr);//获取链表长度  
  29.     int linklist_getcapacity(linklist* ptr);//获取链表容量  
  30.     void linklist_display(linklist* ptr);//顺序输出链表中的每个元素  
  31.     void linklist_increaselength(linklist* ptr);//增加元素的个数  
  32.     void linklist_decreaselength(linklist* ptr);//减少元素的个数  
  33.     int linklist_getmember(linklist* ptr,int pos);//获取某位置上的元素  
  34.   
  35.     ~linklist()//析构函数  
  36.     {  
  37.       
  38.     }     
  39. };</span>  

每次最难的就是插入,插入的是第一个和最后一个时比较麻烦

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. <span style="font-size: 18px;">int linklist::linklist_insert(linklist* ptr,int pos,int member)  
  2. {     
  3.     if(ptr->linklist_getlength(ptr) == ptr->linklist_getcapacity(ptr))//链表已满  
  4.     {  
  5.         cout<<"超出链表的容量"<<endl;  
  6.         return -1;  
  7.     }  
  8.     if(pos > linklist::linklist_getcapacity(ptr) - 1)//位置在链表之外  
  9.     {  
  10.         cout<<"位置超出链表的尾巴"<<endl;  
  11.         return -1;  
  12.     }  
  13.     else  
  14.     {  
  15.         if(rootnode->ptrnext == NULL) //空链表的  
  16.         {  
  17.             ptr_node = new node;  
  18.             ptr_node->data = member;  
  19.             ptr_node->ptrnext = NULL;  
  20.             rootnode->ptrnext = ptr_node;  
  21.             ptr->linklist_increaselength(ptr);  
  22.         }  
  23.         else    //在中间插入  
  24.             if(ptr->linklist_getlength(ptr) - 1 < pos)  
  25.             {  
  26.                 node* current;  
  27.                 node* next;  
  28.                 current = rootnode;  
  29.                 next = current->ptrnext;  
  30.                 ptr_node = new node;  
  31.                 ptr_node->data = member;  
  32.                 ptr_node->ptrnext = NULL;  
  33.                 for(int i = 0; i < ptr->linklist_getlength(ptr) ; i++)  
  34.                 {  
  35.                     current = next;  
  36.                     next = current->ptrnext;  
  37.                 }  
  38.                 current->ptrnext = ptr_node;  
  39.                 ptr->linklist_increaselength(ptr);  
  40.                 return 0;  
  41.             }  
  42.             else if(pos == 0) //空链表,貌似跟上面的重复了,额,不影响运行  
  43.             {  
  44.                 ptr_node = new node;  
  45.                 ptr_node->data = member;  
  46.                 ptr_node->ptrnext = rootnode->ptrnext;  
  47.                 rootnode->ptrnext = ptr_node;  
  48.                 ptr->linklist_increaselength(ptr);  
  49.                 return 0;  
  50.             }  
  51.             else  
  52.             {  
  53.                 node* current;  
  54.                 node* next;  
  55.                 current = rootnode;  
  56.                 ptr_node = new node;  
  57.                 ptr_node->data = member;  
  58.                 next = current->ptrnext;  
  59.                 for(int i = 0; i < pos; i++)  
  60.                 {  
  61.                     current = next;  
  62.                     next = next->ptrnext;  
  63.                 }  
  64.                 ptr_node->ptrnext = current->ptrnext;  
  65.                 current->ptrnext = ptr_node;  
  66.                 ptr->linklist_increaselength(ptr);  
  67.                 return 0;  
  68.             }  
  69.               
  70.     }  
  71.       
  72. }</span>  
之后就是删除了

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. int linklist::linklist_erase(linklist* ptr,int pos)  
  2. {  
  3.     node* current = rootnode;  
  4.     node* next = current->ptrnext;  
  5.     if(pos > ptr->linklist_getlength(ptr))//位置在链表之外  
  6.     {  
  7.         cout<<"oop!!该位置没有元素"<<endl;  
  8.         return -1;  
  9.     }  
  10.     else  
  11.     {  
  12.         for(int i = 0; i < pos; i++)  
  13.         {  
  14.             current = next;  
  15.             next = current->ptrnext;  
  16.         }  
  17.   
  18.         current->ptrnext = next->ptrnext;  
  19.         ptr->linklist_decreaselength(ptr);  
  20.     }  
  21. }  

删除比插入简单多了。。。

线性表的链式存储具体实现和完整测试代码如下:

[cpp] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. using namespace std;  
  3.  struct node    //链表节点  
  4. {  
  5.     node* ptrnext;  
  6.     int data;  
  7.   
  8. };  
  9. class linklist //链表类  
  10. {  
  11. private:  
  12.     static int length; //链表长度  
  13.     static int capacity;//链表容量  
  14.   
  15. public:  
  16.     node* ptr_node;//节点指针  
  17.     node* rootnode;//链表根节点,没有元素,指向position为0的节点  
  18.     linklist()//构造函数  
  19.     {  
  20.         length = 0;  
  21.         capacity = 0;  
  22.         ptr_node = NULL;  
  23.         rootnode->ptrnext = NULL;  
  24.     }  
  25.     linklist(int capacity_num)//带参数的构造函数  
  26.     {  
  27.         capacity = capacity_num;  
  28.         length = 0;  
  29.         ptr_node = NULL;  
  30.         rootnode = new node;  
  31.         rootnode->ptrnext = NULL;  
  32.     }  
  33.       
  34.     int linklist_insert(linklist* ptr,int pos,int member);//插入元素  
  35.     int linklist_erase(linklist* ptr,int  pos);//删除元素  
  36.     int linklist_getlength(linklist* ptr);//获取链表长度  
  37.     int linklist_getcapacity(linklist* ptr);//获取链表容量  
  38.     void linklist_display(linklist* ptr);//顺序输出链表中的每个元素  
  39.     void linklist_increaselength(linklist* ptr);//增加元素的个数  
  40.     void linklist_decreaselength(linklist* ptr);//减少元素的个数  
  41.     int linklist_getmember(linklist* ptr,int pos);//获取某位置上的元素  
  42.   
  43.     ~linklist()//析构函数  
  44.     {  
  45.       
  46.     }     
  47. };  
  48. int linklist::length = 0;  
  49. int linklist::capacity = 0;  
  50. int linklist::linklist_getmember(linklist* ptr,int pos)  
  51. {  
  52.     node* current = rootnode;  
  53.     node* next = current->ptrnext;  
  54.     for(int i = 0; i < pos; i++)  
  55.     {  
  56.         current = next;//循环迭代  
  57.         next = current->ptrnext;  
  58.     }  
  59.     return next->data;  
  60. }  
  61. void linklist::linklist_decreaselength(linklist *ptr)  
  62. {  
  63.     ptr->length--;  
  64. }  
  65. int linklist::linklist_erase(linklist* ptr,int pos)  
  66. {  
  67.     node* current = rootnode;  
  68.     node* next = current->ptrnext;  
  69.     if(pos > ptr->linklist_getlength(ptr))//位置在链表之外  
  70.     {  
  71.         cout<<"oop!!该位置没有元素"<<endl;  
  72.         return -1;  
  73.     }  
  74.     else  
  75.     {  
  76.         for(int i = 0; i < pos; i++)  
  77.         {  
  78.             current = next;  
  79.             next = current->ptrnext;  
  80.         }  
  81.   
  82.         current->ptrnext = next->ptrnext;  
  83.         ptr->linklist_decreaselength(ptr);  
  84.     }  
  85. }  
  86. int linklist::linklist_getcapacity(linklist *ptr)  
  87. {  
  88.     return ptr->capacity;  
  89. }  
  90. void linklist::linklist_increaselength(linklist* ptr)  
  91. {  
  92.     ptr->length++;  
  93. }  
  94. int linklist::linklist_getlength(linklist* ptr)  
  95. {  
  96.     return ptr->length;  
  97. }  
  98. void linklist::linklist_display(linklist* ptr)//输出每个元素  
  99. {  
  100.     node *current;  
  101.     current = rootnode;  
  102.     node* next;  
  103.     next = current->ptrnext;  
  104.     for(int i = 0; i< ptr->linklist_getlength(ptr); i++)  
  105.     {  
  106.         cout<<current->ptrnext->data<<" ";  
  107.         current = next;  
  108.         next = current->ptrnext;  
  109.           
  110.     }  
  111.     cout<<endl;  
  112. }  
  113. int linklist::linklist_insert(linklist* ptr,int pos,int member)  
  114. {     
  115.     if(ptr->linklist_getlength(ptr) == ptr->linklist_getcapacity(ptr))//链表已满  
  116.     {  
  117.         cout<<"超出链表的容量"<<endl;  
  118.         return -1;  
  119.     }  
  120.     if(pos > linklist::linklist_getcapacity(ptr) - 1)//位置在链表之外  
  121.     {  
  122.         cout<<"位置超出链表的尾巴"<<endl;  
  123.         return -1;  
  124.     }  
  125.     else  
  126.     {  
  127.         if(rootnode->ptrnext == NULL) //空链表的  
  128.         {  
  129.             ptr_node = new node;  
  130.             ptr_node->data = member;  
  131.             ptr_node->ptrnext = NULL;  
  132.             rootnode->ptrnext = ptr_node;  
  133.             ptr->linklist_increaselength(ptr);  
  134.         }  
  135.         else    //在中间插入  
  136.             if(ptr->linklist_getlength(ptr) - 1 < pos)  
  137.             {  
  138.                 node* current;  
  139.                 node* next;  
  140.                 current = rootnode;  
  141.                 next = current->ptrnext;  
  142.                 ptr_node = new node;  
  143.                 ptr_node->data = member;  
  144.                 ptr_node->ptrnext = NULL;  
  145.                 for(int i = 0; i < ptr->linklist_getlength(ptr) ; i++)  
  146.                 {  
  147.                     current = next;  
  148.                     next = current->ptrnext;  
  149.                 }  
  150.                 current->ptrnext = ptr_node;  
  151.                 ptr->linklist_increaselength(ptr);  
  152.                 return 0;  
  153.             }  
  154.             else if(pos == 0) //空链表,貌似跟上面的重复了,额,不影响运行  
  155.             {  
  156.                 ptr_node = new node;  
  157.                 ptr_node->data = member;  
  158.                 ptr_node->ptrnext = rootnode->ptrnext;  
  159.                 rootnode->ptrnext = ptr_node;  
  160.                 ptr->linklist_increaselength(ptr);  
  161.                 return 0;  
  162.             }  
  163.             else  
  164.             {  
  165.                 node* current;  
  166.                 node* next;  
  167.                 current = rootnode;  
  168.                 ptr_node = new node;  
  169.                 ptr_node->data = member;  
  170.                 next = current->ptrnext;  
  171.                 for(int i = 0; i < pos; i++)  
  172.                 {  
  173.                     current = next;  
  174.                     next = next->ptrnext;  
  175.                 }  
  176.                 ptr_node->ptrnext = current->ptrnext;  
  177.                 current->ptrnext = ptr_node;  
  178.                 ptr->linklist_increaselength(ptr);  
  179.                 return 0;  
  180.             }  
  181.               
  182.     }  
  183.       
  184. }  
  185. int main()  
  186. {  
  187.     int num;  
  188.     cout<<"链表的容量是多少"<<endl;  
  189.     cin>>num;  
  190.     linklist* LinkList = new linklist(num);  
  191.     int n;  
  192.     cout<<"你想要在链表中放入多少元素"<<endl;  
  193.     cin>>n;  
  194.     int* ptrint = new int[n];  
  195.     for(int i = 0; i < n; i++)  
  196.     {  
  197.         cin>>ptrint[i];  
  198.     }  
  199.     for(int i = 0; i < n; i++)  
  200.     {  
  201.         LinkList->linklist_insert(LinkList,i,ptrint[i]);  
  202.     }  
  203.     cout<<"链表中元素是:"<<endl;  
  204.     LinkList->linklist_display(LinkList);  
  205.     LinkList->linklist_erase(LinkList,3);  
  206.     cout<<"删除位置是3(即第4个)的元素后,链表中元素是:"<<endl;  
  207.     LinkList->linklist_display(LinkList);  
  208.     cout<<"位置为2(即第3个)的元素是:"<<endl;  
  209.     cout<<LinkList->linklist_getmember(LinkList,2);  
  210.       
  211.       
  212.     return 0;  
  213. }  

测试结果如下:


0 0
原创粉丝点击