顺序表 删除 插入:
#include<stdio.h>
#include"demo2.h"
#include<stdlib.h>
#define LIST_INIT_SIZE 8
#define N 3
typedef Status;
#define OK
#define ERROR
typedef int ElemType;
typedef struct seqList {
int length;
int listsize;
ElemType * element;
}SeqList;
Status initSeqList(SeqList* seq) {
seq->element = (int*)malloc(LIST_INIT_SIZE * sizeof(int));
if (!seq->element){
exit(0);
}
seq->length = 0;
seq->listsize = LIST_INIT_SIZE;
return OK;
}
Status printSeqList(SeqList *seq ) {
for (int i = 0; i <seq->length; i++){
printf("%d", seq->element[i]);
}
return 0;
}
Status insert(SeqList* L, int i, ElemType x) {
if (i<-1 || i>L->length - 1) {
return ERROR;
}
if (L->length == L->listsize) {
return ERROR;
}
for (int j = L->length - 1; j >=i; j--) {
L->element[j + 1] = L->element[j];
}
L->element[i] = x;
L->length = L->length + 1;
return OK;
}
Status delete (SeqList* L, int i) {
if (i<0||(i>L->length-1)){
return ERROR;
}
if (!L->length){
return ERROR;
}
for (int j = i; j < L->length; j++) {
L->element[j] = L->element[j+1];
}
L->length--;
return OK;
}
int main() {
SeqList seq;
int i, len;
initSeqList(&seq);
printf("请输入线性表的长度: ");
scanf_s("%d", &len);
seq.length = len;
printf("请输入线性表的元素: ");
for (i = 0; i < len; i++) {
scanf_s("%d", seq.element + i);
seq.listsize--;
}
printf("插入前线性表的元素: \n");
printSeqList(&seq);
printf("\n插入后线性表的元素: \n");
insert(&seq, 6,9);
printSeqList(&seq);
}