掌握链表的基本算法并完成对链表各个功能的实现
链表的基本运算实现,要求至少具有以下功能:
1. #include
2. #include
3.
4. /*定义*/
5. typedef int DataType;
6. struct Node{
7. int n;
8. DataType info;
9. struct Node *link;
10. };
11. typedef struct Node *LinkList;
12. typedef struct Node *PNode;
13.
14. LinkList initLinkList()
15. {
16. LinkList head;
17. head = (LinkList)malloc(sizeof(struct Node));
18. if (head == NULL)
19. {
20. printf("内存分配失败!\n");
21. return 0;
22. }
23. head->link = NULL;
24. return head;
25. }
26.
27. /*创建链表*/
28. void createLinkList(LinkList head)
29. {
30. LinkList rear;
31. PNode p;
32. head->n = 0;
33. int m;
34. rear = head;
35. scanf_s("%d", &m);
36. while (m != -1)
37. {
38. p = (PNode)malloc(sizeof(struct Node));
39. p->info = m;
40. p->link = NULL;
41. rear->link = p;
42. rear = p;
43. head->n ++ ;
44. scanf_s("%d", &m);
45. }
46. }
47.
48. /*插入运算*/
49. /*查找i所在节点p*/
50. PNode Locate_i(LinkList llist, int i)
51. {
52. PNode p; int count = 0;
53. p = llist->link;
54. While (Count < i)
55. {
56. p = p->link;
57. count++;
58. }
59. return p;
60. }
61.
62. /*判断是否为空*/
63. int isEmptyLinkList(LinkList llist)
64. {
65. return (llist->link == NULL);
66. }
67.
68. /*查找p所指结点的前驱结点*/
69. PNode locatePre_link(LinkList llist, PNode p)
70. {
71. PNode p1;
72. if (isEmptyLinkList(llist))
73. return 0;
74. p1 = llist;
75. while (p1 != NULL&&p1->link != p)
76. p1 = p1->link;
77. return p1;
78. }
79.
80. /*后插法*/
81. int insertPost_link(LinkList llist, PNode p, DataType x)
82. {
83. PNode q = (PNode)malloc(sizeof(struct Node));
84. if (q == NULL)
85. {
86. printf("out of space!\n");
87. return 0;
88. }
89. Else
90. {
91. q->info = x;
92. q->link = p->link;
93. p->link = q;
94. return 1;
95. }
96. }
97.
98. /*前插法*/
99. int insertPre_link(LinkList llist, PNode p, DataType x)
100. {
101. PNode p1 = (PNode)malloc(sizeof(struct Node));
102. if (p1 == NULL)
103. return 0;
104. p1 = llist;
105. while (p1 != NULL&&p1->link != p)
106. p1 = p1->link;
107. insertPost_link(llist, p1, x);
108. return 1;
109. }
110.
111. /*删除运算*/
112. int deleteLinkList(LinkList llist, DataType x)
113. {
114. PNode p, q;
115. p = llist;
116. if (p == NULL)
117. return 0;
118. while (p->link != NULL&&p->link->info != x)
119. p = p->link;/*找值为x的节点的存储位置*/
120. if (p->link == NULL)
121. {
122. printf("No exist!\n");
123. return 0;
124. }
125. else
126. {
127. q = p->link;
128. p->link = q->link;
129. free(q);
130. return 1;
131. }
132. }
133.
134. /*查找运算*/
135. PNode findLinkList(LinkList llist, DataType x)
136. {
137. PNode p;
138. if (llist == NULL)
139. return 0;
140. p = llist->link;
141. while (p != NULL&&p->info != x)
142. p = p->link;
143. return p;
144. }
145.
146. /*输出*/
147. void printLinkList(LinkList head)
148. {
149. int j;
150. j = head->n;
151. head = head->link;
152. While (head)
153. {
154. printf("%d ", head->info);
155. head = head->link;
156. }
157. printf("\n");
158. }
159.
160. int main()
161. {
162. int i,k,flag=0;
163. DataType x, n;
164. LinkList l;
165. PNode p, q;
166. l = initLinkList();
167. printf("------请输入单链表数据以-1结尾------\n");
168. createLinkList(l);
169. printf("此单链表为:\n");
170. printLinkList(l);
171. printf("------插入运算:请选择 前插法选1 后插法选0------\n");
172. scanf_s("%d", &flag);
173. printf("------请选择插入位置------\n");
174. scanf_s("%d", &i);
175. p = Locate_i(l, i-1);
176. if (flag==0)
177. {
178. printf("------后插法:输入x表示插入的数字------\n");
179. scanf_s("%d", &x);
180. insertPost_link(l, p, x);
181. }
182. else if(flag==1)
183. {
184. printf("------前插法:输入x表示插入的数字------\n");
185. q = locatePre_link(l,p);
186. scanf_s("%d", &x);
187. insertPre_link(l, p, x);
188. }
189. else
190. printf("输入有误!!\n");
191. printf("单链表此时为:\n");
192. printLinkList(l);
193. printf("------输入k表示要删除的数据------\n");
194. scanf_s("%d", &k);
195. deleteLinkList(l, k);
196. printf("单链表此时为:\n");
197. printLinkList(l);
198. printf("------输入查找的数据n------\n");
199. scanf_s("%d", &n);
200. if (findLinkList(l, n))
201. printf("YES\n");
202. else
203. printf("NO\n");
204. printf("------判断是否为空链表------\n");
205. if (isEmptyLinkList(l))
206. printf("TRUE\n");
207. else
208. printf("FALSE\n");
209. return 0;
210. }
211.
掌握单链表应用:用链表表示多项式,并实现多项式的加减运算。
设计一个一元稀疏多项式简单的加减法计算器,要求:
1. #include
2. #include
3. #define MAXSIZE 200
4.
5. struct Node
6. {
7. int exp;//指数
8. int coe;//系数
9. struct Node *Next;
10. };
11. typedef struct Node *LinkList;
12. typedef struct Node *PNode;
13.
14. /*初始化链表*/
15. LinkList initList()
16. {
17. LinkList head = (LinkList)malloc(sizeof(struct Node));
18. if (head == NULL)
19. {
20. printf("内存分配失败\n");
21. return 0;
22. }
23. head->Next = NULL;
24. return head;
25. }
26.
27. /*对输入数据进行插入排序处理*/
28. LinkList insertLinkList(LinkList head,PNode p)
29. {
30. PNode q;
31. LinkList pm;
32. pm = (LinkList)malloc(sizeof(struct Node));
33. pm = head;
34. while (pm->Next != NULL)
35. {
36. if (pm->Next->exp > p->exp)//判断指数大小,根据指数顺序来
37. {
38. p->Next = pm->Next;
39. pm->Next = p;//将最大的存入p
40. return 1;
41. }
42. else if (pm->Next->exp == p->exp)
43. {
44. pm->Next->coe += p->coe;
45. if (pm->coe == 0)
46. {
47. q = pm->Next;
48. pm->Next = pm->Next->Next;
49. free(q);
50. }
51. free(p);
52. return 1;
53. }
54. pm = pm->Next;
55. }
56. if (pm->Next == NULL)//当所有节点都比p小时
57. {
58. pm->Next = p;
59. return 1;
60. }
61. }
62.
63. /*输入数据并处理*/
64. LinkList createLinkList(LinkList head)//开始输入数据
65. {
66. PNode p;
67. int data_exp, data_coe;
68. scanf_s("%d%d", &data_coe, &data_exp);
69. p = head;
70. while (data_coe != -1 || data_exp != -1)
71. {
72. p = (PNode)malloc(sizeof(struct Node));
73. if (p == NULL)
74. {
75. printf("内存分配失败\n");
76. return 0;
77. }
78. p->coe = data_coe;
79. p->exp = data_exp;
80. p->Next = NULL;
81. insertLinkList(head,p);//对输入的节点进行排序处理
82. scanf_s("%d%d", &data_coe, &data_exp);
83. }
84. return head;
85. }
86.
87. /*多项式相加*/
88. void addLinkList(LinkList l1,LinkList l2)
89. {
90. PNode p,pr;
91. PNode pa, pb;
92. pr = l1;
93. pa = l1->Next;
94. pb = l2->Next;
95. while (pa != NULL && pb != NULL)
96. {
97. if (pa->exp < pb->exp)
98. {
99. pr = pa;
100. pa = pa->Next;
101. }
102. else if (pa->exp == pb->exp)
103. {
104. if (pa->coe + pb->coe == 0)//如果两者相加为0
105. {
106. pr->Next = pa->Next;
107. p = pa;
108. free(p);
109. }
110. else
111. {
112. pa->coe += pb->coe;
113. pr = pa;
114. }
115. pa = pr->Next;
116. p = pb;
117. pb = pb->Next;
118. free(p);
119. }
120. else//如果要配入的主链表当前节点比l2大的话 将l2当前节点放置l1当前节点的前端并释放l2节点
121. {
122. p = pb->Next;
123. pb->Next = pa;
124. pr->Next = pb;
125. pr = pb;
126. pb = p;
127. }
128. }
129. if(pb)
130. {
131. pr->Next = pb;
132. }
133. }
134.
135. /*输出链表*/
136. void printLinkList(LinkList head)
137. {
138. if (head->Next == NULL)
139. printf("此链表为空!\n");
140. else
141. while (head->Next != NULL)
142. {
143. printf("%d %d\n", head->Next->coe, head->Next->exp);
144. head = head->Next;
145. }
146. }
147.
148. int main()
149. {
150. LinkList LA, LB;
151. printf("输入多项式LA的系数和指数,以-1 -1结尾\n");
152. LA = initList();
153. createLinkList(LA);
154. printf("此链表表示为\n");
155. printLinkList(LA);
156. printf("输入多项式LB的系数和指数,以-1 -1结尾\n");
157. LB = initList();
158. createLinkList(LB);
159. printLinkList(LB);
160. printf("两式相加结果为\n");
161. addLinkList(LA, LB);
162. printLinkList(LA);
163. return 0;
164. }
总觉得DLL封装这个东西学好了做一个小型的项目是很有用的一件事,在自己写这个代码的时候用了一个小时搞明白是怎么写封装,果然deadline的效果威力真大(代码引用的链表中级和初级的代码)
注意:dll文件是要放到Test文件的代码文件那一堆里面去的,lib文件也是
掌握DLL,掌握接口封装的方法
实现链表的接口DLL封装,要求:
1. Create_dll.h
2. #ifndef Create_dll_H
3. #define Create_dll_H
4. /*定义*/
5. typedef int DataType;
6. struct Node {
7. int n;
8. DataType info;
9. struct Node *link;
10. };
11. typedef struct Node *LinkList;
12. typedef struct Node *PNode;
13. __declspec(dllexport) LinkList initLinkList();
14. __declspec(dllexport) void createLinkList(LinkList head);
15. __declspec(dllexport) PNode Locate_i(LinkList llist, int i);
16. __declspec(dllexport) int isEmptyLinkList(LinkList llist);
17. __declspec(dllexport) PNode locatePre_link(LinkList llist, PNode p);
18. __declspec(dllexport) PNode Locate_i(LinkList llist, int i);
19. __declspec(dllexport) int insertPost_link(LinkList llist, PNode p, DataType x);
20. __declspec(dllexport) int insertPre_link(LinkList llist, PNode p, DataType x);
21. __declspec(dllexport) int deleteLinkList(LinkList llist, DataType x);
22. __declspec(dllexport) PNode findLinkList(LinkList llist, DataType x);
23. __declspec(dllexport) void printLinkList(LinkList head);
24. #endif
25.
26. #include
27. #include
28. #include"Create_dll.h"
29. #pragma comment(lib,"Create_dll")
30.
31. LinkList initLinkList()
32. {
33. LinkList head;
34. head = (LinkList)malloc(sizeof(struct Node));
35. if (head == NULL)
36. {
37. printf("内存分配失败!\n");
38. return 0;
39. }
40. head->link = NULL;
41. return head;
42. }
43.
44. /*创建链表*/
45. void createLinkList(LinkList head)
46. {
47. LinkList rear;
48. PNode p;
49. head->n = 0;
50. int m;
51. rear = head;
52. scanf_s("%d", &m);
53. while (m != -1)
54. {
55. p = (PNode)malloc(sizeof(struct Node));
56. p->info = m;
57. p->link = NULL;
58. rear->link = p;
59. rear = p;
60. head->n++;
61. scanf_s("%d", &m);
62. }
63. }
64.
65. /*插入运算*/
66. /*查找i所在节点p*/
67. PNode Locate_i(LinkList llist, int i)
68. {
69. PNode p; int count = 0;
70. p = llist->link;
71. while (count < i)
72. {
73. p = p->link;
74. count++;
75. }
76. return p;
77. }
78.
79. /*判断是否为空*/
80. int isEmptyLinkList(LinkList llist)
81. {
82. return (llist->link == NULL);
83. }
84.
85. /*查找p所指结点的前驱结点*/
86. PNode locatePre_link(LinkList llist, PNode p)
87. {
88. PNode p1;
89. if (isEmptyLinkList(llist))
90. return 0;
91. p1 = llist;
92. while (p1 != NULL&&p1->link != p)
93. p1 = p1->link;
94. return p1;
95. }
96.
97. /*后插法*/
98. int insertPost_link(LinkList llist, PNode p, DataType x)
99. {
100. PNode q = (PNode)malloc(sizeof(struct Node));
101. if (q == NULL)
102. {
103. printf("out of space!\n");
104. return 0;
105. }
106. else
107. {
108. q->info = x;
109. q->link = p->link;
110. p->link = q;
111. return 1;
112. }
113. }
114.
115. /*前插法*/
116. int insertPre_link(LinkList llist, PNode p, DataType x)
117. {
118. PNode p1 = (PNode)malloc(sizeof(struct Node));
119. if (p1 == NULL)
120. return 0;
121. p1 = llist;
122. while (p1 != NULL&&p1->link != p)
123. p1 = p1->link;
124. insertPost_link(llist, p1, x);
125. return 1;
126. }
127.
128. /*删除运算*/
129. int deleteLinkList(LinkList llist, DataType x)
130. {
131. PNode p, q;
132. p = llist;
133. if (p == NULL)
134. return 0;
135. while (p->link != NULL&&p->link->info != x)
136. p = p->link;/*找值为x的节点的存储位置*/
137. if (p->link == NULL)
138. {
139. printf("No exist!\n");
140. return 0;
141. }
142. else
143. {
144. q = p->link;
145. p->link = q->link;
146. free(q);
147. return 1;
148. }
149. }
150.
151. /*查找运算*/
152. PNode findLinkList(LinkList llist, DataType x)
153. {
154. PNode p;
155. if (llist == NULL)
156. return 0;
157. p = llist->link;
158. while (p != NULL&&p->info != x)
159. p = p->link;
160. return p;
161. }
162.
163. /*输出*/
164. void printLinkList(LinkList head)
165. {
166. int j;
167. j = head->n;
168. head = head->link;
169. while (head)
170. {
171. printf("%d ", head->info);
172. head = head->link;
173. }
174. printf("\n");
175. }
176.
177. int main()
178. {
179. int i, k, flag = 0;
180. DataType x, n;
181. LinkList l;
182. PNode p, q;
183. l = initLinkList();
184. printf("------请输入单链表数据以-1结尾------\n");
185. createLinkList(l);
186. printf("此单链表为:\n");
187. printLinkList(l);
188. printf("------插入运算:请选择 前插法选1 后插法选0------\n");
189. scanf_s("%d", &flag);
190. printf("------请选择插入位置------\n");
191. scanf_s("%d", &i);
192. p = Locate_i(l, i - 1);
193. if (flag == 0)
194. {
195. printf("------后插法:输入x表示插入的数字------\n");
196. scanf_s("%d", &x);
197. insertPost_link(l, p, x);
198. }
199. else if (flag == 1)
200. {
201. printf("------前插法:输入x表示插入的数字------\n");
202. q = locatePre_link(l, p);
203. scanf_s("%d", &x);
204. insertPre_link(l, p, x);
205. }
206. else
207. printf("输入有误!!\n");
208. printf("单链表此时为:\n");
209. printLinkList(l);
210. printf("------输入k表示要删除的数据------\n");
211. scanf_s("%d", &k);
212. deleteLinkList(l, k);
213. printf("单链表此时为:\n");
214. printLinkList(l);
215. printf("------输入查找的数据n------\n");
216. scanf_s("%d", &n);
217. if (findLinkList(l, n))
218. printf("YES\n");
219. else
220. printf("NO\n");
221. printf("------判断是否为空链表------\n");
222. if (isEmptyLinkList(l))
223. printf("TRUE\n");
224. else
225. printf("FALSE\n");
226. return 0;
227. }