头文件
后续的各种顺序表运算都要用,亲测没问题~
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define OK 1
#define ERROR -1
#define OVERFLOW 0
#define LIST_INIT_SIZE 100 //初始分配长度为100
#define LISTINCREMENT 10 //存储空间的分配增加量
/*定义顺序表*/
typedef struct
{
int *elem; //存储空间基地址
int length; //表长度初值为0
int listsize; //表空间初始尺寸
}SeqList;
/*创建顺序表*/
int CreateList_Seq(SeqList *L)
{
int i;
printf("Input the datas of the SeqList:");
for(i=0;i<L->length;i++)
scanf("%d",&L->elem[i]);
return OK;
}
/*顺序表的初始化*/
int InitList_Seq(SeqList *L)
{
L->elem = (int*)malloc(LIST_INIT_SIZE*sizeof(int));//动态分配空间
if(!L->elem)
{
printf("Out of space!!\n");
exit(OVERFLOW);
}
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}
int Output_SeqList(SeqList L)//输出
{
int i;
for(i=0;i<L.length;i++)
printf("%4d",L.elem[i]);
printf("\n");
return OK;
}
插入算法
#include "SeqList.h"
/*插入算法*/
int ListInsert_Seq( SeqList *L, int i, int e)
{
int *p, *q, *new1;
if((i<1)||(i>L->length+1)) //插入元素的参数不合理
return ERROR;
if(L->length>=L->listsize) //开辟新空间
{
new1 = (int*)realloc(L->elem,(L->length+LISTINCREMENT)*sizeof(int));
if(!new1)
exit(OVERFLOW);
L->elem = new1;
L->listsize += LISTINCREMENT;//增加存储容量
}
q = &L->elem[i-1]; //指针q指向插入位置
for(p=&L->elem[L->length-1];p>=q;--p)
{
*(p+1) = *p; //插入位置及其后的元素右移
}
*q = e; //在L的第i个位置插入元素e
L->length++; //线性表长度自增
return OK;
}
void main()
{
int i, n, e;
SeqList L; //定义一个线性表
InitList_Seq(&L); //线性表初始化
printf("\n Input the length of the SeqList L: n=");
scanf("%d",&n);
L.length = n;
CreateList_Seq(&L); //创建线性表
printf("Input the insert data: e = ");
scanf("%d",&e);
printf("Input the insert location: i = ");
scanf("%d",&i);
if(ListInsert_Seq(&L,i,e))
{
printf("output the data of the Seqlist: ");
Output_SeqList(L);
}
else
printf("Can't insert the data!\n");
system("pause");
}
删除算法
#include "SeqList.h"
/*顺序表的删除算法*/
int ListDelete_Seq(SeqList *L, int i, int *e)
{
int *p,*q;
if(i<1||i>L->length)
return ERROR; //i值不合法
p = &L->elem[i-1]; //被删除元素的位置
*e = *p; //被删除元素的值赋给e
q = L->elem + L->length-1;//表尾元素的位置
for(++p;p<=q;p++) //++p为移动的第一个元素
*(p-1) = *p; //元素左移
L->length--; //顺序表长度减一
return OK;
}
void main()
{
int i,n,e;
SeqList L; //定义一个顺序表
InitList_Seq(&L);//初始化
printf("\n Input the length of the Seqlist L: n=");
scanf("%d",&n);
L.length = n;
CreateList_Seq(&L);//创建顺序表
printf("Input the delete location: i=");
scanf("%d",&i);
if(ListDelete_Seq(&L,i,&e))
{
printf("Delete the data %d:\n",e);
printf("Output the data of the Seqlist: ");
Output_SeqList(L);
}
else
printf("Can't find the delete data!\n");
system("pause");
}
查找算法
#include "SeqList.h"
/*按内容查找算法*/
int ListSearch_Seq( SeqList *L,int n, int e)
{
int i=1;
while(i<=L->elem[i]&&L->elem[i]!=e)
i++;
if(i>L->length) return ERROR;
else return i;
}
void main()
{
int i, n, e;
SeqList L; //定义一个线性表
InitList_Seq(&L); //线性表初始化
printf("\n Input the length of the SeqList L: n=");
scanf("%d",&n);
L.length = n;
CreateList_Seq(&L); //创建线性表
printf("Input the insert data: e = ");
scanf("%d",&e);
if(ListSearch_Seq(&L,n,e)!=-1)
printf("\n%d在顺序表的第%d位\nn",e,ListSearch_Seq(&L,n,e)+1);
system("pause");
}