顺序表和单链表的设计与实现
1.顺序表
#include
#include
#define MaxSize 50
typedef char ElemType;
typedef struct
{
ElemType data[MaxSize];
int length;
}Sqlist;//定义顺序表
void CreateList(Sqlist *&L,ElemType a[],int n)
{
int i;
L=(Sqlist *)malloc(sizeof(Sqlist));
for(i=0;idata[i]=a[i];
}
L->length=n;
}//建立顺序表
void InList(Sqlist *&L)
{
L=(Sqlist *)malloc(sizeof(Sqlist));
L->length=0;
}//初始化线性表
bool ListInsert(Sqlist *&L,int i,ElemType e)
{
int j;
if(i<1 ||i>L->length+1)
return false;
i--;
for(j=L->length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return true;
}//插入元素
void DispList(Sqlist *L)
{
int i;
for(i=0;ilength;i++)
printf("%c",L->data[i]);
printf("\n");
}//输出线性表
int ListLength(Sqlist * L)
{
return (L->length);
}//输出线性表的长度
bool ListEmpty(Sqlist *L)
{
return(L->length==0);
}//判断线性表是否为空
bool GetElem(Sqlist *L,int i,ElemType &e)
{
if(i<1||i>L->length)
return false;
e=L->data[i-1];
return true;
}//输出具体元素
int LocateElem(Sqlist *L,ElemType e)
{
int i=0;
while(ilength && L->data[i]!=e)
i++;
if(i>L->length)
return 0;
else
printf("%d\n", i+1);
}//输出元素的下标
bool ListDelete(Sqlist * &L,int i,ElemType &e)
{
int j;
if(i<1||i>L->length)
return false;
i--;
e=L->data[i];
for(j=i;jlength-1;j++)
L->data[j]=L->data[j+1];
L->length--;
return true;
}//删除元素
void DestoryList (Sqlist *&L)
{
free(L);
}
int main()
{
Sqlist *L;
InList(L);//1
ElemType e[]={'a','b','c','d','e'};
ElemType p;
ElemType a1='a';
ElemType f1='f';
int mm=4;
int m3=3;
CreateList(L,e, 5); //2
DispList(L);//3
printf("%d\n",ListLength(L));//4
ListEmpty(L);//5
GetElem(L,3,p);//6
printf("%c\n",p);
LocateElem(L,a1) ;//7
ListInsert(L,mm,f1);//8
DispList(L);//9
GetElem(L,m3,p);//10
DispList(L);//11
DestoryList (L);//12
return 0;
}
2.单链表
#include
#include
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LinkList;//定义线性表
void CreateListR(LinkList *&L,ElemType a[],int n)
{
LinkList *s,*r;
int i;
L=(LinkList *)malloc (sizeof(LinkList));
r=L;
for(i=0;idata=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}//尾插法建表
void InitList(LinkList *&L)
{
L=(LinkList *)malloc (sizeof(LinkList));
L->next=NULL;
}//初始化
void DestoryList(LinkList *&L)
{
LinkList *pre=L,*p=L->next;
while (p!=NULL)
{
free(pre);
pre=p;
p=pre->next;
}
free(pre);
}//销毁线性表
bool ListEmpty(LinkList *L)
{
return(L->next==NULL);
}//判断线性表是否为空
int ListLength(LinkList *L)
{
int n=0;
LinkList *p=L;
while (p->next!=NULL)
{
n++;
p=p->next;
}
return (n);
}//长度
void DispList(LinkList *L)
{
LinkList *p=L->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}//输出线性表
bool GetElem(LinkList *&L,ElemType &e,int i)
{
int j=0;
LinkList *p=L;
while(jnext;
}
if(p==NULL)
return false;
else
{
e=p->data;
return true;
}
}//输出某个数据值
int LocateElem(LinkList *L,ElemType e)
{
int i=1;
LinkList *p=L->next;
while(p!=NULL && p->data!=e)
{
p=p->next;
i++;
}
if(p==NULL)
return(0);
else
printf("%d\n",i);
}//元素的序号
bool ListInsert(LinkList *&L,int i,ElemType e)
{
int j=0;
LinkList *p=L,*s;
while(jnext;
}
if(p==NULL)
return false;
else
{
s=(LinkList *)malloc (sizeof(LinkList));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}//插入数据元素
bool ListDelete(LinkList *&L,int i,ElemType &e)
{
int j=0;
LinkList *p=L,*q;
while(jnext;
}
if(p==NULL)
return false;
else
{
q=p->next;
if(q==NULL)
return false;
e=q->data;
p->next=q->next;
free(q);
return true;
}//删除数据元素
}
int main()
{
LinkList *h;
InitList(h);//1
ElemType a[]={'a','b','c','d','e'};
ElemType e;
ElemType m='f';
ElemType k='a';
CreateListR(h,a,5);//2
DispList(h);//3
printf("%d\n",ListLength(h));//4
ListEmpty(h);//5
GetElem(h,e,3);//6
printf("%c\n",e);
LocateElem(h,k);//7
ListInsert(h,4,m);//8
DispList(h);//9
ListDelete(h,3,e);//10
DispList(h);//11
DestoryList(h);//12*/
return 0;
}