单链表的实现:增删改查
生活随笔
收集整理的這篇文章主要介紹了
单链表的实现:增删改查
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 /*
2 *@author ?
3 *@since 2011/07/29
4 *@function 實現(xiàn)鏈表的常用操作,比較粗糙。
5 * 功能大概是:顯示鏈表結(jié)構(gòu);查詢鏈表;刪除節(jié)點;插入結(jié)點;修改結(jié)點
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 struct node
11 {
12 int value;
13 struct node *next;
14 };
15 /*
16 *插入結(jié)點(單個)
17 */
18 struct node *add_to_list(struct node *list,int n)
19 {
20 struct node *new_node;
21
22 new_node=malloc(sizeof(struct node));
23 if(new_node==NULL)
24 {
25 printf("Error:malloc failed in add_to_list\n");
26 exit(EXIT_FAILURE);
27 }
28 new_node->value=n;
29 new_node->next=list;
30
31 return new_node;
32 }
33 /*
34 *添加結(jié)點(多個)
35 */
36 struct node *read_numbers(void)
37 {
38 struct node *first=NULL;
39 int n;
40
41 printf("Enter a series of integers (0 to terminate): ");
42 for(;;)
43 {
44 scanf("%d",&n);
45 if(n==0)
46 {
47 return first;
48 }
49 first=add_to_list(first,n);
50 }
51 }
52 /*
53 *搜索結(jié)點
54 */
55 struct node *search_list(struct node *list,int n)
56 {
57 struct node *p;
58
59 for(p=list;p!=NULL;p=p->next)
60 if(p->value==n)
61 return p;
62
63 return NULL;
64 }
65 /*
66 *刪除結(jié)點
67 */
68 struct node *delete_node(struct node *list,int n)
69 {
70 struct node *cur,*prev;
71
72 for(cur=list,prev=NULL;
73 cur!=NULL&&cur->value!=n;
74 prev=cur,cur=cur->next)
75 ;
76 if(cur==NULL)
77 {
78 return list;
79 }
80 if(prev==NULL)
81 {
82 list=list->next;
83 }else
84 {
85 prev->next=cur->next;
86 }
87 free(cur);
88
89 return list;
90 }
91 /*
92 *修改結(jié)點
93 */
94 struct node *update_node(struct node *list,int n)
95 {
96 struct node *cur;
97 int new_value;
98
99 for(cur=list;cur!=NULL&&cur->value!=n;cur=cur->next)
100 ;
101 if(cur==NULL)
102 {
103 printf("NO NODE!!!");
104 }
105 printf("please enter the new value: ");
106 scanf("%d",&new_value);
107 cur->value=new_value;
108
109 return list;
110 }
111 /*
112 *打印鏈表結(jié)點結(jié)構(gòu)
113 */
114 void print(struct node *list)
115 {
116 struct node *p;
117
118 printf("The current relation of the list is ");
119
120 for(p=list;p!=NULL;p=p->next)
121 {
122 if(p->next!=NULL)
123 {
124 printf("%d->",p->value);
125 }else
126 {
127 printf("%d\n",p->value);
128 }
129 }
130 }
131
132 int main()
133 {
134 struct node *test,*p;
135 int n;
136
137 test=read_numbers();
138
139 /*
140 *輸出當前鏈表
141 */
142 print(test);
143
144 /*
145 *查找鏈表中結(jié)點,并輸出結(jié)點的值
146 */
147 printf("Please enter the value to look for: ");
148 scanf("%d",&n);
149 p=search_list(test,n);
150 printf("The value of the node is %d\n",p->value);
151
152 /*
153 *刪除結(jié)點并顯示更新后鏈表的結(jié)構(gòu)
154 */
155 printf("Please choose the value of the node you would like to delete: ");
156 scanf("%d",&n);
157 p=delete_node(test,n);
158 print(p);
159
160 /*
161 *修改結(jié)點并顯示更新后鏈表的結(jié)構(gòu)
162 */
163 printf("Please choose the value of the node you would like to update: ");
164 scanf("%d",&n);
165 p=update_node(test,n);
166 print(p);
167
168 return 0;
169 }
2 *@author ?
3 *@since 2011/07/29
4 *@function 實現(xiàn)鏈表的常用操作,比較粗糙。
5 * 功能大概是:顯示鏈表結(jié)構(gòu);查詢鏈表;刪除節(jié)點;插入結(jié)點;修改結(jié)點
6 */
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 struct node
11 {
12 int value;
13 struct node *next;
14 };
15 /*
16 *插入結(jié)點(單個)
17 */
18 struct node *add_to_list(struct node *list,int n)
19 {
20 struct node *new_node;
21
22 new_node=malloc(sizeof(struct node));
23 if(new_node==NULL)
24 {
25 printf("Error:malloc failed in add_to_list\n");
26 exit(EXIT_FAILURE);
27 }
28 new_node->value=n;
29 new_node->next=list;
30
31 return new_node;
32 }
33 /*
34 *添加結(jié)點(多個)
35 */
36 struct node *read_numbers(void)
37 {
38 struct node *first=NULL;
39 int n;
40
41 printf("Enter a series of integers (0 to terminate): ");
42 for(;;)
43 {
44 scanf("%d",&n);
45 if(n==0)
46 {
47 return first;
48 }
49 first=add_to_list(first,n);
50 }
51 }
52 /*
53 *搜索結(jié)點
54 */
55 struct node *search_list(struct node *list,int n)
56 {
57 struct node *p;
58
59 for(p=list;p!=NULL;p=p->next)
60 if(p->value==n)
61 return p;
62
63 return NULL;
64 }
65 /*
66 *刪除結(jié)點
67 */
68 struct node *delete_node(struct node *list,int n)
69 {
70 struct node *cur,*prev;
71
72 for(cur=list,prev=NULL;
73 cur!=NULL&&cur->value!=n;
74 prev=cur,cur=cur->next)
75 ;
76 if(cur==NULL)
77 {
78 return list;
79 }
80 if(prev==NULL)
81 {
82 list=list->next;
83 }else
84 {
85 prev->next=cur->next;
86 }
87 free(cur);
88
89 return list;
90 }
91 /*
92 *修改結(jié)點
93 */
94 struct node *update_node(struct node *list,int n)
95 {
96 struct node *cur;
97 int new_value;
98
99 for(cur=list;cur!=NULL&&cur->value!=n;cur=cur->next)
100 ;
101 if(cur==NULL)
102 {
103 printf("NO NODE!!!");
104 }
105 printf("please enter the new value: ");
106 scanf("%d",&new_value);
107 cur->value=new_value;
108
109 return list;
110 }
111 /*
112 *打印鏈表結(jié)點結(jié)構(gòu)
113 */
114 void print(struct node *list)
115 {
116 struct node *p;
117
118 printf("The current relation of the list is ");
119
120 for(p=list;p!=NULL;p=p->next)
121 {
122 if(p->next!=NULL)
123 {
124 printf("%d->",p->value);
125 }else
126 {
127 printf("%d\n",p->value);
128 }
129 }
130 }
131
132 int main()
133 {
134 struct node *test,*p;
135 int n;
136
137 test=read_numbers();
138
139 /*
140 *輸出當前鏈表
141 */
142 print(test);
143
144 /*
145 *查找鏈表中結(jié)點,并輸出結(jié)點的值
146 */
147 printf("Please enter the value to look for: ");
148 scanf("%d",&n);
149 p=search_list(test,n);
150 printf("The value of the node is %d\n",p->value);
151
152 /*
153 *刪除結(jié)點并顯示更新后鏈表的結(jié)構(gòu)
154 */
155 printf("Please choose the value of the node you would like to delete: ");
156 scanf("%d",&n);
157 p=delete_node(test,n);
158 print(p);
159
160 /*
161 *修改結(jié)點并顯示更新后鏈表的結(jié)構(gòu)
162 */
163 printf("Please choose the value of the node you would like to update: ");
164 scanf("%d",&n);
165 p=update_node(test,n);
166 print(p);
167
168 return 0;
169 }
結(jié)果如下:
轉(zhuǎn)載于:https://www.cnblogs.com/zengge/archive/2011/07/29/2121330.html
總結(jié)
以上是生活随笔為你收集整理的单链表的实现:增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将字符串的首字母变为大写
- 下一篇: java-jpa-criteriaBui