线性表的基本操作Word格式文档下载.docx
《线性表的基本操作Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《线性表的基本操作Word格式文档下载.docx(23页珍藏版)》请在冰豆网上搜索。
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;
=*p_n)return
(1);
for(j=i+1;
j<
=*p_n;
j++)
list[j-1]=list[j];
(*p_n)--;
voidmain()
{inti,x,temp;
printf("
pleaseinputthenumberforn\n"
);
n="
scanf("
%d"
&
n);
for(i=0;
i<
=n;
i++)
{printf("
list[%d]="
i);
scanf("
list[i]);
}
Thelistbeforeinsertionis\n"
i++)printf("
%d"
list[i]);
\n"
pleaseinputthepositionwhereyouwanttoinsertavalue\nposition="
i);
pleaseinputthevalueyouwanttoinsert.\nx="
x);
temp=sq_insert(list,&
n,i,x);
switch(temp)
{case0:
printf("
Theinsertionissuccessful!
printf("
Thelistisafterinsertionis\n"
for(i=0;
%d\n"
n);
break;
case1:
case2:
Theinsertionisnotsuccessful!
break;
/*deleting*/
Thelistbeforedeletingis\n"
pleaseinputthepositionwhereyouwanttodeleteavalue\nposition="
temp=sq_delete(list,&
n,i);
Thedeletingissuccessful!
Thelistisafterdeletingis\n"
Thedeletingisnotsuccessful!
"
2.分析并运行以下各子程序的主要功能。
程序2链式存储的线性表和运算
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;
Pleaseinput%dcharsforthelinklist\n"
n;
{scanf("
%c"
&
(p->
data));
q=(NODE*)malloc(sizeof(NODE));
test3\n"
p->
next=q;
p=q;
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;
next=NULL;
if(*p_head==NULL)*p_head=q;
else
{p=(NODE*)malloc(sizeof(NODE));
p=*p_head;
while(p->
data!
=a&
&
next!
=NULL)
p=p->
next;
q->
next=p->
next=q;
/*Thefunctiondeletesthenodewhosevalueisa,*/
/*ifsuccess,return0,orreturn1*/
intdeletenode(NODE**p_head,chara)
q=*p_head;
if(q==NULL)return
(1);
if(q->
data==a)
{*p_head=q->
free(q);
return(0);
{while(q->
{p=q;
q=q->
if(q->
{p->
next=q->
free(q);
return(0);
elsereturn
(1);
{NODE*my_head,*p;
/*createalinklistwithmnodes*/
intm;
charch_a,ch_b;
pleaseinputthenumberofnodesforthelink_list\nm="
m);
test1\n"
my_head=(NODE*)malloc(sizeof(NODE));
my_head=create_link_list(m);
/*Outputthelinklist*/
Thelinklistislike:
p=my_head;
while(p!
p->
data);
p=p->
}
/*insertanodewhosevalueisbbeforea*/
Pleaseinputthepositionfora\nch_a="
%c"
ch_a);
Pleaseinputthevaluethatyouwanttoinsert\nch_b="
ch_b);
insert(&
my_head,ch_a,ch_b);
Thelinklistafterinsertionislike:
/*deleteanodewhosevalueisa*/
Pleaseinputthepositionforaa="
deletenode(&
my_head,ch_a);
Thelinklistafterdeletingislike:
3.运行以下程序并分析各子函数的主要功能。
#include<
structtagNode
{
intdata;
structtagNode*pNext;
};
typedefstructtagNode*pNode;
//将结点插入到链表的适当位置,这是一个降序排列的链表
//
voidinsertList(pNodehead,//链表头结点
pNodepnode)//要插入的结点
pNod