-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqList.cpp
More file actions
94 lines (85 loc) · 1.32 KB
/
SqList.cpp
File metadata and controls
94 lines (85 loc) · 1.32 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
//
// Created by yoran on 2022/3/31.
// 顺序表
#include <stdio.h>
#define MaxSize 50
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int len;
}SqList;
//初始化顺序表
void InitSqList(SqList &L)
{
L.len=0;
}
//按值查找
int LocateElem(SqList L,ElemType e)
{
int i=0;
for(i;i<L.len;i++)
{
if(L.data[i]==e)
{
return i+1;
}
}
return 0;
}
//按位查找
ElemType GetElem(SqList L,int i)
{
if(i<1||i>L.len)
{
return NULL;
}
return L.data[i-1];
}
//插入元素
bool ListInsert(SqList &L,int i,ElemType e)
{
if(i<1||i>L.len+1)
{
return false;
}
int j;
for(j=L.len;j>=i;j--)
{
L.data[j]=L.data[j-1];
}
L.data[i-1]=e;
L.len++;
return true;
}
//删除元素
bool ListDelete(SqList &L,int i,ElemType &e)
{
if(i<1||i>L.len)
{
return false;
}
if(L.len==0)
{
return false;
}
e=L.data[i-1];
for(int j=i;j<L.len;j++)
{
L.data[j-1]=L.data[j];
}
L.len--;
return true;
}
//int main()
//{
// ElemType e;
// SqList L;
// InitSqList(L);
// ListInsert(L,1,1);
// ListInsert(L,2,3);
// ListInsert(L,3,4);
// ListInsert(L,4,5);
// ListInsert(L,2,2);
// ListDelete(L,4,e);
// return 0;
//}