链表相关操作的C++实现

  1 #include "pch.h"
  2 #include <iostream>
  3 #include <stdlib.h>
  4 using namespace std;
  5 
  6 struct Node
  7 {
  8     int ele;
  9     struct Node* next;
 10 };
 11 typedef struct Node* ptr;
 12 typedef ptr list;
 13 typedef ptr position;
 14 
 15 list create_empty_list()//创建一个空表
 16 {
 17     list L = (list)malloc(sizeof(Node));
 18     L->next = NULL;
 19     return L;
 20 }
 21 position find_end(list L)//找出最后一个结点
 22 {
 23     position p = L;
 24     while (p->next != NULL)
 25         p = p->next;
 26     return p;
 27 }
 28 void add_ele_end(list L, int ele)//往列表末尾添加结点
 29 {
 30     position p = (position)malloc(sizeof(Node));
 31     p->ele = ele;
 32     p->next = NULL;
 33 
 34     position end = find_end(L);
 35     end->next = p;
 36 }
 37 int num_of_Node(list L)//计算结点个数
 38 {
 39     int count = 0;
 40     position p=L->next;
 41     while (p != NULL)
 42     {
 43         p = p->next;
 44         count++;
 45     }
 46     return count;
 47 }
 48 
 49 void print_list(list L)//打印出表的所有结点
 50 {
 51     position p;
 52     p = L->next;
 53     while (p != NULL)
 54     {
 55         cout << p->ele << " ";
 56         p = p->next;
 57     }
 58     cout << endl;
 59 }
 60 bool isempty(list L)//判断表是否为空
 61 {
 62     return L->next == NULL;
 63 }
 64 bool islast(list L,position p)//根据指针判断是否是最后一个结点
 65 {
 66     return p->next == NULL;
 67 }
 68 bool islast(list L, int x)//根据值判断是否是最后一个结点
 69 {
 70     position p = find_end(L);
 71     return p->ele == x;
 72 }
 73 position find_pos(list L,int x)//根据值找出结点位置
 74 {
 75     position p=L->next;
 76     while (p->ele != x && p->next != NULL)
 77         p = p->next;
 78     if (p->ele != x)
 79     {
 80         cout << "表中没有这个数据" << endl;
 81         exit(1);
 82     }
 83     else
 84         return p;
 85 }
 86 position find_preNode(list L, int x)//根据值找出前驱元
 87 {
 88     position p = L;
 89     while (p->next != NULL && (p->next->ele != x))
 90         p = p->next;
 91     return p;
 92 }
 93 position find_preNode_pos(list L, int pos)//根据输入的位置找前驱元
 94 {
 95     if (pos <= num_of_Node(L) && pos >= 1)
 96     {
 97         position p = L;
 98         while (pos-1)
 99         {
100             pos--;
101             p = p->next;
102         }
103         return p;
104     }
105     else
106         cout << "请输入正确的位置" << endl;
107 }
108 void delete_Node(list L, int x)//根据值来删除结点
109 {
110     position p, tmpcell;
111     p = find_preNode(L, x);
112     if (!islast(L, x))//判断是否是最后一个值
113     {
114         tmpcell = p->next;
115         p->next = tmpcell->next;
116         free(tmpcell);
117     }
118     else
119         p->next = NULL;
120 }
121 void delete_Node_pos(list L, int pos)//根据位置来删除结点
122 {
123     position p,tmpcell;
124     if (pos == num_of_Node(L))//如果是最后一个结点
125     {
126         p = find_end(L);
127         tmpcell = find_preNode_pos(L, pos);
128         free(p);
129         tmpcell = NULL;
130     }
131     else
132     {
133         p= find_preNode_pos(L, pos);
134         tmpcell = p->next;
135         p->next = tmpcell->next;
136         free(tmpcell);
137     }
138 }
139 void insert(list L, int x, int pos)//插入结点 
140 {
141     position p,tmpcell;
142     if (pos > (num_of_Node(L)+1) || pos <= 0)
143         cout << "位置给出错误" << endl;
144     else
145     {
146         if (pos == (num_of_Node(L) + 1))
147             add_ele_end(L, x);
148         else
149         {
150             p = find_preNode_pos(L, pos);
151             tmpcell = (position)malloc(sizeof(Node));
152             tmpcell->ele = x;
153             tmpcell->next = p->next;
154             p->next = tmpcell;
155         }    
156     }
157 }
158 void delete_list(list L)//删除一个表
159 {
160     position tmpcell,p = L->next;
161     L->next = NULL;
162     while (p != NULL)
163     {
164         tmpcell = p->next;
165         free(p);
166         p = tmpcell;
167     }
168 }
169 int main()
170 {
171     list L = create_empty_list();//创建一个空表
172     add_ele_end(L, 3);
173     add_ele_end(L, 8);
174     add_ele_end(L, 11);//往链表结尾添加结点
175     print_list(L);//打印出所有结点
176     position p = find_pos(L, 8);//根据值找出结点
177     cout << p->ele << endl;
178     delete_Node(L, 3);//根据值删除结点
179     insert(L, 5, 2);//在第二个位置插入值为5的结点
180     insert(L, 6, 4);//在第四个位置插入值为6的结点
181     print_list(L);
182     delete_Node(L, 6);//根据值删除遇到的第一个结点
183     print_list(L);
184     delete_Node_pos(L, 1);//根据位置删除结点
185     print_list(L);
186     if (!isempty(L))
187         cout << "这不是一个空表" << endl;
188     delete_list(L);//删除表
189     if (isempty(L))
190         cout << "这是一个空表" << endl;
191     system("pause");
192     return 0;
193 }

运行结果:

猜你喜欢

转载自www.cnblogs.com/cs0915/p/12128556.html