双向链表的构建插入删除和显示实验报告Word文档格式.docx
《双向链表的构建插入删除和显示实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《双向链表的构建插入删除和显示实验报告Word文档格式.docx(9页珍藏版)》请在冰豆网上搜索。
1.首先建立一个带头结点的非空的双向链表。
建立函数creat,操作与建立线性链表类似,但需要修改两个方向的指针。
本实验以输入6个学生的数据为例。
2.建立函数GetElemP_DuL,得到第i个元素的位置指针。
3.对链表进展插入操作。
建立函数ListInsert_DuL,输入要插入的学生数据以及位置,调用函数GetElemP_DuL,插入成功返回1,否那么返回0。
4.对链表进展删除操作。
建立函数ListDelete_DuL,输入要删除的学生位置,调用函数GetElemP_DuL,删除成功返回1,否那么返回0。
5.对链表的结果进展输出与显示。
建立函数print,对链表进展正向和反向的输出。
6.建立main函数,把以上五个函数整合到一个程序之中,当输入的数据不为0时可进展屡次删除,插入操作,并对每一次的结果进展显示。
四.实验方法
运行环境:
VisualC++6.0
把所有程序思想写成代码,通过VisualC++编译,得到结果,即运行成功。
本实验程序代码:
#include"
stdio.h"
malloc.h"
#defineNULL0
#defineLENsizeof(structstudent)
#defineOK1
#defineERROR0
structstudent
{
intdata;
structstudent*prior;
structstudent*next;
};
intn;
structstudent*creat(void)
structstudent*L;
structstudent*p1,*p2;
L=(structstudent*)malloc(LEN);
L->
next=NULL;
prior=NULL;
n=0;
p1=p2=(structstudent*)malloc(LEN);
scanf("
%d"
&
p1->
data);
while(p1->
data!
=0)
{
n=n+1;
if(n==1)
{L->
next=p1;
prior=L;
}
else
{
p2->
next->
prior=p2;
}
p2=p1;
p1=(structstudent*)malloc(LEN);
scanf("
}
p2->
next=L;
return(L->
next);
voidprint(structstudent*head)
structstudent*p;
p=head;
printf("
正向输出:
\n"
);
if(head!
=NULL)
do{
printf("
%d\n"
p->
p=p->
next;
}while(p!
=head->
prior);
反向输出:
if(p==head->
prior)
{p=p->
prior;
structstudent*GetElemP_DuL(structstudent*L,inti)
intj=1;
p=L;
while(j!
=i)
p=p->
j++;
returnp;
intListInsert_DuL(structstudent*L,inti,inte)
structstudent*p,*s;
if(!
(p=GetElemP_DuL(L,i)))
returnERROR;
(s=(structstudent*)malloc(LEN)))
returnERROR;
s->
data=e;
prior=p->
p->
prior->
next=s;
next=p;
p->
prior=s;
n=n+1;
returnOK;
intListDelete_DuL(structstudent*L,inti)
inte;
e=p->
data;
next=p->
free(p);
n=n-1;
voidmain()
structstudent*head;
inti,e;
请输入学生的数据:
head=creat();
print(head);
请输入要插入的位置以及数据:
%d%d"
i,&
e);
while(e!
if(ListInsert_DuL(head,i,e)==1)
print(head);
printf("
请输入要删除的位置:
i);
while(i!
if(ListDelete_DuL(head,i)==1)
五.实验结果
运行结果截图:
说明双向链表构建成功。
六.实验结论
可以通过指针的方法建立双向链表并对其进展插入,删除和显示操作。