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

用C++ “包装” 后的单向链表操作

时间:2014-11-14 00:12:08      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   使用   sp   文件   数据   

下面是用以实现类的代码:

Node.h

 1 #ifndef __NODE_H__
 2 /*单向 链表*/
 3 #define  __NODE_H__
 4 
 5 typedef struct  _NODE
 6 {
 7     int data;
 8     struct _NODE *next;
 9 }node;
10 
11 class Node
12 {
13 public:
14     Node();
15     ~Node();
16     //外部使用的公有函数部分
17 public:
18 
19     /*重载add,分别为在表后插入和按照指定位置插入*/
20     Node* add(int data);
21     Node* add(int index, int data);
22 
23     /*删除数据,注意是删除所有值为data的数据*/
24     void delData(int data);
25 
26     //重载delIndex,一个按照索引删除是安全的,就是用户已知要删除的数据和其位置,另一个就是完全删除,不论位置上的数据是否已知
27     void delIndex(int index);
28     void delIndex(int index,int data);
29 
30     void display();
31 
32 private:
33     node *bak;
34     node *first;
35     node *current;
36     node *previous;
37 
38 private:
39     bool isOver(node *first);
40 };
41 
42 #endif //__NODE_H__

 

Node.cpp

  1 #include "Node.h"
  2 #include <iostream>
  3 
  4 Node::Node()
  5 {
  6     bak = nullptr;
  7     first = nullptr;
  8     current = nullptr;
  9     previous = nullptr;
 10 }
 11 
 12 /*添加数据节点*/
 13 Node* Node::add(int data)
 14 {
 15     if (!first)
 16     {
 17         current = new node;
 18         current->data = data;
 19         current->next = nullptr;
 20         first = current;
 21     }
 22     else
 23     {
 24         previous = current;
 25         current = new node;
 26         current->data = data;
 27         current->next = nullptr;
 28         previous->next = current;
 29     }
 30     return this;
 31 }
 32 
 33 Node* Node::add(int index, int data)
 34 {
 35     int num = 0;
 36     short flag;
 37     bak = first;
 38     node *cur = nullptr;
 39     node *pre = nullptr;
 40     if (first)
 41     {
 42         while (first)
 43         {
 44             ++num;
 45             pre = first; /*保存当前位置*/
 46 
 47             if (index == num)    /*如果索引的位置已经存在,标志为 1 */
 48             {
 49                 flag = 1;
 50                 break;
 51             }
 52             if (isOver(first)) /*如果已经到了链表的结尾*/
 53             {
 54                 flag = 3; /*索引的位置不存在,标志为 3 ,此时num计数的正是链表中数据的个数*/
 55                 if (index==num+1)
 56                 {
 57                     flag = 2; /*如果索引的位置不存在,但是在链表的结尾,相当与在表后新加上一个数据,标志为 2 */
 58                 }
 59                 break;
 60             }
 61             first = first->next;
 62         }
 63     }
 64     else
 65     {
 66         if (index == 1)
 67         {
 68             add(1);
 69             return this;
 70         }
 71         std::cout << "链表尚且为空" << std::endl;
 72         return this;
 73     }
 74     switch (flag)
 75     {
 76     case 1:
 77         cur = new node;
 78         //数据交换
 79         num = pre->data;
 80         pre->data = data;
 81         cur->data = num;
 82         //位置交换
 83         cur->next = pre->next;
 84         pre->next = cur;
 85         break;
 86 
 87     case 2:
 88         add(data);
 89         break;
 90 
 91     case 3:
 92         std::cout << "索引的位置 " << index << " 越界" << std::endl;
 93     }
 94     first = bak;
 95     return this;
 96 }
 97 
 98 void Node::delData(int data)
 99 {
100     int num = 0;
101     int i = 1;
102     bak = first;
103     node *next = nullptr;
104     if (first)
105     {
106         while (first)
107         {
108             if (i == 1)
109             {
110                 if (first->data == data)
111                 {
112                     ++num;
113                     next = first->next;
114                     delete first;
115                     first = next;
116                     bak = first;
117                     continue; /*若没有continue的话,将会在第一个是被删数据的时候漏掉第二个数据*/
118                 }
119                 if (first->next)
120                 {
121                     if (first->next->data == data)
122                     {
123                         ++num;
124                         next = first->next->next;
125                         delete first->next;
126                         first->next = next;
127                     }
128                 }
129                 ++i; //此句不能放在上面的continue前面,因为上面的操作完成后,first仍然指向的是第一个节点,也就是头节点,所以必须再次进入此语句进行判断,否则会漏掉数据
130             }
131             else
132             {
133                 if (first->next)
134                 {
135                     if (first->next->data == data)
136                     {
137                         ++num;
138                         next = first->next->next;
139                         delete first->next;
140                         first->next = next;
141                     }
142                 }
143             }
144             first = first->next;
145         }
146     }
147     else
148     {
149         std::cout << "链表没有数据可以删除" << std::endl;
150         return ;
151     }
152     std::cout << "共删除了 " << num << " 个数据" << std::endl;
153     first = bak;
154 }
155 
156 void Node::delIndex(int index)
157 {
158     bak = first;
159     bool over = true;
160     node *next = nullptr;
161     int num=0;
162     if (first)
163     {
164         while (first)
165         {
166             if (index == 1)
167             {
168                 over = false;
169                 next = first->next;
170                 delete first;
171                 first = next;
172                 bak = first;
173                 break;
174             }
175             ++num;
176             if (index == num + 1)
177             {
178                 if (first->next)
179                 {
180                     over = false;
181                     next = first->next->next;
182                     delete first->next;
183                     first->next = next;
184                     break;
185                 }
186                 else
187                 {
188                     break;
189                 }
190             }
191             first = first->next;
192         }
193     }
194     else
195     {
196         std::cout << "链表没有数据可以删除" << std::endl;
197         return;
198     }
199     if (over)
200     {
201         std::cout << "" << index <<" 号索引不存在" << std::endl;
202     }
203     first = bak;
204 }
205 
206 void Node::delIndex(int index, int data)
207 {
208     bak = first;
209     bool over = true;
210     node *next = nullptr;
211     int num = 0;
212     if (first)
213     {
214         while (first)
215         {
216             if (index == 1 && data==first->data)
217             {
218                 over = false;
219                 next = first->next;
220                 delete first;
221                 first = next;
222                 bak = first;
223                 break;
224             }
225             ++num;
226             if (index == num + 1)
227             {
228                 if (first->next && first->next->data==data)
229                 {
230                     over = false;
231                     next = first->next->next;
232                     delete first->next;
233                     first->next = next;
234                     break;
235                 }
236                 else
237                 {
238                     break;
239                 }
240             }
241             first = first->next;
242         }
243     }
244     else
245     {
246         std::cout << "链表没有数据可以删除" << std::endl;
247     }
248     if (over)
249     {
250         std::cout << "" << index << " 号满足条件 data = "<< data <<" 的索引不存在" << std::endl;
251     }
252     first = bak;
253 }
254 
255 /*输出类中所有的数据,并按照编号打印*/
256 void Node::display()
257 {
258     int index = 1;
259     bak = first;
260     if (first)
261     {
262         while (first)
263         {
264             std::cout << index++<<" -> "<<first->data << std::endl;
265             first = first->next;
266         }
267         std::cout << "总计 " << index-1 << " 个数据." << std::endl;
268     }
269     else
270     {
271         std::cout << "链表为空" << std::endl;
272     }
273     first = bak;
274 }
275 
276 bool Node::isOver(node *first)
277 {
278     if (!first->next)
279     {
280         return true;
281     }
282     return false;
283 }
284 
285 Node::~Node()
286 {
287     delete first;
288     delete current;
289     delete previous;
290     std::cout << "进入析构函数,准备退出" << std::endl;
291 }

 

测试文件 main.cpp

#include "Node.h"
#include<iostream>

int main(void)
{
    Node *link = new Node;

    link->add(1);
    link->add(2, 2);

    link->add(1)->add(2)->add(3);
    link->add(2, 12)->add(2,13);
    link->display();

    link->delData(1);
    link->display();

    link->delIndex(2);
    link->display();

    link->delIndex(1, 2);
    link->display();

    system("pause");
    return 0;
}

 

代码未经严格测试,因此可能在某些情况下发生错误。希望有朋友发现的话及时指出,不胜感激。

以上。

用C++ “包装” 后的单向链表操作

标签:style   blog   io   color   os   使用   sp   文件   数据   

原文地址:http://www.cnblogs.com/lhyz/p/4096162.html

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