-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList_D.c
125 lines (107 loc) · 2.13 KB
/
LinkedList_D.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
#include<stdlib.h>
#include<stdio.h>
typedef struct LinkNodeD{
char Data;
struct LinkNodeD *p,*n;
}LinkNodeD,*PLinkNodeD;
//创建
PLinkNodeD Create(){
PLinkNodeD Head,p,np;
char ch;
Head = (PLinkNodeD)malloc(sizeof(p));
Head->p = Head;
Head->n = NULL;
np = Head;
ch = getchar();
while (ch!='\n')
{
p = (PLinkNodeD)malloc(sizeof(p));
p->p = np;
np->n = p;
p->n = NULL;
p->Data = ch;
np = p;
ch = getchar();
}
return Head;
}
//输出
void Print(PLinkNodeD Head){
while (Head->n!=NULL)
{
printf("%c ",Head->n->Data);
Head = Head->n;
}
printf("\n");
}
//按位置插入
void Ins(PLinkNodeD Head,int i,char ch){
int j = 0;
PLinkNodeD L;
L = (PLinkNodeD)malloc(sizeof(L));
while (Head->n!=NULL)
{
++j;
if (j==i)
{
L->n = Head->n;
L->p = Head;
Head->n->p = L;
Head->n = L;
L->Data = ch;
break;
}
Head = Head->n;
}
}
//删除
void Del(PLinkNodeD Head,int j){
int i = 0;
PLinkNodeD D;
while (Head->n!=NULL)
{
++i;
if (i==j)
{
D = Head->n; //要删除的节点
Head->n = Head->n->n;
if (Head->n == NULL)
{
free(D);
break;
}
Head->n->p = Head;
free(D);
break;
}
Head = Head->n;
}
}
int main(){
PLinkNodeD head;
head = Create();
printf("Source: ");
Print(head);
Ins(head,1,'X');
printf("Insert X in 1: ");
Print(head);
Ins(head,3,'C');
printf("Insert C in 3: ");
Print(head);
Del(head,1);
printf("Delete 1: ");
Print(head);
Del(head,3);
printf("Delete 3: ");
Print(head);
Del(head,2);
printf("Delete 2: ");
Print(head);
Ins(head,1,'B');
printf("Insert B in 1");
Print(head);
Del(head,1);
printf("Delete 1: ");
Print(head);
return 0;
}