/
DoubleLindedList.cpp
179 lines (165 loc) · 2.85 KB
/
DoubleLindedList.cpp
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
/*双向链表*/
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node * prior, *next;
}Node,*DoubleList;
int length; //数组长度
//建立双向表
void CreateList(DoubleList L,int *a, int i)
{
Node * head = L;
Node * node;
while(i < length)
{
node = (Node *)malloc(sizeof(Node));
node->next = L->next;
node->data = *(a + i);
L->next = node;
node->prior = L;
L = L->next;
i++;
}
L = head;
}
//打印链表
void PrintList(DoubleList L)
{
printf("打印结果:\n");
for(int j = 0; j < length; j++)
{
L = L->next;
printf("%d\t",L->data);
}
printf("\n");
}
//删除表
void DeleteList(DoubleList L)
{
while(L->next != NULL)
{
Node *t = L->next;
L->next = L->next->next;
free(t);
}
printf("删除成功\n");
}
//查找第i个结点
Node* GetNode(DoubleList L, int i)
{
for(int j = 0; j < i; j++)
{
L = L->next;
}
return L;
}
//查找结点位置
int Locate(DoubleList L, int x)
{
for(int j = 0; j < length; j++)
{
L = L->next;
if(L->data == x)
break;
}
return ++j;
}
//插入结点
void Insert(DoubleList L, int i, int x)
{
Node *node;
node = (Node *)malloc(sizeof(Node));
node->data = x;
for(int j = 0; j < i - 1; j++)
{
L = L->next;
}
Node *t;
t = L->next;
L->next = node;
node->prior = L;
node->next = t;
if(t != NULL)
t->prior = node;
length++;
}
//删除指定位置结点
void DeleteNode(DoubleList L, int i)
{
for(int j = 0; j < i - 1; j++)
{
L = L->next;
}
Node *t;
t = L->next;
if(t->next != NULL)
{
L->next = L->next->next;
L->next->prior = L->next;
}
free(t);
length--;
}
//删除指定结点
void DeleteData(DoubleList L, int x)
{
int i = Locate(L, x);
if(i > length)
printf("删除元素不存在");
else
DeleteNode(L, i);
}
int main()
{
int a[100];
int i = 0;
Node *node;
int x; //需要查找的元素
DoubleList L; //建立链表,L也为头结点
L = (DoubleList)malloc(sizeof(Node));
L->next = NULL;
L->prior = NULL;
printf("请输入链表的长度:\n");
scanf("%d",&length);
for(; i < length; i++)
{
scanf("%d",&a[i]);
if(getchar() == '\n')
break;
}
//建立链表
CreateList(L, a, 0);
PrintList(L);
//查找结点
printf("请输入查找的节点位置:\n");
scanf("%d",&i);
node = GetNode(L, i);
printf("查找结果:%d\n",node->data);
//定位结点
printf("请输入查找的元素:\n");
scanf("%d",&x);
if(Locate(L,x) > length)
printf("元素不存在!\n");
else
printf("查找结果:%d\n",Locate(L, x));
//插入结点
printf("请输入要插入的位置和插入结点:\n");
scanf("%d%d",&i,&x);
Insert(L, i, x);
PrintList(L);
//删除指定位置元素
printf("请输入删除的位置:\n");
scanf("%d",&i);
DeleteNode(L, i);
PrintList(L);
//删除元素
printf("请输入删除的元素:\n");
scanf("%d",&x);
DeleteData(L, x);
PrintList(L);
//删除表
DeleteList(L);
return 0;
}