1、线性表的顺序存储,用如下结构来表示
// list.h 线性表的动态分配顺序存储结构
#define LIST_INIT_SIZE 10 // 线性表存储空间的初始分配量
#define LIST_INCREMENT 2 // 线性表存储空间的分配增量
typedef struct
{
ElemType *elem; // 存储空间基址
int length; // 当前长度
int listsize; // 当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;
/
2、相关函数实现
/
//functions.h 相关的操作函数
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>//realloc函数
#include "list.h"
/
//初始化一个线性表
void initlist(SqList *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)
exit(0);
L->length=0;
L->listsize=LIST_INIT_SIZE;
}
//输出建立链表后的相关信息
void print_list(SqList *L)
{
printf("初始化后的线性链表的相关信息:\n");
printf("L->elem=%d,L->length=%d,L->listsize=%d\n",L->elem,L->length,L->listsize);
}
//返回线性链表的长度
int getlength(SqList *L)
{
return L->length;
}
//在第i个位置之前插入新元素e(位置从1开始,1,2,3.....)
void insertlist(SqList *L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
if(i<1 ||i>L->length+1)
{
printf("插入的位置溢出!");
exit(0);
}
//检查分配的空间是否够用
if(L->length >= L->listsize)
{
newbase=(ElemType *)realloc(L->elem,(LIST_INIT_SIZE+LIST_INCREMENT)*sizeof(ElemType));
if(!newbase)
exit(0);
L->elem=newbase;
L->listsize+=LIST_INCREMENT;
}
q=L->elem+i-1;
for( p=L->elem+L->length-1 ; p>=q ; p-- )
{
*(p+1)=*p;
}
*q=e;
++L->length;
}
//删除一个元素
int deletelist(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1 || i>L->length)
printf("删除的位置溢出!");
p=L->elem+i-1;
e=p;
q=L->elem+L->length-1;//
for(p=p+1 ; p<=q ; ++p)
*(p-1)=*p;
--L->length;
return *e;
}
//给定一个元素,找到它的直接前驱
ElemType priorelem(SqList *L,ElemType cur_e,ElemType *prior_e)
{
// 操作结果:若cur_e是L的数据元素,且不是第一个,则返回它的前驱,
// 否则操作失败
ElemType *p=L->elem+1;//指向第2个元素(第一个元素没有前驱)
ElemType *q=L->elem+L->length-1;//指向最后1个元素
//判断是否为第一个元素
if(cur_e==*(L->elem))
{
printf("%d是链表第一个元素,没有前驱!",cur_e);
exit(0);
}
//
while( (*p!=cur_e) && (p<=q) )
{
p++;
}
if(p>q)
{
printf("%d不是链表中的元素\n",cur_e);
exit(0);//必须加上这个语句来终止后面代码的执行
}
else
prior_e=--p;//prior指向cur_e的前驱
//返回前驱
return *prior_e;
}
//给定一个元素,找到它的后继
ElemType nextelem(SqList *L,ElemType cur_e,ElemType *next_e)
{
// 操作结果:若cur_e是L的数据元素,且不是最后一个,则返回它的后继,
// 否则操作失败
ElemType *p=L->elem;//指向第1个元素
ElemType *q=L->elem+L->length-2;//指向倒数第二个元素(最后一个元素没有后继)
//判断是否为最后一个元素
if(cur_e==*(q+1))
{
printf("%d是链表的最后一个元素,没有后继!",cur_e);
exit(0);
}
//找后继
while( (*q!=cur_e) && (q>=p) )
{
q--;
}
if(q<p)
{
printf("%d不是链表中的元素\n",cur_e);
exit(0);
}
else
next_e=++q;
return *next_e;
}
//输出每个元素
void show_list(SqList *L)
{
int i,length=L->length;
for(i=0;i<length;i++)
printf("%d ",*(L->elem+i));
}
3、主函数
typedef int ElemType;
#include"functions.h"
int main()
{
SqList L;
int *value_delete=0;
ElemType *prior=0,*next=0,prior_elem=0,next_elem=0;
int element;
int L_length;
int i;
initlist(&L);
print_list(&L);
L_length=getlength(&L);
printf("线性链表的长度:%d\n",L_length);
//插入一个元素
for(i=1;i<11;i++)
insertlist(&L,1,i);
insertlist(&L,5,0);
//删除一个元素
printf("删除的元素为:%d",deletelist(&L,5,value_delete));
//deletelist(&L,5);
print_list(&L);
show_list(&L);
//找前驱
printf("\n");
scanf("%d",&element);
prior_elem=priorelem(&L,element,prior);
printf("%d的前驱为:%d\n",element,prior_elem);
//找后继
printf("\n");
scanf("%d",&element);
next_elem=nextelem(&L,element,next);
printf("%d的后继为:%d\n",element,next_elem);
return 0;
}