实验二 顺序表与链表Word下载.docx
《实验二 顺序表与链表Word下载.docx》由会员分享,可在线阅读,更多相关《实验二 顺序表与链表Word下载.docx(28页珍藏版)》请在冰豆网上搜索。
malloc.h>
#defineERROR0
#defineMAXSIZE100
#defineOK1
typedefintElemType;
/*定义表元素的类型*/
typedefstructslist{
ElemType*list;
intlistsize;
intlength;
}Sqlist;
Sqlist*L;
/*
(1)---补充顺序表的存储分配表示,采用定长和可变长度存储均可*/
/*函数声明*/
intInitList_sq(Sqlist*L);
intCreateList_sq(Sqlist*L);
intListInsert_sq(Sqlist*L,inti,ElemTypee);
intPrintList_sq(Sqlist*L);
intListDelete_sq(Sqlist*L,inti,ElemType*e);
intListLocate(Sqlist*L,ElemTypee,int*pos);
intmenu_select();
/*
(2)---顺序表的初始化*/
intInitList_sq(Sqlist*L)
{
L->
list=(ElemType*)malloc(MAXSIZE*sizeof(ElemType));
if(L->
list==NULL)
returnERROR;
else
{
length=0;
listsize=MAXSIZE;
}
return0;
}/*InitList*/
/*(3)---创建具有n个元素的顺序表*/
intCreateList_sq(Sqlist*L)
inta,b,c;
printf("
请输入输入数据的个数n:
"
);
scanf("
%d"
&
a);
请输入输入的数据:
for(b=0;
b<
a;
b++)
c);
list[b]=c;
length=L->
length+a;
}/*CreateList*/
/*(4)---输出顺序表中的元素*/
intPrintList_sq(Sqlist*L)
inta;
输出数据:
for(a=0;
a<
L->
length;
a++)
%d"
L->
list[a]);
}/*PrintList*/
/*(5)---在顺序表的第i个位置之前插入新元素e*/
intListInsert_sq(Sqlist*L,inti,ElemTypee)
inta=L->
length-1;
for(;
a>
=i-1;
a--)
list[a+1]=L->
list[a];
list[i-1]=e;
length+=1;
returnOK;
}/*ListInsert*/
/*(6)---在顺序表中删除第i个元素,e返回删除的元素*/
intListDelete_sq(Sqlist*L,inti,ElemType*e)
inta=i-1;
*e=L->
list[i-1];
list[a]=L->
list[a+1];
length-=1;
}/*ListDelete_sq*/
/*(7)---在顺序表中查找指定值元素,pos为返回其位置序号*/
intListLocate(Sqlist*L,ElemTypee,int*pos)
inta,b=0;
if(e==L->
list[a])
b=0;
*pos=a+1;
break;
b=1;
if(b==1)
}/*ListLocate*/
/*定义菜单字符串数组*/
intmenu_select()
char*menu[]={"
\n***************MENU******************\n"
"
1.CreateList\n"
/*创建顺序表*/
2.GetElement\n"
/*查找顺序表中的元素*/
3.Insertdata\n"
/*插入数据*/
4.Deletedata\n"
/*删除数据*/
0.Quit\n"
/*退出*/
};
chars[3];
/*以字符形式保存选择号*/
intc,i;
/*定义整形变量*/
for(i=0;
i<
7;
i++)/*输出主菜单数组*/
%s"
menu[i]);
do
\nEnteryouchoice(0~4):
/*在菜单窗口外显示提示信息*/
s);
/*输入选择项*/
c=atoi(s);
/*将输入的字符串转化为整形数*/
while(c<
0||c>
4);
/*选择项不在0~4之间重输*/
returnc;
/*返回选择项,主程序根据该数调用相应的函数*/
}
/*主函数*/
intmain()
Sqlistsl;
InitList_sq(&
sl);
intm,k;
for(;
;
)/*无限循环*/
switch(menu_select())/*调用主菜单函数,返回值整数作开关语句的条件*/
case1:
\n1-CreateSqlist:
\n"
CreateList_sq(&
\nPrintSqlist:
PrintList_sq(&
case2:
\n3-GetElemfromSqlist:
pleaseinputsearchdata:
k);
intpos;
if(!
ListLocate(&
sl,k,&
pos))
Notfound"
foundtheelement,positionis%d\n"
pos);
case3:
\n4-InsertfromSqlist:
\ninputinsertlocationanddata:
(location,data)\n"
%d,%d"
m,&
if(ListInsert_sq(&
sl,m,k))
\nOK\n"
\nERROR!
case4:
\n5-DeletefromSqlist:
\npleaseinputdeletelocation\n"
intdeldata;
if(ListDelete_sq(&
deldata))
\nDeletedatais%d\n"
deldata);
\nPrintSqlist:
case0:
exit(0);
/*如菜单返回值为0程序结束*/
(1)创建一个顺序表
(2)查找元素位置
(3)插入元素
(4)删除元素
2、按照要求完成程序exp2_2.c,实现单链表的相关操作。
exp2_2.c部分代码如下:
/*
(1)---线性表的单链表存储表示*/
typedefstructLNode
ElemTypedate;
structLNode*next;
}LNode,*LinkList;
LNode*InitList();
/*带头结点单链表初始化*/
voidPrintList(LinkListL);
/*输出带头结点单链表的所有元素*/
intGetElem(LinkListL,inti,ElemType*e);
/*查找第i位置的元素,并由e返回其值*/
intInsertElem(LinkListL,inti,ElemTypee);
/*在第i个位置插入元素e*/
intDeleteElem(LinkListL,inti,ElemType*e);
/*删除第i位置的元素,并由e返回其值*/
voidDestroyLinkList(LinkListL);
/*释放链表及其空间*/
LinkListCreateList(intn);
/*创建n个结点的单链表*/
/*菜单函数*/
/*带头结点单链表初始化*/
LNode*InitList()
LinkListL;
L=(LNode*)malloc(sizeof(LNode));
/*申请一个头结点*/
L)returnERROR;
/*申请失败*/
next=NULL;
/*头结点的指针域置空*/
returnL;
/*
(1)---输出带头结点单链表的所有元素*/
voidPrintList(LinkListL)
LNode*p=L->
next;
inti=0;
while(p)
i++;
\n第%d个元素%d"
i,p->
date);
p=p->
/*
(2)---在单链表的第i个位置插入元素e,若插入成功返回OK,插入失败返回ERROR*/
intInsertElem(LinkListL,inti,ElemTypee)
LNode*p=L,*s;
intj=0;
while(p&
&
j<
i-1)
j++;
if(!
p||j>
s=(LNode*)malloc(sizeof(LNode));
s)returnERROR;
s->
date=e;
next=p->
p->
next=s;
}/*InsertElem*/
/*(3)---查找第i位置的元素,若存在返回OK并由e返回其值,若不存在返回ERROR*/
intGetElem(LinkListL,inti,ElemType*e)
LNode*p;
intj=1;
p=L->
i)
*e=p->
date;
}/*GetElem*/
/*(4)---删除第i位置的元素,成功返回OK,并由e返回其值,若不成功返回ERROR,注意删除的结点必须释放其所占空间*/
intDeleteElem(LinkListL,inti,ElemType*e)
s=p->
next=s->
*e=s->
free(s);
}/*DeleteElem*/
/*(5)---创建具有n个结点的单链表,创建成功返回其头指针*/
LinkListCreateList(intn)
LNode*p,*q,*L;
L=InitList();
p=L;
inti=1;
while(i<
=n)
q=(LNode*)malloc(sizeof(LNode));
输入链表的结点date%d:
i++);
q->
q->
next=q;
p=q;
voidDestroyLinkList(LinkListL)
LNode*p=L,*q;
q=p->
free(p);
}/*DestroyLinkList*/
1.InitLinkList\n"
/*初始化链表*/
/*查找元素*/
/*插入元素*/
/*删除元素*/
5.CreateLinkList\n"
/*创建具有n个元素的链表*/
0.DestroyLinkList&
Quit\n"
/*释放链表所占空间&
退出*/
8;
\nEnteryouchoice(0~5):
5);
/*选择项不在0~5之间重输*/
inti,n;
ElemTypee;
LinkListL=NULL;
/*定义指向单链表的指针*/
/*值不同,执行的函数不同,break不能省略*/
\n1-InitLinkList:
L=InitList(L);
if(L!
=NULL)
\nInitLinkListOK!
\nInitLinkListError!
\n2-GetElemfromLinkList:
inputpos="
i);
if(L!
=NULL&
GetElem(L,i,&
e))
No%iis%d"
i,e);
\nPrintfList:
PrintList(L);
Error&
Notexists!
\n3-InserteintoLinkList:
inpute="
e);
InsertElem(L,i,e))
\nInsertOK!
\nInsertError!
\n4-DeletefromLinkList:
DeleteElem(L,i,&
e);
\nDeleteError!
case5:
pleaseinputn:
/*输入单链表的元素个数*/
n);
if(n<
0)
ERROR"
\nCreateLinkList......\n"
L=CreateList(n);
if(L==NULL)
Error!
\nDestroylinklistandfreememory......\n"
DestroyLinkList(L);
L=NULL;
实验结果:
(1)初始化链表:
(2)查找元素:
(3)插入数据:
(4)删除数据:
(5)创建链表:
(6)销毁和退出链表:
3循环链表的应用(约瑟夫回环问题、)
用整数序列1,2,3,…,n表示顺序坐在圆桌周围的人,并采用循环链表作为存储结构。
任意位置k开始计数,计到m让此位置的人出局,重复上述过程,直至只剩下最后一个人。
依次输出每个出局的人的序号。
提示:
用一个无头结点的循环单链表来实现n个元素的存储。
exp2_3.c部分代码如下:
typedefstructLNode/*线性表的单链表存储*/
ElemTypedata;
}LNode,*LinkList;
/*
(1)---创建具有n个结点的无头结点的单向循环链表,返回其头指针*/
LinkListL;
L=(LinkList)malloc(sizeof(LinkList));
LNode*q,*p;
输入元素:
data);
q=L;
n-1;
p=(LNode*)malloc(sizeof(LNode));
p->
data)