-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList_create.cpp
More file actions
134 lines (127 loc) · 2.37 KB
/
List_create.cpp
File metadata and controls
134 lines (127 loc) · 2.37 KB
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
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef int ElemType;
typedef struct
{
ElemType a[MAXSIZE];
int length;
}SqList;
SqList b;
void create_list(SqList* L);
void out_list(SqList L);
void insert_sq(SqList* L, int i, ElemType e);
ElemType delete_sq(SqList* L, int i);
int locat_sq(SqList L, ElemType e);
int main()
{
int k, i;
ElemType e, x;
do
{
printf("\n\n\n");
printf("\n 1.建立线性表");
printf("\n 2.在i位置上插入元素e");
printf("\n 3.删除第i个元素,返回其值");
printf("\n 4.查找值为e的元素");
printf("\n 5.结束程序运行");
printf("\n=======================");
printf("\n 请输入你的选择(1,2,3,4,5)");
scanf_s("%d", &k);
switch (k)
{
case 1:
{
create_list(&b);
out_list(b);
}break;
case 2:
{
printf("\n i,e=?");
scanf_s("%d,%d", &i, &e);
insert_sq(&b, i, e);
out_list(b);
}break;
case 3:
{
printf("\n i=?");
scanf_s("%d", &i);
x = delete_sq(&b, i);
out_list(b);
printf("\n x=%d", x);
}break;
case 4:
{
int loc;
printf("\n e=?");
scanf_s("%d", &e);
loc = locat_sq(b, e);
if (loc == -1)
printf("\b 未找到 &d", loc);
else printf("\n 已找到,元素位置是%d", loc);
}break;
case 5:
{
return 0;
}
}
} while (k < 6);
}
void create_list(SqList* L)
{
printf("\n n=?");
scanf_s("%d", &L->length);
for (int i = 0; i < L->length; i++)
{
printf("\n data %d=?", i);
scanf_s("%d", &(L->a[i]));
}
}
void out_list(SqList L)
{
printf("\n");
for (int i = 0; i <= L.length; i++)
{
printf("%10d", L.a[i]);
}
printf("\n");
}
void insert_sq(SqList* L, int i, ElemType e)
{
int j;
if (L->length == MAXSIZE)
printf("\n overflow!");
else if ((i < 1) || (i > L->length + 1))
printf("\n error i!");
else
{
for (j = L->length - 1; j >= i - 1; j--)
L->a[j + 1] = L->a[j];
L->a[i - 1] = e;
++L->length;
}
}
ElemType delete_sq(SqList* L, int i)
{
ElemType x;
int j;
if ((i < 1) || (i > L->length))
{
printf("\n error i!");
x = -1;
}
else
{
x = L->a[i - 1];
for (j = i; j < L->length; j++)
L->a[j - 1] = L->a[j];
--L->length;
}return(x);
}
int locat_sq(SqList L, ElemType e)
{
int i;
for (i = 0; i < L.length; i++)
if (L.a[i] == e) return i + 1;
return 0;
}