-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete_more.c
124 lines (108 loc) · 1.97 KB
/
delete_more.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<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef int Status;
#define ERROR 0
#define OK 1
typedef struct Node {
ElemType element;
struct Node * link;
}Node;
typedef struct {
struct Node* first;
int n;
}SingleList;
Status Init(SingleList *L);
Status Output(SingleList L);
Status del(SingleList *L, int mSize,int y);
Status Insert(SingleList *L, int i, ElemType x);
// Status Delete(SeqList *L,int i);
// void Destory(SeqList *L);
// 单链表的初始化
Status Init(SingleList *L) {
L->first = NULL;
L->n = 0;
return OK;
}
Status del(SingleList *L, int n,int y){
Node *q,*p;
int i=0;
if(!L->n){
return ERROR;
}
/*
if(L->first->element==y){
L->first=L->first->link;
i=1;
}
*/
p=L->first;
q=p->link;
for(;i<n;i++){
if(p->element==y){
L->first=q;
free(p);
p=q;
q=p->link;
}
else if(q->element==y){
p->link=q->link;
free(q);
p=p->link;
q=p->link;
}
}
return OK;
}
Status Insert(SingleList *L, int i, ElemType x) {
Node *p, *q;
int j;
if (i<-1 || i>L->n - 1)
return ERROR;
p = L->first;
for (j = 0; j < i; j++) {
p = p->link;
}
q = malloc(sizeof(Node));
q->element = x;
if (i > -1) {
q->link = p->link;
p->link = q;
}
else {
q->link = L->first;
L->first = q;
}
L->n++;
return OK;
}
// 单链表的输出
Status Output(SingleList L) {
Node *p;
if (!L.n)
return ERROR;
p = L.first;
while (p) {
printf("%d ",p->element);
p = p->link;
}
return OK;
}
void main()
{
int i, x, nn,y;
SingleList list;
scanf("%d", &nn);
printf("\n");
Init(&list);
for (i = 0; i < nn; i++) {
scanf("%d", &x);
Insert(&list, i - 1, x);
}
Output(list);
scanf("%d",&y);
del(&list,nn,y);
printf("\n");
Output(list);
printf("\n");
}