顺序表

基于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
#include <stdio.h>

#define MAXLEN 30
#define ElemType int

struct Seqlist {
ElemType *data;
int Maxsize;
int length;
};


void Seqlist_Print(Seqlist &list) {

for (int i = 0; i < list.length; i++) {
printf("%d ", list.data[i]);
}

printf("\n");

}

void Seqlist_Init(Seqlist &list, int n, int Seqlist_init[]) {

list.length = n;

list.Maxsize = MAXLEN;

list.data = &Seqlist_init[0];

Seqlist_Print(list);
}

bool Seqlist_Insert(Seqlist &list, int pos, ElemType element) {

if (pos < 1 || pos > list.length + 1) {
return false;
}

if (list.length == list.Maxsize) {
return false;
}

for (int i = list.length; i >= pos; i--) {
list.data[i] = list.data[i - 1];
}

list.data[pos - 1] = element;

list.length++;

Seqlist_Print(list);

}

bool Seqlist_Delete(Seqlist &list, int pos, int ok) {

if (pos < 1 || pos > list.length) {
return false;
}

for (int i = pos; i < list.length; i++) {
list.data[i - 1] = list.data[i];
}

list.length--;

if (ok)
Seqlist_Print(list);

return true;

}

int Seqlist_Element_Search(Seqlist &list, ElemType element) {

for (int i = 0; i < list.length; i++) {
if (list.data[i] == element) {
printf("查找成功\n");
return i + 1;
}
}
printf("查找失败\n");
return -1;

}

Seqlist Seqlist_Delete_Repeated_Element(Seqlist &list) {

int i = 0;
while (i < list.length - 1) {
if (list.data[i] == list.data[i + 1]) {
Seqlist_Delete(list, i + 1, 0);
} else {
i++;
}
}
return list;

}

int main() {
int n;
scanf("%d", &n);

int Seqlist_init[n];
for (int i = 0; i < n; i++) {
scanf("%d", &Seqlist_init[i]);
}

Seqlist list;
Seqlist_Init(list, n, Seqlist_init);

Seqlist tem;
tem = Seqlist_Delete_Repeated_Element(list);
Seqlist_Print(tem);

return 0;
}

Welcome to my other publishing channels