今日学习任务
| 掌握无表头单链表和有表头单链表的所有操作(插入、删除、倒序) |
今日任务完成情况
| 今日任务按计划完成,开发的代码量为200行。 |
今日开发中出现的问题汇总
|
|
今日未解决问题
| 无 |
今日开发收获 |
|
自我评价
| 按照开发规范完成了既定任务 |
其他
| 无 |
1、创建一个不带表头的单链表链表,并对此链表插入结点、删除结点和倒序输出。
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 enum RESULT
5 {
6 LINK_EMPTY,
7 INSERT_SUCCEED,
8 INSERT_FAIL,
9 DELETE_SUCCEED,
10 DELETE_FAIL,
11 RESERVE_SUCCEED,
12 RESERVE_FAIL
13 };
14
15 struct node
16 {
17 int num;
18
19 struct node *next;
20 };
21
22 typedef struct node Node;
23 typedef Node * Link;
24
25 void creat_link(Link *head)
26 {
27 *head = NULL;
28 }
29
30 void insert_head_node(Link newnode,Link *head)//头插
31 {
32 newnode->next = *head;
33 *head = newnode;
34 }
35
36 void insert_tail_node(Link newnode,Link *head)//尾插
37 {
38 Link temp;
39
40 if(*head == NULL)
41 {
42 newnode->next = NULL;
43 *head = newnode;
44 }
45 else
46 {
47 temp = *head;
48 while(temp->next != NULL)
49 {
50 temp = temp->next;
51 }
52 newnode->next = NULL;
53 temp->next = newnode;
54 }
55 }
56
57 int insert_mid_node(Link newnode,Link head,int num)//中间插入
58 {
59 Link temp = head;
60
61 if(head == NULL)
62 {
63 return LINK_EMPTY;
64 }
65 else
66 {
67 while(temp->next != NULL)
68 {
69 if(temp->num == num)
70 {
71 newnode->next = temp->next;
72 temp->next = newnode;
73 return INSERT_SUCCEED;
74 }
75 temp = temp->next;
76 }
77 return INSERT_FAIL;
78 }
79 }
80
81 int delete_node(Link *head,int num)//删除结点
82 {
83 Link p;
84 Link temp = *head;
85
86 if(*head == NULL)
87 {
88 return LINK_EMPTY;
89 }
90
91 if((*head)->num == num)
92 {
93 (*head) = (*head)->next;
94 free(temp);
95 temp = NULL;
96 return DELETE_SUCCEED;
97 }
98 else
99 {
100 p = temp;
101 temp = temp->next;
102 while(temp != NULL)
103 {
104 if(temp->num == num)
105 {
106 p->next = temp->next;
107 free(temp);
108 temp = NULL;
109 return DELETE_SUCCEED;
110 }
111 p = temp;
112 temp = temp->next;
113 }
114 return DELETE_FAIL;
115 }
116 }
117
118 int reverse_link(Link *head)//单链表逆序
119 {
120 if(*head == NULL || (*head)->next == NULL)//空链表或者是单结点链表
121 {
122 return RESERVE_FAIL;
123 }
124
125 Link p = *head;//前指针
126 Link s = p->next;//中指针
127 Link t = s->next;//后指针
128
129 while(t != NULL)//后指针不为空执行循环体
130 {
131 s->next = p;//改变中指针中next指针的指向,使其指向前指针
132 p = s;//前指针往后移动一个结点
133 s = t;//中指针往后移动一个结点
134 t = t->next;//后指针往后移动一个结点
135 }
136
137 s->next = p;
138 (*head)->next = NULL;//使头指针中的next指针指向空
139 *head = s;//
140
141 return RESERVE_SUCCEED;
142 }
143
144 void display_link(Link head)//输出链表
145 {
146 Link temp = head;
147
148 while(temp != NULL)
149 {
150 printf("%d\n",temp->num);
151 temp = temp->next;
152 }
153 }
154
155 int main()
156 {
157 int i;
158 Link head;
159 Link newnode;
160
161 creat_link(&head);
162
163 for(i = 0; i < 10; i++)
164 {
165 newnode = (Link)malloc(sizeof(Node));
166 newnode->num = i + 1;
167
168 //insert_head_node(newnode,&head);//头插
169 insert_tail_node(newnode,&head);//尾插
170 }
171 display_link(head);
172
173 #if 0
174 newnode = (Link)malloc(sizeof(Node));//中间插入
175 newnode->num = 11;
176 if(insert_mid_node(newnode,head,5) == INSERT_SUCCEED)
177 {
178 print(head);
179 }
180 else
181 {
182 printf("link is empty or not find num\n");
183 }
184 #endif
185
186 #if 0
187 delete_node(&head,10);//删除结点
188 display_link(head);
189 #endif
190
191 #if 1
192 reverse_link(&head);//单链表逆序
193 display_link(head);
194 #endif
195
196 return 0;
197 }