顺序表操作集(插入删除查找等)[C语言版]【完整可编译】详细注释
顺序表操作集源代码
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 30
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
/*List结构定义如下:*/
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List MakeEmpty(); /*创建并返回一个空的线性表;*/
ElementType FindKth(int K,List L); /*根据位序K,返回相应元素*/
Position Find( List L, ElementType X ); /*返回线性表中X的位置。若找不到则返回ERROR;*/
bool Insert( List L, ElementType X, Position P ); /*将X插入在位置P并返回true。若空间已满,则打印“表满/FULL”并返回false;如果参数P指向非法位置,则打印“位置不合法/ILLEGAL POSITION”并返回false;*/
bool Delete( List L, Position P ); /*将位置P的元素删除并返回true。若参数P指向非法位置,则打印“位置%d不存在元素/POSITION P EMPTY”(其中P是参数值)并返回false。*/
void PrintList(List L); /*打印线性表*/
int Length(List L); /*返回线性表L的长度n*/
int main() {
List L;
ElementType X,e;
Position P;
int i,N,K,choice = 999;
printf("*********顺序表*********\n");
printf("1.创建\n2.查找\n3.插入\n4.删除\n5.输出\n6.求表长\n7.根据位序K查找元素\n0.安全退出程序\n");
while (choice != 0) {
printf("\t请输入你的选择:");
scanf("%d",&choice);
fflush(stdin);
switch (choice) {
case 0:
printf("\n安全退出程序...");
exit(0);
case 1:
printf("\n创建线性表--请输入线性表长度:");
scanf("%d",&N);
if (N > MAXSIZE) {
printf("超出MAXSIZE");
break;
}
L = MakeEmpty();
printf("请输入%d个元素:",N);
for (i=0; i<N; i++) {
scanf("%d", &L->Data[i]);
Insert(L, L->Data[i], i );
}
break;
case 2:
printf("\n请输入要查找的元素X:");
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR ) printf("查找错误: %d 不是此线性表中元素.\n", X);
else printf("%d 在 %d 处 .\n", X, P);
break;
case 3:
printf("\n请输入要插入的元素X和要插入的位置P:");
scanf("%d %d",&X,&P);
Insert(L, X, P);
break;
case 4:
printf("\n请输入要删除的位置P:");
scanf("%d", &P);
Delete(L, P);
break;
case 5:
PrintList(L);
break;
case 6:
printf(" 表长为%d",Length(L));
break;
case 7:
printf("\n请输入位序K以查找相应元素:");
scanf("%d",&K);
printf(" \n线性表中位序%d处的元素为:%d",K,FindKth(K,L));
break;
default:
printf("无效选择,请确认菜单有此选项再重新输入!");
break;
}
}
return 0;
}
/*创建-----------创建并返回一个空的线性表;*/
List MakeEmpty() {
List L;
L = (List)malloc(sizeof(struct LNode));
L->Last = -1;
return L;
}
/*查找-----------返回线性表中X的位置。若找不到则返回ERROR;*/
Position Find( List L, ElementType X ) {
Position i = 0;
while( i <= L->Last && L->Data[i]!= X )
i++;
if ( i > L->Last ) return ERROR; /* 如果没找到,返回错误信息 */
else return i; /* 找到后返回的是存储位置 */
}
/*插入----------*/ /*注意:在插入位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),这里P是存储下标位置(从0开始),两者差1*/
bool Insert( List L, ElementType X, Position P ) {
/* 在L的指定位置P前插入一个新元素X */
Position i;
if ( L->Last == MAXSIZE-1) {
/* 表空间已满,不能插入 */
printf("表满");
return false;
}
if ( P<0 || P>L->Last+1 ) { /* 检查插入位置的合法性 */
printf("位置不合法");
return false;
}
for( i=L->Last; i>=P; i-- )
L->Data[i+1] = L->Data[i]; /* 将位置P及以后的元素顺序向后移动 */
L->Data[P] = X; /* 新元素插入 */
L->Last++; /* Last仍指向最后元素 */
return true;
}
/*删除--------*/ /*注意:在删除位置参数P上与课程视频有所不同,课程视频中i是序列位序(从1开始),这里P是存储下标位置(从0开始),两者差1*/
bool Delete( List L, Position P ) {
/* 从L中删除指定位置P的元素 */
Position i;
if( P<0 || P>L->Last ) { /* 检查空表及删除位置的合法性 */
printf("位置%d不存在元素", P );
return false;
}
for( i=P+1; i<=L->Last; i++ )
L->Data[i-1] = L->Data[i]; /* 将位置P+1及以后的元素顺序向前移动 */
L->Last--; /* Last仍指向最后元素 */
return true;
}
/*打印顺序表所有元素*/
void PrintList(List L) {
int i;
printf("\n顺序表为:");
for (i = 0; i <= L->Last; i++) {
printf("%d ",L->Data[i]);
}
}
/*求表长-----返回线性表L的长度n*/
int Length(List L) {
int n = 0;
for (n = 0; n <= L->Last; n++);
return n;
}
/*查找------根据位序K,返回相应元素*/
ElementType FindKth(int K,List L) {
int i;
ElementType E;
if (K > L->Last || K < 0) {
printf("输入位序K有误!\n");
return ERROR;
}
for (i = 0; i<=L->Last; i++) {
if (i == K) {
E = L->Data[i];
return E;
break;
}
}
}
运行结果
如果觉得不错,欢迎一键三连