-------------------------------------
典型例题 24:数据结构问题---链表运算集合
-------------------------------------
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 //节点信息;
6 typedef struct student
7 {
8 int data;
9 struct student *next;
10 }node;
11
12 //创建单链表,返回头指针;
13 node *create_list()
14 {
15 node *head,*p,*s;
16 int x,cycle = 1;
17 head = (node*)malloc(sizeof(node));
18 head->next =NULL;
19 p = head;
20 while(cycle)
21 {
22 printf("/nplease input the data:");
23 scanf("%d",&x);
24 if(x!=0)
25 {
26 s = (node*)malloc(sizeof(node));
27 s->data = x;
28 p->next = s;
29 p = s;
30 }else{
31 cycle = 0;
32 }
33 }
34 return (head);
35 }
36
37 //遍历单链表元素;
38 void display_list(node *head)
39 {
40 node *p;
41 p = head->next;
42 if(head->next!= NULL)
43 {
44 while(p->next != NULL)
45 {
46 printf("%d -->",p->data );
47 p = p->next;
48 }
49 printf("%d;/n",p->data);
50 }
51 }
52 //获取单链表长度
53 int getlen_list(node *head)
54 {
55 int length=0;
56 node *p;
57 p = head;
58 while(p != NULL)
59 {
60 p = p->next;
61 length++;
62 }
63 return (length);
64 }
65
66 //单链表删除结点
67 //有两种情况:1、有单独头结点情况,这情况不用考虑删除头结点;
68 //2、没有单独头结点情况,这情况要考虑删除头结点的情况;
69 //现在我的实现有单独的头文件,所有本来不用考虑删除头结点!
70 //不过考虑到兼容性,我还是实现检测这种情况;
71
72 node * delnode_list(node * head,int num)
73 {
74 node *p1,*p2;
75 p1 = head;
76 while(num != p1->data&&p1->next != NULL)
77 {
78 p2 = p1;
79 p1 = p1->next;
80 }
81
82 if(num == p1 -> data)
83 {
84 if(p1 == head)
85 {
86 head = p1->next;
87 }else{
88 p2->next = p1 ->next;
89 }
90 free(p1);
91 }else{
92 printf("/n %d could not been found",num);
93 }
94 return head;
95 }
96 //单链表插入结点;
97 //跟上面差不多;
98 node *insertnode_list(node *head,int num)
99 {
100 node *p0,*p1,*p2;
101 p1 = head;
102 p0 = (node*)malloc(sizeof(node));
103 p0 ->data = num;
104 while(num != p1->data&&p1->next!=NULL)
105 {
106 p2 = p1;
107 p1 = p1->next;
108
109 }
110 if(num <= p1->data)
111 {
112 if(head==p1)
113 {
114 p0->next = p1;
115 head = p0;
116 }else{
117 p2->next = p0;
118 p0->next = p1;
119 }
120 }else{
121 p1->next = p0;
122 p0->next = NULL;
123 }
124 return (head);
125 }
126
127 //主界面选项;
128 int main()
129 {
130 node *listhead;
131 int length;
132 int delnum;
133 int insertnum;
134 menu:
135 printf("/n-- -- -- -- --single list menu -- -- -- -- -/n");
136 printf("create the list -- -- -- -- --input the number 1/n");
137 printf("display the list -- -- -- -- -input the number 2/n");
138 printf("get length of the list -- -- -input the number 3/n");
139 printf("delete node -- -- -- - -- -- -input the number 4/n");
140 printf("insert node -- -- -- - -- -- -input the number 5/n");
141 printf("quit or exit -- -- -- -- -- -input the number 0/n");
142 int choose;
143 scanf("%d",&choose);
144
145 switch(choose)
146 {
147 case 1:
148 listhead = create_list();
149 break;
150 case 2:
151 display_list(listhead);
152 break;
153 case 3:
154 length = getlen_list(listhead);
155 printf("/nThe length of the list is :%d(contain the head node) /n",length );
156 break;
157 case 4:
158 printf("/n Please input the number you want to delete:");
159 scanf("%d",&delnum);
160 listhead = delnode_list(listhead,delnum);
161 break;
162 case 5:
163 printf("/n Please input the number you want to insert:");
164 scanf("%d",&insertnum);
165 listhead = insertnode_list(listhead,insertnum);
166 break;
167 default:
168 goto end;
169 }
170 goto menu;
171 end:
172 return 0;
173 }