学习日志 姓名: 日期:2018.7.24
|
|
今日学习任务
| 掌握链表的所有操作;(单链表、单向循环,双向循环链表) |
今日任务完成情况
(详细说明本日任务是否按计划完成,开发的代码量) | 任务基本完成。 |
今日开发中出现的问题汇总
| 1、单向链表带表头和不带表头的区别 2、单向循环链表的问题课后自己网上学习 |
今日未解决问题
| 无 |
今日开发收获 |
|
自我评价
(是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等)
| 按开发规定完成既定任务,用流程图来思考链表就基本可以理解单向链表,但有些特殊情况也需要考虑到。 |
其他
|
|
1#include <stdio.h>
2 #include <stdlib.h>
3
4 enum RESULT
5 {
6 LINK_EMPTRY,
7 INSERT_SUCCESS,
8 INSERT_FAIL,
9 DELETE_SUCCESS,
10 DELETE_FAIL,
11 REVERSE_SUCCESS,
12 REVERSE_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 create_link(Link *head)
26 {
27 //*head = NULL;
28 *head = (Link)malloc(sizeof(Node));
29 (*head)->next = NULL;
30 }
31
32 void insert_head_node(Link newnode, Link *head)
33 {
34 newnode->next = (*head)->next;
35 (*head)->next = newnode;
36 }
37
38 void insert_tail_node(Link newnode, Link *head)
39 {
40 Link temp = *head;
41
42 while(temp->next != NULL)
43 {
44 temp = temp->next;
45 }
46
47 temp->next = newnode;
48 newnode->next = NULL;
49 }
50
51 int insert_mid_node(Link newnode, Link *head, int num)
52 {
53 Link temp = (*head)->next;
54
55 while(temp != NULL)
56 {
57 if(temp->num == num)
58 {
59 newnode->next = temp->next;
60 temp->next = newnode;
61 return INSERT_SUCCESS;
62 }
63 temp = temp->next;
64 }
65
66 return INSERT_FAIL;
67 }
68
69 int delete_node(Link *head, int num)
70 {
71
72 Link temp = *head;
73 Link p = temp;
74 temp = temp->next;
75 while(temp != NULL)
76 {
77 if(temp->num == num)
78 {
79 p->next = temp->next;
80 free(temp);
81 temp = NULL;
82 return DELETE_SUCCESS;
83 }
84 p = temp;
85 temp = temp->next;
86 }
87 return DELETE_FAIL;
88 }
89
90 int reverse_link(Link *head)
91 {
92 if((*head)->next == NULL || (*head)->next->next == NULL)
93 {
94 return REVERSE_FAIL;
95 }
95
97 Link p = *head;
98 Link s = p->next;
99 Link t = s->next;
100
101 while(t != NULL)
102 {
103 s->next = p;
104
105 p = s;
106 s = t;
107 t = t->next;
108 }
109 s->next = p;
110
111 (*head)->next->next = NULL;
112 (*head)->next = s;
113
114 return REVERSE_SUCCESS;
115 }
116
117 void display_link(Link head)
118 {
119 Link temp = head->next;
120
121 while(temp != NULL)
122 {
123 printf("%d\n",temp->num);
124 temp = temp->next;
125 }
126
127 }
128
129 int main()
130 {
131 Link head;
132 Link newnode;
133
134 create_link(&head);
135
136 int i;
137 for(i = 0; i < 10; i++)
138 {
139 newnode = (Link)malloc(sizeof(Node));
140 newnode->num = i + 1;
141
142 insert_tail_node(newnode,&head);
143 //insert_head_node(newnode,&head);
144 }
145
146 display_link(head);
147 /*
148 newnode = (Link)malloc(sizeof(Node));
149 newnode->num = 11;
150
151 insert_mid_node(newnode,&head,5);
152 */
153
154 //delete_node(&head,1);
155
156 reverse_link(&head);
157
158 display_link(head);
159
160 return 0;
161 }