过程
代码
//头插法
void insertHeadSeqList(SeqList *list, int data) {
if (list->size == list->length) {
expandSeqList(list);
}
for (int i = list->size; i > 0; --i) {
list->data[i] = list->data[i - 1];
}
list->data[0] = data;
list->size++;
}
过程
代码
//尾插法
void insertTailSeqList(SeqList *list, int data) {
if (list->size == list->length) {
expandSeqList(list);
}
list->data[list->size++] = data;
}
过程
代码
//指定下标插入
void insertSeqList(SeqList *list, int data, int index) {
if (index < 0 || index > list->size) {
printf("插入失败!\n");
return;
}
if (list->size == list->length) {
expandSeqList(list);
}
for (int i = list->size; i > index; --i) {
list->data[i] = list->data[i - 1];
}
list->data[index] = data;
list->size++;
}
过程
代码
//指定元素删除
void deleteDataSeqList(SeqList *list, int data) {
for (int i = 0; i < list->size; ++i) {
if (list->data[i] == data) {
for (int j = i; j < list->size - 1; j++) {
list->data[j] = list->data[j + 1];
}
list->size--;
i--;
}
}
}
过程
代码
//指定下标删除
void deleteIndexSeqList(SeqList *list, int index) {
if (index < 0 || index > list->size - 1) {
printf("删除失败!\n");
return;
}
for (int i = index; i < list->size - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->size--;
}
#include <stdio.h>
#include <stdlib.h>
//顺序表
typedef struct SeqList {
int *data; //数据
int size; //有效数据大小
int length; //当前总容量长度
} SeqList;
//初始化
void initSeqList(SeqList *list) {
list->size = 0;
list->length = 10;
list->data = (int *) malloc(sizeof(int) * list->length);
}
//扩容
void expandSeqList(SeqList *list) {
list->length += 10;
int *data = (int *) realloc(list->data, sizeof(int) * list->length);
list->data = data;
}
//头插法
void insertHeadSeqList(SeqList *list, int data) {
if (list->size == list->length) {
expandSeqList(list);
}
for (int i = list->size; i > 0; --i) {
list->data[i] = list->data[i - 1];
}
list->data[0] = data;
list->size++;
}
//尾插法
void insertTailSeqList(SeqList *list, int data) {
if (list->size == list->length) {
expandSeqList(list);
}
list->data[list->size++] = data;
}
//指定下标插入
void insertSeqList(SeqList *list, int data, int index) {
if (index < 0 || index > list->size) {
printf("插入失败!\n");
return;
}
if (list->size == list->length) {
expandSeqList(list);
}
for (int i = list->size; i > index; --i) {
list->data[i] = list->data[i - 1];
}
list->data[index] = data;
list->size++;
}
//指定元素删除
void deleteDataSeqList(SeqList *list, int data) {
for (int i = 0; i < list->size; ++i) {
if (list->data[i] == data) {
for (int j = i; j < list->size - 1; j++) {
list->data[j] = list->data[j + 1];
}
list->size--;
i--;
}
}
}
//指定下标删除
void deleteIndexSeqList(SeqList *list, int index) {
if (index < 0 || index > list->size - 1) {
printf("删除失败!\n");
return;
}
for (int i = index; i < list->size - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->size--;
}
//打印顺序表
void printSeqList(SeqList *list) {
for (int i = 0; i < list->size; ++i) {
printf("%d ", list->data[i]);
}
printf("\n");
printf("有效数据大小:%d\n", list->size);
printf("当前容量大小:%d\n", list->length);
}
//测试代码
void test() {
SeqList list;
initSeqList(&list);
int sel, data, index;
while (true) {
printf("1.头插法插入\n2.尾插法插入\n3.指定下标插入\n4.指定元素删除\n5.指定下标删除\n6.打印顺序表\n");
scanf("%d", &sel);
switch (sel) {
case 1: {
printf("元素:");
scanf("%d", &data);
insertHeadSeqList(&list, data);
break;
}
case 2: {
printf("元素:");
scanf("%d", &data);
insertTailSeqList(&list, data);
break;
}
case 3: {
printf("元素:");
scanf("%d", &data);
printf("下标:");
scanf("%d", &index);
insertSeqList(&list, data, index);
break;
}
case 4: {
printf("元素:");
scanf("%d", &data);
deleteDataSeqList(&list, data);
break;
}
case 5: {
printf("下标:");
scanf("%d", &index);
deleteIndexSeqList(&list, index);
break;
}
case 6: {
printSeqList(&list);
break;
}
default:
return;
}
system("pause");
system("cls");
}
}
int main() {
test();
return 0;
}