-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.c
190 lines (171 loc) · 2.92 KB
/
LinkList.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//如果我输入1 2 4 5;在2之后加入3
//输入1 2 3 4 5 6删掉3
typedef
struct node
{
int data;
struct node*next;
}*ptr,snode;
void Add(ptr);
void Delete(ptr);
ptr Input(void);
void Show();
void Search();
void Change(ptr);
void Reverse(ptr);
int main(void)
{
ptr head=Input();
Show();
printf("请输入您的选择");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1: Add(head);
break;
case 2: Delete(head);
break;
case 3: Search(head);
break;
case 4:Change(head);
break;
case 5:Reverse(head);
break;
default:
printf("输入有误,请重新输入\n");
}
putchar('\n');
return 0;
}
void Show()
{
printf("***********1-添加元素********\n");
printf("***********2-删除元素********\n");
printf("***********3-查找元素********\n");
printf("***********4-修改元素********\n");
printf("***********5-倒置元素********\n");
}
void Add(ptr head)
{
printf("请输入要添加的元素:");
int element;
scanf("%d",&element);
while(head!=NULL)
{
if(head->data==element-1)
{
ptr p=(ptr)malloc(sizeof(snode));
p->data=element;
p->next=head->next;
head->next=p;
}
printf("%d ",head->data);
head=head->next;
}
}
void Delete(ptr head)
{
printf("请输入要删除的元素:");
int element;
scanf("%d",&element);
while(head!=NULL)
{
if(head->data==element-1)
{
ptr p=(ptr)malloc(sizeof(snode));
p=head->next;
head->next=p->next;
free(p);
}
printf("%d ",head->data);
head=head->next;
}
}
ptr Input(void)
{
ptr head=NULL,last;
int x;
scanf("%d",&x);
while(x!=0)
{
ptr p=(ptr)malloc(sizeof(snode));
p->data=x;
if(head==NULL)
{
p->next=head;
head=p;
last=p;
}else{
last->next=p;
p->next=NULL;
last=p;
}
scanf("%d",&x);
}
return head;
}
void Search(ptr head)
{
printf("请输入要查找的元素:");
int element;
scanf("%d",&element);
while(head!=NULL)
{
if(head->data==element)
{
printf("存在该元素");
return;
}
head=head->next;
}
printf("不存在该元素");
}
void Change(ptr head)
{
printf("请输入要被修改的元素与修改后的值:");
int element,value;
scanf("%d%d",&element,&value);
while(head!=NULL)
{
if(head->data==element)
{
head->data=value;
}
printf("%d ",head->data);
head=head->next;
}
}
void Reverse(ptr head)
{
// ptr p,q,pr;
// p = head->next;
// q = NULL;
// head->next = NULL;
// while(p){
// pr = p->next;
// p->next = q;
// q = p;
// p = pr;
// }
// head->next = q;
ptr first = head; //总是指向新链表的首部。
ptr now = head->next;
ptr next = now->next;
first->next = NULL; //首节点变成尾节点,尾节点的下一个节点置空,防止环路。
do {
next = now->next;
now->next = first;
first = now;
now = next;
} while (next != NULL);
while(first!=NULL)
{
printf("%d ",first->data);
first=first->next;
}
putchar('\n');
}