数据结构实验报告线性表及其应用.docx

上传人:b****6 文档编号:7139943 上传时间:2023-01-21 格式:DOCX 页数:13 大小:91.97KB
下载 相关 举报
数据结构实验报告线性表及其应用.docx_第1页
第1页 / 共13页
数据结构实验报告线性表及其应用.docx_第2页
第2页 / 共13页
数据结构实验报告线性表及其应用.docx_第3页
第3页 / 共13页
数据结构实验报告线性表及其应用.docx_第4页
第4页 / 共13页
数据结构实验报告线性表及其应用.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

数据结构实验报告线性表及其应用.docx

《数据结构实验报告线性表及其应用.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告线性表及其应用.docx(13页珍藏版)》请在冰豆网上搜索。

数据结构实验报告线性表及其应用.docx

数据结构实验报告线性表及其应用

数据结构实验报告

(1)

姓名:

张惠荣学号:

**********专业班级:

计算机科学与技术153

实验目的:

帮助学生掌握线性表的基本操作在顺序和链表这两种存储结构上的实现,尤以链表的操作和应用作为重点。

问题描述:

1、问题描述:

构造一个空的线性表L。

2、在线性表L的第i个元素之前插入新的元素e;

3、在线性表L中删除第i个元素,并用e返回其值。

实验要求:

1、分别利用顺序和链表存储结构实现线性表的存储,并设计出在不同的存储结构中线性表的基本操作算法。

2、在实验过程中,对相同的操作在不同的存储结构下的时间复杂度和空间复杂度进行分析。

算法分析:

顺序结构算法:

#defineLIST_INIT_SIZE100//线性存储空间的初始分配量

#defineLISTINCREMENT10//线性表存储空间的分配增量

typedefstruct{

ElemType*elem;//存储空间基址

intlength;/当前长度

intlistsize;//当前分配的存储量

}Sqlist;

StatusInitList_Sq(SqList&L)

{//操作结果:

构造一个空的顺序线性表

L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));

if(!

L.elem)exit(OVERFLOW);//存储分配失败

L.length=0;//空表长度为0

L.listsize=LIST_INIT_SIZE;//初始存储容量

returnOK;

}//InitList_Sq

StatusListInsert_Sq(SqList&L,inti,ElemTypee){

//在顺序线性表L中第i个元素之前插入新的元素e,

//i的合法值为1<

If(i<1||i>ListLength(L)+1)returnERROR;//i值不合法

If(L.length>=L.listsize){//当前存储空间已满,增加分配

newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREATMENT)*sizeof(ElemType);

if(!

newbase)exit(OVERFLOW);//存储分配失败

L.elem=newbase;//新基址

L.listsize+=LISTINCREMENT;//增加存储容量

}

q=&(L.elem[i-1]);//q为插入位置

for(p=(&(L.elem[L.length-1]);p>=q;--p)

*(p+1)=*p;//插入位置及之后的元素后移

*q=e;//插入e

++L.length;//表长增1

returnOK;

}//ListInsert_Sq

StatusListDelete_Sq(SqList&L,inti,ElemTypee){

//在顺序表L中删除第i个元素,并用e返回其值

if(i<1||i>ListLength(L))returnERROR;//i值不合法

for(p=&(L.elem[i]);p<&(L.elem[L.length];++p)

*p=*(p+1);//删除位置之后的前移

--l.length;//表长减1

returnOK;

}//ListDelete_Sq

链式结构算法:

typedefstructLNode{

ElemTypedata;//数据域

structLNode*next;//指针域

}LNode;

StatusInit_L(LinkList&L)

{//构造一个空的线性链表

L=(Linklist*)malloc(sizeof(LNode));

if(!

L)exit(OVERFLOW)//分配内存失败

L.next=NULL

returnOK;

};Init_L

StatusInsert_L(LinkList&L,inti,,ElemTypee)

{//在第i个元素之前插入e

q=(Linklist*)malloc(sizeof(LNode));//创建插入结点

if(!

q)exit(OVERFLOW)//创建失败

q->data=e;

LNode*p;

p=L.next;

intj=1;

while((p!

=NULL)&&(j

{

p=p.next;

j++;}

p.next=q.next;

q=p.next;

returnOK;}//Insert_L

StatusDelete_L(LinkList&L,inti){

//删除第i个元素

p=L.next;

intj=1;

while((p!

=NULL)&&(j

p=p.next;

j++;}

q=p.next;

p.next=q.next;

e=q.data;

free(q);

returnOK;}//Delete_L

 

实验内容和过程:

根据算法设计C语言程序,在visualstdio上运行。

顺序结构程序:

#include

#include

#defineLIST_INIT_SIZE100

#defineLISTINCREMENT10

#defineOVERFLOW1

#defineOK1

#defineERROR0

typedefintElemType;

typedefintStatus;

typedefstructlist{

ElemType*elem;//存储空间基址

intlength;//当前长度

intlistsize;//当前分配存储量

}Sqlist;

//构造一个空的线性表L

StatusInitlist_Sq(Sqlist&L)

{

L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));

if(!

L.elem)exit(OVERFLOW);

L.length=0;

L.listsize=LIST_INIT_SIZE;

returnOK;

}

//插入操作函数

Statusinsert_Sq(Sqlist&L,inti,inte)

{

ElemType*p,*q,*newbase;

if(i<1||i>L.length+1)

returnERROR;

if(L.length>=L.listsize)//当前存储空间已满,增加分配

{

newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));

if(!

newbase)exit(OVERFLOW);

L.elem=newbase;

L.listsize+=LISTINCREMENT;

}

q=&L.elem[i-1];

for(p=(&L.elem[L.length-1]);p>=q;--p)

{

*(p+1)=*p;

}

*q=e;

++L.length;

returnOK;

}

//删除操作函数

Statusdelete_Sq(Sqlist&L,intj)

{

ElemType*p,*q;

if(j<1||j>L.length+1)

returnERROR;

for(p=&(L.elem[j-1]);p<&(L.elem[L.length]);++p)

{

*p=*(p+1);

}

--L.length;

returnOK;

}

voidshow_Sq(SqlistL)

{

for(inti=0;i

{

printf("%d\t",L.elem[i]);

}

}

intmain()

{

SqlistL;

inti;

inta;//输入元素个数

printf("请输入要创建的元素个数:

\t");

scanf("%d",&a);

Initlist_Sq(L);

L.length=a;

printf("请输入线性表的元素:

");

getchar();

for(i=0;i

{

scanf("%d",&L.elem[i]);

}

show_Sq(L);

loop:

printf("\n请输入要进行的操作:

\n1.插入\n2.删除\n");

intk;

scanf("%d",&k);

switch(k)

{

case1:

intj,e;

printf("请输入第i个元素之前插入e:

\t");

getchar();

scanf("%d%d",&i,&e);

insert_Sq(L,i,e);

show_Sq(L);

break;

case2:

printf("请输入要删除第i个元素:

\t");

getchar();

scanf("%d",&i);

delete_Sq(L,i);

show_Sq(L);

break;

default:

printf("输入的数字不正确\n");

break;

}

printf("\n是否还要进行操作?

(选择是按1,选择否按0):

");

scanf("%d",&i);

if(i==1)

{

gotoloop;

}

return0;

}

 

链式结构程序:

#include

#include

#defineLENsizeof(structLNode)

typedefintElemType;

typedefstructLNode{

ElemTypedata;//数据域

structLNode*next;//指针域

}LNode;

structLNode*Init_L()//建立链表

{

structLNode*head;

structLNode*p,*q;

head=(structLNode*)malloc(LEN);

if(!

head)exit(0);//分配内存失败

head->data=0;

head->next=NULL;

return(head);

}

structLNode*creat(structLNode*head)

{

LNode*p,*q;

inta;//输入元素个数

printf("请输入要创建的元素个数:

\t");

scanf("%d",&a);

getchar();

inti;

for(i=0;i

{

p=(structLNode*)malloc(sizeof(LNode));

if(!

p)exit(0);//分配内存失败

printf("请输入线性表的第%d个元素:

",i+1);

scanf("%d",&p->data);

p->next=NULL;

if(i==0){

q=p;

head->next=p;

}

else

{

q->next=p;

}

q=p;

}

return(head);

}

structLNode*Insert_L(structLNode*head,inti,ElemTypee)//在第i个元素之前插入e

{

structLNode*p,*q;

q=(structLNode*)malloc(LEN);//创建插入结点

if(!

q)exit(0);//创建失败

q->data=e;

p=head->next;

intj=1;

while((p!

=NULL)&&(j

{

p=p->next;

j++;

}

q->next=p->next;

p->next=q;

return(head);

}

structLNode*Delete_L(structLNode*head,inti)//删除第i个元素

{

structLNode*p,*q;

ElemTypee;

p=head->next;

intj=1;

while((p!

=NULL)&&(j

{

p=p->next;

j++;

}

q=p->next;

p->next=q->next;

e=q->data;

free(q);

return(head);

}

voidprint(structLNode*head)

{

inti;

LNode*p;

p=head->next;

while(p!

=NULL)

{

printf("%d\t",p->data);

p=p->next;

}

printf("\n");

}

intmain()

{

structLNode*head;

head=Init_L();

creat(head);

print(head);

loop:

printf("\n请输入要进行的操作:

\n1.插入\n2.删除\n");

intk;

scanf("%d",&k);

switch(k)

{

case1:

inti;ElemTypee;

printf("请输入插入位置及元素(空格隔开)");

scanf("%d%d",&i,&e);

Insert_L(head,i,e);

print(head);

break;

case2:

intj;

printf("删除第j个元素:

\n");

scanf("%d",&j);

Delete_L(head,j);

print(head);

break;

}

printf("\n是否还要进行操作?

(选择是按1,选择否按0):

");

inti;

scanf("%d",&i);

if(i==1)

{

gotoloop;

}

return0;

}

实验结果:

顺序结构程序截图:

 

链式结构程序截图:

 

总结和感想:

经过这次实验,我们通过C语言程序实现了线性表的几个基本操作,对线性表有了更深入的理解。

 

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

当前位置:首页 > 工作范文 > 行政公文

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

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