码迷,mamicode.com
首页 > 编程语言 > 详细

C++链表简单实现

时间:2017-07-24 14:33:25      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:类型   lap   next   节点   last   efault   链表   div   img   

C++并没有像C#一样的List类,需要手动实现一个链表。 首先声明一个数据结点模板类:

技术分享
1  template<class T> 
2 class LinkNode {
3  public:
4    linkNode() {
5         next = NULL;
6 } 
7   T data;   
8 linkNode* next;
9  }; 
View Code

data:存放T类型数据,next:存放下一节点指针,初始化next为NULL。

声明链表主体模板:

 1 template<class T> 
 2 class Link { 
 3         private:
 4             unsigned int  listLength; 
 5             linkNode<T> * tempNode;  
 6             linkNode<T> * headNode;
 7             linkNode<T> * lastNode;
 8         public:   
 9             Link();//init this list
10             unsigned int length();
11             void add(T x);  
12             bool isEmpty();  
13             linkNode<T> * findNode(T x);
14             void deleteNode(T x);
15             void deleteNode(linkNode<T>* x); 
16             void insert(T x,linkNode<T> * p);
17             void insertHead(T x);   
18             linkNode<T> * firstOrDefault();  
19             linkNode<T>* lastOrDefault(); 
20             linkNode<T>* removeAll(); 
21 }; 

对链表进行初始化操作;

1 template<class T>
2 Link<T>::Link(){
3   listLength = 0;
4   tempNode = NULL;
5   headNode = NULL;
6   lastNode =  NULL;
7 }

实现各方法:

  1 template<class T>
  2 linkNode<T> *Link<T>::removeAll(){
  3   linkNode<T>* p1,*p2;
  4   for(p1=headNode;p1!=NULL;p1=p2){
  5       p2 = p1->next;
  6       delete p1;
  7       p1=NULL;
  8     }
  9   tempNode = headNode = lastNode = p2 = NULL;
 10   listLength = 0;
 11   return NULL;
 12 }
 13 
 14 //获取链表中的第一个节点  默认为NULL
 15 template<class T>
 16 linkNode<T>* Link<T>::firstOrDefault(){
 17   return headNode;
 18 }
 19 
 20 //获取链表中的最后一个节点  默认为NULL
 21 template<class T>
 22 linkNode<T>* Link<T>::lastOrDefault(){
 23   return lastNode;
 24 }
 25 
 26 //返回LIST长度
 27 template<class T>
 28 unsigned int Link<T>::length(){
 29   return listLength;
 30 }
 31 
 32 
 33 //向链表添加一个元素
 34 template<class T>
 35 void Link<T>::add(T x){
 36   tempNode = new linkNode<T>();
 37   tempNode->data = x;
 38   if(lastNode == NULL){
 39       headNode = tempNode;
 40       lastNode = tempNode;
 41     }else
 42     {
 43       lastNode->next = tempNode;
 44       lastNode = tempNode;
 45     }
 46   ++listLength;
 47 }
 48 
 49 
 50 //判断LIST是否为空
 51 template<class T>
 52 bool Link<T>::isEmpty(){
 53   return listLength == 0;
 54 }
 55 
 56 //在LIST中查找某个节点
 57 template<class T>
 58 linkNode<T>* Link<T>::findNode(T x){
 59   tempNode = headNode;
 60   while (tempNode!=NULL&&tempNode->data!=x) {
 61       tempNode = tempNode->next;
 62     }
 63   return tempNode;
 64 }
 65 
 66 //删除LIST中的某个节点
 67 template<class T>
 68 void Link<T>::deleteNode(linkNode<T> *x){
 69   linkNode<T>* _temp = headNode;
 70   if(_temp == NULL){ return ;}
 71   if(_temp == x){
 72       headNode = _temp->next;
 73       if(headNode == NULL){ lastNode = NULL; }
 74       delete _temp;
 75       if(listLength>0){
 76           --listLength;
 77         }
 78       return;
 79     }
 80   while (_temp->next!=NULL && _temp->next!=x) {
 81       _temp = _temp->next;
 82     }
 83   if(_temp->next == NULL ){return;}
 84   if(_temp->next == lastNode){
 85       delete _temp->next;
 86       _temp->next =NULL;
 87       lastNode = _temp;
 88     }else{
 89       tempNode = _temp->next;
 90       _temp->next = tempNode->next;
 91       delete tempNode;
 92       tempNode =NULL;
 93     }
 94   if(listLength>0){--listLength;}
 95 }
 96 
 97 //删除LIST中的某个节点
 98 template<class T>
 99 void Link<T>::deleteNode(T x){
100   linkNode<T>* _temp = headNode;
101   if(_temp == NULL){ return ;}
102   if((_temp->data) == x){
103       headNode = _temp->next;
104       if(_temp->next == NULL){ lastNode = NULL;}
105       delete(_temp);
106       if(listLength>0){
107           --listLength;
108         }
109       return ;
110     }
111   while (_temp->next!=NULL&&_temp->next->data!=x) {
112       _temp = _temp->next;
113     }
114   if(_temp->next ==NULL){return;}
115   if(_temp->next == lastNode){
116       lastNode = _temp;
117       delete(_temp->next);
118       _temp->next = NULL;
119     }else{
120       tempNode = _temp->next;
121       _temp->next  = tempNode->next;
122       delete(tempNode);
123       tempNode = NULL;
124     }
125   if(listLength>0){
126       --listLength;
127     }
128 }
129 
130 //在P元素后添加X
131 template<class T>
132 void Link<T>::insert(T x ,linkNode<T> *p){
133   if(p == NULL){return;}
134   tempNode = new linkNode<T>();
135   tempNode->data = x;
136   tempNode->next = p->next;
137   p->next = tempNode;
138   if(tempNode->next == NULL){lastNode = tempNode;}
139   ++listLength;
140 }
141 
142 //在LIST头添加元素X
143 template<class T>
144 void Link<T>::insertHead(T x){
145   tempNode = new linkNode<T>();
146   tempNode->data = x;
147   tempNode->next = headNode;
148   headNode = tempNode;
149   ++listLength;
150 }

 

C++链表简单实现

标签:类型   lap   next   节点   last   efault   链表   div   img   

原文地址:http://www.cnblogs.com/lvlinlv/p/7228398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!