双向链表的基本操作
1.利用尾插法建立一个双向链表。
2.遍历双向链表。
3.实现双向链表中删除一个指定元素。
4.在非递减有序双向链表中实现插入元素e仍有序算法。
5.判断双向链表中元素是否对称若对称返回1否则返回0。
6.设元素为正整型,实现算法把所有奇数排列在偶数之前。
7.在主函数中设计一个简单的菜单调试上述算法。
实例代码:
?
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
|
//排序的时候因为没有说明奇数和偶数需不需要各自再排序,我就没有排序,只是将奇数放在偶数后面。
//创建链表的时候,因为这个实验没有要求输出链表的长度,所以我就输入了一个长度为n的链表。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
node *pre,*next;
}*h,*end;
void CreatList() //创建一个双向链表
{
int n;
node *s,*e;
printf ( "请输入链表长度: " );
scanf ( "%d" ,&n);
printf ( "请输入数据: " );
h=(node *) malloc ( sizeof (node));
s=(node *) malloc ( sizeof (node));
h->pre=NULL;
e=h;
e->next=s;
s->pre=e;
while (n--)
{
e=s;
scanf ( "%d" ,&s->data);
s=(node *) malloc ( sizeof (node));
e->next=s;
s->pre=e;
}
s->next=NULL;
end=s;
return ;
}
void PrintList() //输出链表
{
node *s;
s=h->next;
printf ( "链表数据: " );
while (s!=end)
{
printf ( "%d " ,s->data);
s=s->next;
}
printf ( "\\n" );
return ;
}
void DeletList() //删除链表中的某个元素
{
int x;
int flag;
node *s,*e;
printf ( "请输入需删除元素: " );
scanf ( "%d" ,&x);
s=h->next;
e=h;
flag=0;
while (s!=end)
{
if (s->data==x)
{
e->next=s->next;
s->next->pre=e;
free (s);
flag=1;
break ;
}
e=s;
s=e->next;
}
if (!flag)
printf ( "链表中不存在值为%d的元素。\\n" ,x); //如果链表中没有x,输出这句话。
return ;
}
void InsetList() //在有序链表中插入某个元素
{
int x;
node *s,*e;
printf ( "输入需要插入的元素: " );
scanf ( "%d" ,&x);
s=h->next;
while (1)
{
if (s->data>=x)
{
e=(node *) malloc ( sizeof (node));
e->data=x;
e->next=s;
e->pre=s->pre;
s->pre->next=e;
s->pre=e;
break ;
}
else if (s==end) //将x放入链表末尾
{
end=(node *) malloc ( sizeof (node));
s->data=x;
end->pre=s;
end->next=NULL;
s->next=end;
break ;
}
s=s->next;
}
return ;
}
void JudgeList() //判断双向链表是否对称
{
node *s,*e;
int flag=0;
s=h->next;
e=end->pre;
while (s->data==e->data&&s!=end&&e!=h)
{
s=s->next;
e=e->pre;
}
if (s==end&&e==h)
printf ( "链表对称。\\n" );
else
printf ( "链表不对称。\\n" );
return ;
}
void SortList() //将链表中的奇数放在偶数后面
{
node *s;
node *odd;
int temp;
odd=h->next;
s=h->next;
while (s!=end)
{
if (s->data%2!=0)
{
temp=odd->data;
odd->data=s->data;
s->data=temp;
odd=odd->next;
s=s->next;
}
else
s=s->next;
}
return ;
}
int PrintMenu() //打印目录
{
int T;
printf ( "******************目录******************\\n" );
printf ( "创建一个双向链表: 1\\n" );
printf ( "输出链表: 2\\n" );
printf ( "删除链表中的指定元素: 3\\n" );
printf ( "向链表中插入元素: 4\\n" );
printf ( "判断链表是否对称: 5\\n" );
printf ( "排列链表: 6\\n" );
printf ( "操作结束: 0\\n" );
printf ( "输入操作指令: " );
scanf ( "%d" ,&T);
switch (T)
{
case 1:CreatList(); break ;
case 2:PrintList(); break ;
case 3:DeletList(); break ;
case 4:InsetList(); break ;
case 5:JudgeList(); break ;
case 6:SortList(); break ;
case 0: return 1;
default : printf ( "输入错误。请重新输入。\\n" );
}
return 0;
}
int main()
{
int flag;
while (1)
{
flag=PrintMenu();
if (flag) //通过flag控制循环的跳出
break ;
}
printf ( "谢谢使用!\\n" );
return 0;
}
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 59
-
2025-05-29 34
-
2025-05-29 38
-
详解使用Mybatis-plus + velocity模板生成自定义的代码
2025-05-29 79 -
2025-05-29 54
热门评论