线性表的基本操作.docx
- 文档编号:3062296
- 上传时间:2022-11-17
- 格式:DOCX
- 页数:23
- 大小:20.17KB
线性表的基本操作.docx
《线性表的基本操作.docx》由会员分享,可在线阅读,更多相关《线性表的基本操作.docx(23页珍藏版)》请在冰豆网上搜索。
线性表的基本操作
实验二线性表的基本操作
一、实验目的
1.掌握用C++/C语言调试程序的基本方法。
2.掌握线性表的顺序存储和链式存储的基本运算,如插入、删除等。
二、实验要求
1.C++/C完成算法设计和程序设计并上机调试通过。
2.撰写实验报告,提供实验结果和数据。
3. 分析算法,要求给出具体的算法分析结果,包括时间复杂度和空间复杂度,并简要给出算法设计小结和心得。
三、实验内容:
1.分析并运行以下各子程序的主要功能。
程序1:
顺序存储的线性表和运算
#include<>
#defineMAXSIZE100
intlist[MAXSIZE];
intn;
/*insertinaseqlist*/
intsq_insert(intlist[],int*p_n,inti,intx)
{intj;
if(i<0||i>*p_n)return
(1);
if(*p_n==MAXSIZE)return
(2);
for(j=*p_n+1;j>i;j--)
list[j]=list[j-1];
list[i]=x;
(*p_n)++;
return(0);
}
/*deleteinaseqlist*/
intsq_delete(intlist[],int*p_n,inti)
{intj;
if(i<0||i>=*p_n)return
(1);
for(j=i+1;j<=*p_n;j++)
list[j-1]=list[j];
(*p_n)--;
return(0);
}
voidmain()
{inti,x,temp;
printf("pleaseinputthenumberforn\n");
printf("n=");
scanf("%d",&n);
for(i=0;i<=n;i++)
{printf("list[%d]=",i);
scanf("%d",&list[i]);}
printf("Thelistbeforeinsertionis\n");
for(i=0;i<=n;i++)printf("%d",list[i]);
printf("\n");
printf("pleaseinputthepositionwhereyouwanttoinsertavalue\nposition=");
scanf("%d",&i);
printf("pleaseinputthevalueyouwanttoinsert.\nx=");
scanf("%d",&x);
temp=sq_insert(list,&n,i,x);
switch(temp)
{case0:
printf("Theinsertionissuccessful!
\n");
printf("Thelistisafterinsertionis\n");
for(i=0;i<=n;i++)printf("%d",list[i]);
printf("\n");
printf("%d\n",n);
break;
case1:
case2:
printf("Theinsertionisnotsuccessful!
\n");break;}
/*deleting*/
printf("Thelistbeforedeletingis\n");
for(i=0;i<=n;i++)printf("%d",list[i]);
printf("\n");
printf("pleaseinputthepositionwhereyouwanttodeleteavalue\nposition=");
scanf("%d",&i);
temp=sq_delete(list,&n,i);
switch(temp)
{case0:
printf("Thedeletingissuccessful!
\n");
printf("Thelistisafterdeletingis\n");
for(i=0;i<=n;i++)printf("%d",list[i]);
printf("\n");
printf("%d",n);
break;
case1:
printf("Thedeletingisnotsuccessful!
");break;}
}
2.分析并运行以下各子程序的主要功能。
程序2链式存储的线性表和运算
#include<>
#include<>
structnode{
chardata;
structnode*next;
};
typedefstructnodeNODE;
/*Thisfunctioncreatesalink_listwithNnodes.*/
NODE*create_link_list(intn)
{inti;
NODE*head,*p,*q;
if(n==0)returnNULL;
head=(NODE*)malloc(sizeof(NODE));
p=head;
printf("Pleaseinput%dcharsforthelinklist\n",n);
for(i=0;i {scanf("%c",&(p->data)); q=(NODE*)malloc(sizeof(NODE)); printf("test3\n"); p->next=q; p=q;} scanf("%c",&(p->data)); getchar(); p->next=NULL; return(head);} /*Thisfunctioninsertsanodewhosevalueisb*/ /*beforethenodewhosevalueisa,ifthenodeisnotexist,*/ /*theninsertitattheendofthelist*/ voidinsert(NODE**p_head,chara,charb) {NODE*p,*q; q=(NODE*)malloc(sizeof(NODE)); q->data=b; q->next=NULL; if(*p_head==NULL)*p_head=q; else {p=(NODE*)malloc(sizeof(NODE)); p=*p_head; while(p->data! =a&&p->next! =NULL) p=p->next; q->next=p->next; p->next=q;} } /*Thefunctiondeletesthenodewhosevalueisa,*/ /*ifsuccess,return0,orreturn1*/ intdeletenode(NODE**p_head,chara) {NODE*p,*q; q=*p_head; if(q==NULL)return (1); if(q->data==a) {*p_head=q->next; free(q); return(0);} else {while(q->data! =a&&q->next! =NULL) {p=q; q=q->next;} if(q->data==a) {p->next=q->next; free(q); return(0);} elsereturn (1);} } voidmain() {NODE*my_head,*p; /*createalinklistwithmnodes*/ intm; charch_a,ch_b; printf("pleaseinputthenumberofnodesforthelink_list\nm="); scanf("%d",&m); getchar(); printf("test1\n"); my_head=(NODE*)malloc(sizeof(NODE)); my_head=create_link_list(m); /*Outputthelinklist*/ printf("Thelinklistislike: \n"); p=my_head; while(p! =NULL) {printf("%c",p->data); p=p->next; } printf("\n"); /*insertanodewhosevalueisbbeforea*/ printf("Pleaseinputthepositionfora\nch_a="); getchar(); scanf("%c",&ch_a); getchar(); printf("Pleaseinputthevaluethatyouwanttoinsert\nch_b="); scanf("%c",&ch_b); getchar(); insert(&my_head,ch_a,ch_b); printf("Thelinklistafterinsertionislike: \n"); p=my_head; while(p! =NULL) {printf("%c",p->data); p=p->next; } printf("\n"); /*deleteanodewhosevalueisa*/ printf("Pleaseinputthepositionforaa="); scanf("%c",&ch_a); getchar(); deletenode(&my_head,ch_a); printf("Thelinklistafterdeletingislike: \n"); p=my_head; while(p! =NULL) {printf("%c",p->data); p=p->next; } printf("\n"); } 3.运行以下程序并分析各子函数的主要功能。 #include<> #include<> structtagNode { intdata; structtagNode*pNext; }; typedefstructtagNode*pNode; //将结点插入到链表的适当位置,这是一个降序排列的链表 // voidinsertList(pNodehead,//链表头结点 pNodepnode)//要插入的结点 { pNod
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 线性 基本 操作