定义:顺序表是用一组地址连续的存储单元依次存储线性表中的数据元素,从而使的逻辑上相邻的两个元素在物理位置上也相邻。
特点:
// 头文件SeqList.h开始
#include
#include
#include
#define OK 1
#define ERROR 0
#define OVERFLOW 0
#define LIST_INIT_SIZE 100 // 存储空间初始分配量
#define LISTINCREMENT 10 //存储空间分配增量
typedef struct{
int *elem;
int length; //当前表长
int listsize; // 当前分配的存储容量
}SeqList;
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;
}
int CreateList_Seq(SeqList L){ //建立顺序表
int i;
printf ("Input the datas: ");
for(i=0;i>;i++){
scanf ("%d",&L.elem[i]);
}
return OK;
}
int CreateList_Seq(SeqList L)
{// 建立顺序表
int i;
printf ("Input the datas: ");
for(i=0;i<L.length;i++)
scanf ("%d",&L.elem[i]);
return OK;
}
//顺序表的建立
#include "SeqList.h"
void main(){
int n;
SeqList L;
InitList_Seq (&L);
printf ("\n Input the length of the list L: "); // 输入顺序表的长度
scanf("%d",&n);
L.length = n; //获取顺序表长度
CreateList_Seq (L);
printf ("Output the datas: "); // 输出顺序表
Output_SeqList(L);
printf("\n");
printf("the lenth of the List L: %d",L.length);
}
//顺序表元素的插入
#include "SeqList.h"
int ListInsert_Seq(SeqList *L,int i,int e){ // 在第 i 个元素之前插入元素至顺序表
int *p,*q,*newbase;
if((i<1)||(i>L->length+1)){ //在第0个或者超出顺序表插入元素,插入失败,返回erroe
return ERROR;
}
if(L->length>=L->listsize){ // 当前空间已满,增加分配
newbase = (int *)realloc(L->elem,(L->length+LISTINCREMENT)*sizeof(int));
if(!newbase){
exit(OVERFLOW);
}
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
q = &L->elem[i-1]; // q 指向插入位置
for(p=&L->elem[L->length-1];p>=q;p--){ // 第 n 至第 i 个元素依次后移一个位置
*(p+1) = *p;
}
*q = e; // 插入新元素 e
L->length++; // 注意!要传值回去,形参L必须用指针变量
return OK;
}
int main(){ //在顺序表的第i个位置插入元素e
int i,n,e;
SeqList L;
InitList_Seq(&L);
printf("\nInput the length of the list L: ");
scanf("%d",&n);
L.length = n;
CreateList_Seq(L);
printf("Input the insert data: ");
scanf("%d",&e);
printf("Input the insert location: ");
scanf("%d",&i);
if(ListInsert_Seq(&L,i,e)){
printf("Output the datas:");
Output_SeqList(L);
}
else{
printf("Can't insert the data!\n");
}
return 0;
}
删除表中第i个元素并用变量*e返回其值。
//顺序表元素的删除
#include "SeqList.h"
int ListDelete_Seq(SeqList *L, int i, int *e) {
int *p,*q;
if((i<1)||(i>L->length)){ //第0个或者超出顺序表输出元素,输出失败,返回erroe
return ERROR;
}
p = &L->elem[i-1]; //p指向要删除元素的地址
*e = *p;
q = L->elem + L->length-1; //令q指向最后一个元素的地址
for(++p; p<=q; p++){
*(p-1) = *p;
}
L->length--; //输出一个元素后,顺序表长度减1
return OK;
}
void main(){
int i,n,e;
SeqList L;
InitList_Seq(&L);
printf("\nInput the length of the list L: ");
scanf("%d",&n);
L.length = n;
CreateList_Seq(L);
printf("Input the delete location: ");
scanf("%d",&i);
if(ListDelete_Seq(&L,i,&e)){
printf("Delete the data %d:\n",e);
printf("Output the datas: ");
Output_SeqList(L);
}
else{
printf("Can't find the delete data!\n");
}
}
// 顺序表的查找
#include "SeqList.h"
int LocateElem(SeqList L,int e){
int i;
i = 0;
while(i<=L.length-1 && L.elem[i] != e){ //遍历顺序表,在小于顺序表长度中查找e,直到找到e,结束循环
++i;
}
if(i<L.length){
return i;
} else{
return -1;
}
}
void main(){
int i,n,e;
SeqList L;
InitList_Seq(&L);
printf("\nInput the length of the list L: ");
scanf("%d",&n);
L.length = n;
CreateList_Seq(L);
printf("Input the search data: ");
scanf("%d",&e);
if((i=LocateElem(L,e))>=0){
printf("The search data subscript is %d in the L\n",i);
}
else{
printf("There is no search data in the L!\n");
}
printf("Output the datas: ");
Output_SeqList(L);
}