数据结构 线性表 源代码.docx

上传人:b****5 文档编号:12185553 上传时间:2023-04-17 格式:DOCX 页数:15 大小:83.57KB
下载 相关 举报
数据结构 线性表 源代码.docx_第1页
第1页 / 共15页
数据结构 线性表 源代码.docx_第2页
第2页 / 共15页
数据结构 线性表 源代码.docx_第3页
第3页 / 共15页
数据结构 线性表 源代码.docx_第4页
第4页 / 共15页
数据结构 线性表 源代码.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

数据结构 线性表 源代码.docx

《数据结构 线性表 源代码.docx》由会员分享,可在线阅读,更多相关《数据结构 线性表 源代码.docx(15页珍藏版)》请在冰豆网上搜索。

数据结构 线性表 源代码.docx

数据结构线性表源代码

实验一线性表

姓名:

                     班级:

学号:

                     日期:

1、实验目的:

 

2、实验内容:

 

3、基本思想,原理和算法描述:

 

4、源程序:

(1)单链表的基本操作:

#include

#include

usingnamespacestd;

typedefstructnode{

intdata;

structnode*next;

}*linklist;

 

voidcreate_list(linklist&head,intn)

{

node*p;

head=(linklist)malloc(sizeof(node));

p=head;

intnum;

for(inti=0;i

{

cin>>num;

node*q;

q=(linklist)malloc(sizeof(node));

q->data=num;

p->next=q;

p=q;

}

p->next=NULL;

}

voidprint(linklist&head)

{

node*p;

p=(linklist)malloc(sizeof(node));

p=head->next;

while(p!

=NULL)

{

cout<data<<"";

p=p->next;

}

cout<

}

voidinsert_list(linklist&head,inti,intx)

{

node*q;

q=(linklist)malloc(sizeof(node));

q=head;

intj=1;

while(q!

=NULL&&j

{

q=q->next;

j++;

}

if(q==NULL&&j>i)

{

cout<<"ERROR"<

}

else

{

node*r;

r=(linklist)malloc(sizeof(node));

r->data=x;

r->next=q->next;

q->next=r;

}

}

voiddelete_x(linklist&head,inty)

{

node*p;

node*q;

p=head;

q=p->next;

while(q->data!

=NULL&&q->data!

=y)

{

p=p->next;

q=q->next;

}

if(q->data==NULL)

cout<<"ERROR"<

else

{

p->next=q->next;

free(q);

}

}

voiddelete_list(linklist&head,inti)

{

node*q;

q=(linklist)malloc(sizeof(node));

q=head;

intj=1;

while(q!

=NULL&&j

{

q=q->next;

j++;

}

if(q!

=NULL&&j>i)

{

cout<<"ERROR"<

}

else

{

node*p;

p=(linklist)malloc(sizeof(node));

p=q->next;

q->next=p->next;

free(p);

}

}

voidcount_list(linklist&head)

{

intnum=0;

node*p;

p=head->next;

while(p!

=NULL)

{

num++;

p=p->next;

}

cout<<"链表元素个数为:

"<

}

intmain()

{

linklisthead;

intn;

intj=1;

while(j)

{

cout<<"1.创建非循环单链表"<

cout<<"2.插入元素"<

cout<<"3.删除元素"<

cout<<"4.删除链表指定值的结点"<

cout<<"5.输出链表"<

cout<<"6.链表中元素的个数"<

cout<<"pleaseinputyourchoice:

"<

inta;

inti,x,y;

cin>>a;

switch(a)

{

case1:

cout<<"输入新创建的链表结点个数"<

cin>>n;

create_list(head,n);

break;

case2:

cout<<"请输入插入位置:

"<

cin>>i;

cout<<"请输入插入元素值:

"<

cin>>x;

insert_list(head,i,x);

break;

case3:

cout<<"请输入删除位置:

"<

cin>>i;

delete_list(head,i);

break;

case4:

cout<<"请输入删除元素的值"<

cin>>y;

delete_x(head,y);

break;

case5:

cout<<"链表为:

"<

print(head);

break;

case6:

cout<<"链表中元素个数为:

"<

count_list(head);

default:

j=0;break;

}

}

return0;

}

(2)线性表的应用:

#include

#include

usingnamespacestd;

typedefintElemType;

typedefstructLNode

{

ElemTypedata;

structLNode*next;

}*linklist;

 

voidcreat_list(linklist&L,intn)

{

LNode*p;

L=(linklist)malloc(sizeof(LNode));

p=L;

intnum;

for(inti=0;i

{

cin>>num;

LNode*q;

q=(linklist)malloc(sizeof(LNode));

q->data=num;

p->next=q;

p=q;

}

p->next=NULL;

}

voidprint(linklist&L)

{

LNode*p;

p=(linklist)malloc(sizeof(LNode));

p=L->next;

while(p!

=NULL)

{

cout<data<<"";

p=p->next;

}

cout<

}

voidcreatC(linklist&A,linklist&B,linklist&C)

{

LNode*pa,*pb,*pc;

C=(linklist)malloc(sizeof(LNode));

pa=A->next;

pb=B->next;

pc=C;

while(pa!

=NULL&&pb!

=NULL)

{

if(pa->data>pb->data)

{

pc->next=pa;

pc=pa;

pa=pa->next;

}

else

{

pc->next=pb;

pc=pb;

pb=pb->next;

}

}

pc->next=pa?

pa:

pb;

}

intmain()

{

linklistA,B,C;

intn,m;

cout<<"请输入链表***A***的长度:

";

cin>>n;

cout<<"请从大到小输入链表A的元素"<

creat_list(A,n);

cout<<"输入链表***B***的长度";

cin>>m;

cout<<"请从大到小输入链表B的元素"<

creat_list(B,m);

cout<<"输出递减链表A:

"<

print(A);

cout<<"输出递减链表B:

"<

print(B);

creatC(A,B,C);

cout<<"输出递减链表C:

"<

print(C);

return0;

}

 

5、运行结果分析:

单链表

图1:

创建链表

 

图2:

插入元素

 

图3:

删除元素

图4:

链表元素个数

单链表

线性表的应用

图5:

合并链表

6、实验总结:

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工作范文 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1