-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.1.c
More file actions
171 lines (158 loc) · 2.76 KB
/
1.1.c
File metadata and controls
171 lines (158 loc) · 2.76 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
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
#define OVERFLOW -1
#define OK 1
#define ERROR 0
#define MAXSIZE 6
#define INCREMENT 10
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList *L)
{
(*L).elem=(ElemType *)malloc(MAXSIZE*sizeof(ElemType));
if(!((*L).elem))
exit(OVERFLOW);
L->length=0;
(*L).listsize=MAXSIZE;
return OK;
}
Status InserList(SqList *L,int i,ElemType e)
{
if(i<1||i>(*L).length+1)
return ERROR;
ElemType *p;
int j;
if((*L).length>=(*L).listsize)
{
p=(ElemType *)realloc((*L).elem,((*L).listsize+INCREMENT)*sizeof(ElemType));
if(!p)
exit (OVERFLOW);
(*L).elem=p;
(*L).listsize+=INCREMENT;
}
for(j=(*L).length-1;j>=i-1;--j)
(*L).elem[j+1]=(*L).elem[j];
(*L).elem[i-1]=e;
(*L).length++;
return OK;
}//InsertList
void PrintList(SqList L)
{
int i;
for(i=0;i<L.length;i++)
{
printf("%d ",L.elem[i]);
}
printf("\n");
}
int SearchList(SqList L,ElemType e)
{
int i;
for(i=0;i<L.length;i++)
{
if(e==L.elem[i])
return i;
}
return -1;
}
int Del_List1(SqList *L,ElemType e)
{
int i,j;
for(i=0;i<(*L).length;i++)
if((*L).elem[i]==e)
break;
if(i<(*L).length)
{
for(j=i;j<(*L).length-1;j++)
(*L).elem[j]=(*L).elem[j+1];
(*L).length--;
return i;
}
return -1;
}
Status Del_List2(SqList *L,int i,ElemType *e)
{
if(i<1||i>(*L).length)
return ERROR;
int j;
*e=(*L).elem[i-1];
for(j=i;j<(*L).length;j++)
{
(*L).elem[j-1]=(*L).elem[j];
}
--(*L).length;
return OK;
}
void main()
{
SqList LL;
ElemType x;
int r,i;
printf("(1)初始化顺序表......\n");
if(!InitList(&LL))
return ;
printf("初始化成功!");
printf("(2)顺序表的插入操作.....\n");
while(1)
{
printf("输入插入元素的值(0:结束)=>");
scanf("%d",&x);
if(x==0)
break;
printf("输入插入的位置");
scanf("%d",&r);
InserList(&LL,r,x);
printf("线性表输出");
PrintList(LL);
}
printf("(3)顺序表上的查找操作....\n");
while(1)
{
printf("输入查找的元素的值(0:结束)=>");
scanf("%d",&x);
if(x==0)
break;
r=SearchList(LL,x);
if(r<0)
printf("没找到!\n");
else
printf("有符合条件的元素,位置为:%d\n",r+1);
}
printf("(4)顺序表中指定元素值的删除操作....\n");
while(1)
{
printf("输入删除元素的值(0:结束)=>");
scanf("%d",&x);
if(x==0)
break;
r=Del_List1(&LL,x);
if(r<0)
printf("没找到\n");
else
{
printf("删除成功!被删除元素的位置是:%d\n 线性表输出;",r+1);
PrintList(LL);
}
}
printf("(5)顺序表中指定元素位置的删除操作....\n");
while(1)
{
printf("输入删除元素的位置(0:结束)=>");
scanf("%d",&r);
if(r==0)
break;
if(!Del_List2(&LL,r,&i))
printf("位置越界!\n");
else
{
printf("线性表输出: ");
PrintList(LL);
}
}
}