带头结点单链表的练习在尾插入删除一个结点Word下载.docx
- 文档编号:14107115
- 上传时间:2022-10-18
- 格式:DOCX
- 页数:19
- 大小:61.58KB
带头结点单链表的练习在尾插入删除一个结点Word下载.docx
《带头结点单链表的练习在尾插入删除一个结点Word下载.docx》由会员分享,可在线阅读,更多相关《带头结点单链表的练习在尾插入删除一个结点Word下载.docx(19页珍藏版)》请在冰豆网上搜索。
string.h>
32.
33.//#include<
conio.h>
34.
35.
36.
37.typedefstructstudent
38.
39.{
40.
41.intdata;
42.
43.structstudent*next;
44.
45.//structstudent*pre;
46.
47.}Node;
48.
49.
50.
51.
52.
53.intGetLength(Node*head)54.
55.{
56.
57.intn=0;
58.
59.Node*p;
60.
61.p=head->
next;
62.
63.while(p!
=NULL)64.
65.{
66.
67.p=p->
68.
69.n++;
70.
71.}
72.
73.returnn;
74.
75.}
76.
77.
78.
79.Node*InsertList(Node*head,intx,inti)//插入元素方法1
80.
81.{
82.
83.Node*p,*q,*s;
84.
85.intj=1;
86.
87.p=head;
88.
89.if(i<
1||i>
GetLength(head)+1)
90.
91.{
92.
93.exit
(1);
94.
95.}
96.
97.s=(Node*)malloc(sizeof(Node));
98.
99.s->
data=x;
100.
101.while(j<
=i)102.
103.{
104.
105.q=p;
106.
107.p=p->
108.
109.j++;
110.
111.}
112.
113.s->
next=q->
114.
115.q->
next=s;
116.
117.returnhead;
118.
119.}
120.
121.
122.
123.voidInsertList2(Node*head,intx,inti)//插入元素方法2
124.
125.{
126.
127.Node*p,*s;
128.
129.intj=1;
130.
131.p=head;
132.
133.if(i<
GetLength(head)+1)//这块儿比删除节点函数多加了一个,因为
可以插入到最后一个节点的后面一个节点。
但是删除节点函数不行,只能删除到最后一个节点
134.
135.{
136.
137.exit
(1);
138.
139.}
140.
141.s=(Node*)malloc(sizeof(Node));
142.
143.s->
144.
145.while(j<
i)146.
147.{
148.
149.p=p->
150.
151.j++;
152.
153.}
154.
155.s->
next=p->
156.
157.p->
158.
159.//returnhead;
160.
161.}
162.
163.Node*Del(Node*head,inti)164.
165.{
166.
167.Node*p,*q;
168.
169.intj=1;
170.
171.p=head;
172.
173.if(i<
GetLength(head))
174.
175.{
176.
177.exit
(1);
178.
179.}
180.
181.while(j<
i)182.
183.{
184.
185.p=p->
186.
187.j++;
188.
189.}
190.
191.q=p->
192.
193.p->
194.
195.free(q);
196.
197.returnhead;
198.
199.}
200.
201.
202.
203.Node*create()
204.
205.{
206.
207.Node*head,*p,*s;
208.
209.intx,cycle=1;
210.
211.head=(Node*)malloc(sizeof(Node));
212.
213.p=head;
//如果换成带头节点的链表。
这里也不能变,切记,不能变成
p=head->
那就错了214.
215.printf("
\npleaseinputthedata:
\n"
);
216.
217.while(cycle)
218.
219.{
220.
221.//printf("
222.
223.scanf("
%d"
&
x);
224.
225.if(x!
=0)
226.
227.{
228.
229.s=(Node*)malloc(sizeof(Node));
230.
231.s->
232.
233.//printf("
\n%d"
s->
data);
234.
235.p->
236.
237.//s->
pre=p;
238.
239.p=s;
240.
241.}
242.
243.else
244.
245.{
246.
247.cycle=0;
248.
249.}
250.
251.}
252.
253.p->
next=NULL;
254.
255.//head=head->
//注意,这句是不带头结点的单链表必须做的,因为按照算法。
输入的第一个数实际上是赋值给了head->
next256.
257.//printf("
\nhead->
data:
%d\n"
head->
258.
259.returnhead;
260.
261.}
262.
263.
264.
265.voidPrintList(Node*head)//打印链表的所有元素
266.
267.{
268.
269.Node*p;
270.
271.p=head->
//1)272.
273.while(p!
=NULL)274.
275.{
276.
277.printf("
%3d"
p->
278.
279.p=p->
280.
281.}
282.
283.printf("
284.
285.}
286.
287.
288.
289.intmain()
290.
291.{
292.
293.Node*p,*q,*t,*head;
294.
295.head=create();
296.
297.//p=p->
//如果是带头节点的单链表建立,除了少了上面那个函数的末尾的步
之外,多了这一步,保证从head的下一个节点输出链表data。
298.
299.printf("
\nnowoutputthelinklist:
300.
301.PrintList(head);
302.
303.
304.
305.InsertList2(head,99,3);
306.
307.//q=q->
308.
309.printf("
\nnowoutputthenewlinklistafterINSERT:
310.
311.PrintList(head);
312.
313./*
314.
315.while(q!
=NULL)316.
317.{
318.
319.printf("
q->
320.
321.q=q->
322.
323.}
324.
325.printf("
326.
327.*/
328.
329.
330.
331.Del(head,3);
332.
333.printf("
\nnowoutputthenewlinklistafterDELETE:
334.
335.PrintList(head);
336.
337./*
338.
339.t=t->
340.
341.while(t!
=NULL);
342.
343.{
344.
345.printf("
t->
346.
347.t=t->
348.
349.}
350.
351.printf("
352.
353.*/
354.
355.system("
pause"
356.
357.return0;
358.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 带头 结点 单链表 练习 插入 删除 一个