用C++ 实现链表:

首先功能分析: 构造,清理,增删改查,求大小 判断空 ,取头取尾

 

 
  
  1. #include <iostream> 
  2. using namespace std; 
  3.  
  4. typedef int T; 
  5. //链表类 
  6. class LinkedList 
  7.     struct Node 
  8.     { 
  9.         T data; 
  10.         Node* next; 
  11.         Node(const T& t):data(t),next(NULL) 
  12.         { 
  13.         } 
  14.     }; 
  15. public
  16.     //构造  析构  清空 
  17.     LinkedList():head(NULL) 
  18.     { 
  19.     } 
  20.      
  21.     ~LinedList() 
  22.     { 
  23.         clear(); 
  24.     } 
  25.  
  26.     void clear() 
  27.     { 
  28.      
  29.     } 
  30.     //增(insertBack insertFront)删改查(find) 
  31.     void insertFront(const T& t) 
  32.     { 
  33.     } 
  34.     void insertBack(const T& t) 
  35.     { 
  36.     } 
  37.     void erase(const T& t) 
  38.     { 
  39.     } 
  40.     void update(const T& t,const T& target) 
  41.     { 
  42.     } 
  43.     unsigned int  find(const T& t) 
  44.     { 
  45.         unsigned int position=0; 
  46.         return position; 
  47.     } 
  48.     //判断empty 求size  遍历(travel) 
  49.     bool empty() 
  50.     { 
  51.     } 
  52.     unsigned int size() 
  53.     { 
  54.         int size=0; 
  55.         return size; 
  56.     } 
  57.      
  58.     void travel() 
  59.     { 
  60.     } 
  61.     //取头 取尾   
  62.     T getHead() 
  63.     { 
  64.     } 
  65.     T getTail() 
  66.     { 
  67.  
  68.     } 
  69.     //去指定位置取指针  辅助作用 
  70.     Node* getPointer(int position) 
  71.     { 
  72.         return NULL; 
  73.     } 
  74. private
  75.     //头指针 最重要的部分 
  76.     Node* head;  
  77. }; 
  78.  
  79. int main() 

功能添加:

 

 
  
  1. #include <iostream> 
  2. using namespace std; 
  3.  
  4. typedef int T; 
  5. //链表类 
  6. class LinkedList 
  7.     struct Node 
  8.     { 
  9.         T data; 
  10.         Node* next; 
  11.         //初始化data next 阻止垃圾数据 
  12.         Node(const T& t=T()):data(t),next(NULL) 
  13.         { 
  14.         } 
  15.     }; 
  16. public: 
  17.     //构造  析构  清空 
  18.     LinkedList():head(NULL) 
  19.     { 
  20.     } 
  21.     ~LinkedList() 
  22.     { 
  23.         clear(); 
  24.     } 
  25.  
  26.     void clear() 
  27.     { 
  28.         Node *p=head
  29.         while (p!=NULL) 
  30.         { 
  31.             Node *q = p->next; 
  32.             delete p;//释放p所在空间 
  33.             p=q
  34.         } 
  35.     } 
  36.     //判断empty 求size  遍历(travel) 
  37.     bool empty() 
  38.     { 
  39.         //判断头指针是否为空 为空表示链表不存在 
  40.         return head==NULL ? true : false; 
  41.     } 
  42.     unsigned int size() 
  43.     { 
  44.         unsigned int size=0
  45.         Node* p =head
  46.         while (p!=NULL) 
  47.         { 
  48.             size++; 
  49.             pp=p->next; 
  50.         } 
  51.         return size; 
  52.     } 
  53.  
  54.     void travel() 
  55.     { 
  56.         //利用while循环一次次的输出来,直到指针为NULL结束 
  57.         Node* p = head
  58.         while (p!=NULL) 
  59.         { 
  60.             cout<<p->data<<endl
  61.             pp=p->next; 
  62.         } 
  63.     } 
  64.     //增(insertAfter insertFront)删改查(find) 
  65.     void insertFront(const T& t) 
  66.     { 
  67.         Node* p = new Node(t); 
  68.         p->next=head;  //讲头指针所指向的地址给p的next 
  69.         head = p;       //让*p作为头 
  70.     } 
  71.     void insertAfter(const T& t) 
  72.     { 
  73.         Node *p = new Node(t); 
  74.         Node *tail = getPointer(size()-1); 
  75.         tail->next = p
  76.     } 
  77.     void erase(const T& t) 
  78.     { 
  79.         unsigned int position = find(t); 
  80.         Node* cur = getPointer(position); 
  81.         if (position!=0) 
  82.         { 
  83.             Node* pre = getPointer(find(t)-1); 
  84.             pre->next = cur->next; 
  85.         }else{ 
  86.             head = cur->next; 
  87.         } 
  88.         delete cur; 
  89.     } 
  90.     void update(const T& t,const T& target) 
  91.     { 
  92.         Node *p=getPointer(find(target)); 
  93.         p->data=t; 
  94.     } 
  95.     unsigned int  find(const T& t) 
  96.     { 
  97.         unsigned int position=0
  98.         Node* p = head
  99.         while(p!=NULL) 
  100.         { 
  101.             if (p->data==t) 
  102.             { 
  103.                 return position; 
  104.             } 
  105.             pp=p->next; 
  106.             position++; 
  107.         } 
  108.         return position; 
  109.     } 
  110.      
  111.     //取头 取尾   
  112.     T getHead() 
  113.     { 
  114.         return head->data; 
  115.     } 
  116.     T getTail() 
  117.     { 
  118.         Node *p=getPointer(this->size()-1); 
  119.         return p->data; 
  120.     } 
  121.     //去指定位置取指针  辅助作用 
  122.     Node* getPointer(int position) 
  123.     { 
  124.         Node* p =head
  125.         for(int i = 0;i<position;i++) 
  126.         { 
  127.             pp=p->next; 
  128.         } 
  129.         return p; 
  130.     } 
  131. private: 
  132.     //头指针 最重要的部分 
  133.     Node* head;  
  134. }; 
  135.  
  136. int main()