顺序表的基本操作及代码(c语言)
时间: 2023-06-06 19:05:13 浏览: 56
Sure, I can help you with that. Here are some basic operations on a sequential list in C language:
1. Creating a sequential list:
```c
#define MAXSIZE 100
typedef struct{
int data[MAXSIZE];
int length;
} SeqList;
SeqList L;
L.length = 0;
```
2. Inserting an element into the sequential list:
```c
int Insert(SeqList *L, int index, int e) {
if (index < 1 || index > L->length + 1) {
return 0;
}
if (L->length >= MAXSIZE) {
return 0;
}
for (int i = L->length; i >= index; i--) {
L->data[i] = L->data[i-1];
}
L->data[index-1] = e;
L->length++;
return 1;
}
```
3. Deleting an element from the sequential list:
```c
int Delete(SeqList *L, int index) {
if (index < 1 || index > L->length) {
return 0;
}
for (int i = index; i < L->length; i++) {
L->data[i-1] = L->data[i];
}
L->length--;
return 1;
}
```
4. Searching for a specific element in the sequential list:
```c
int Search(SeqList *L, int e) {
for (int i = 0; i < L->length; i++) {
if (L->data[i] == e) {
return i+1;
}
}
return 0;
}
```
I hope this helps! Let me know if you have any more questions.