C++实现 简单 单链表

来源:互联网 发布:女生变漂亮知乎 编辑:程序博客网 时间:2024/06/03 19:21

转自: http://blog.csdn.net/wonggonghong/article/details/21527577

  我们首先建立一个<List.h>头文件,声明一个单链表结构:

#include "List.h"

[cpp] view plain copy
  1. //创建一个单链表结构,包含一些常见的操作  
  2. #ifndef _List_H_  
  3. #define _List_H_  
  4.   
  5. #include <iostream>  
  6.   
  7. struct Node{  
  8.     int element;  //节点存储信息可以根据需要修改!  
  9.     Node* next;  
  10. };  
  11.   
  12. Node* CreateLists(); //创建一个空表,并返回表头  
  13. void DeleteLists(Node* head); //删除表头为head的该链表  
  14. bool IsLast(Node* P);  
  15. Node* Find(int X, Node* head);  
  16. Node* FindPrevious(int X, Node* head);  
  17. void Delete(int X, Node* head);  
  18. void Insert(Node* P, int X); //在节点P后面插入X  
  19. void OutputLists(Node* head); //输出链表中所有元素的值  
  20.   
  21. #endif      

    然后在<List.cpp>文件里实现头文件中的链表操作:

#include "List.cpp"

[cpp] view plain copy
  1. #include "list.h"  
  2.   
  3. Node* CreateLists()  
  4. {  
  5.     Node* head = new Node;  
  6.     head->next = NULL;  
  7.     return head;  
  8. }  
  9.   
  10. void DeleteLists(Node* head)  
  11. {  
  12.     Node* P = head->next, *temp;  
  13.     head->next = NULL;  
  14.     while(P)  
  15.     {  
  16.         temp = P->next;  
  17.         delete P;  
  18.         P = temp;  
  19.     }  
  20. }  
  21.   
  22. bool IsLast(Node* P)  
  23. {  
  24.     return P->next == NULL;  
  25. }  
  26.   
  27. Node* Find(int X, Node* head)  
  28. {  
  29.     Node* P = head->next;  
  30.     while(P && P->element!=X)  
  31.         P = P->next;  
  32.     return P;  
  33. }  
  34.   
  35. Node* FindPrevious(int X, Node* head)  
  36. {  
  37.     Node* P=head;  
  38.     while(P->next && P->next->element!=X)  
  39.         P=P->next;  
  40.     return P;  
  41. }  
  42.   
  43. void Delete(int X, Node* head)  
  44. {  
  45.     Node* P = FindPrevious(X,head), *temp; //如果没找到X,则返回的是链表最后一项  
  46.     if(P->next)  
  47.     {  
  48.         temp = P->next;  
  49.         P->next = temp->next;  
  50.         delete temp;  
  51.     }  
  52. }  
  53.   
  54. void Insert(Node* P, int X)  
  55. {  
  56.     Node* tempX = new Node;  
  57.     tempX->element = X;  
  58.     tempX->next = P->next;  
  59.     P->next = tempX;  
  60. }  
  61.   
  62. void OutputLists(Node* head)  
  63. {  
  64.     Node* P = head->next;  
  65.     while(P)  
  66.     {  
  67.         std::cout<<P->element<<"   ";  
  68.         P = P->next;  
  69.     }  
  70.     std::cout<<std::endl;  
  71. }  
        最后,我们用一段main代码验证一下正确性:

[cpp] view plain copy
  1. #include <iostream>  
  2. #include <assert.h>  
  3. #include "list.h"  
  4.   
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     int Date[8] = {2,9,5,8,15,32,7,4};  
  10.     Node *head = CreateLists();  
  11.     Node *P = head;  
  12.     for(int i=0;i<8;i++)  
  13.     {  
  14.         Insert(P,Date[i]);  
  15.         P = P->next;  
  16.     }  
  17.     cout<<"打印出链表中所有元素:\n";  
  18.     OutputLists(head);  
  19.     if(IsLast(P))  
  20.         cout<<"该Lists的最后一个节点为:"<<P->element<<endl;  
  21.     else  
  22.         cout<<"P不是最后一个节点!!P等于:"<<P->element<<endl;  
  23.       
  24.     if(Find(15,head))  
  25.         cout<<"Find函数在该Lists中找到了值为15的节点!!!\n";  
  26.     else  
  27.         cout<<"Find函数没找到值为15的节点!!\n";  
  28.   
  29.     cout<<"FindPrevious函数找到节点15的前一个节点为:"<<FindPrevious(15,head)->element<<endl;  
  30.     cout<<"而节点15的前一个节点应该为“8”!\n";  
  31.   
  32.     Delete(8, head);  
  33.     if(Find(8,head))  
  34.         cout<<"Delete(8, head)后,在该Lists中找到了值为8的节点!!!\n";  
  35.     else  
  36.         cout<<"Delete(8, head)后,Find函数没找到值为8的节点!!\n";  
  37.   
  38.     DeleteLists(head);   
  39.     if(head->next)  
  40.         cout<<"DeleteLists函数未成功删除链表!!\n";  
  41.     else  
  42.         cout<<"链表已经为空!DeleteLists函数没有问题!!!\n";  
  43.     return 0;  
  44. }  
结果如下:

原创粉丝点击