线性表的基本操作Word格式.docx

上传人:b****3 文档编号:14856274 上传时间:2022-10-25 格式:DOCX 页数:23 大小:20.27KB
下载 相关 举报
线性表的基本操作Word格式.docx_第1页
第1页 / 共23页
线性表的基本操作Word格式.docx_第2页
第2页 / 共23页
线性表的基本操作Word格式.docx_第3页
第3页 / 共23页
线性表的基本操作Word格式.docx_第4页
第4页 / 共23页
线性表的基本操作Word格式.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

线性表的基本操作Word格式.docx

《线性表的基本操作Word格式.docx》由会员分享,可在线阅读,更多相关《线性表的基本操作Word格式.docx(23页珍藏版)》请在冰豆网上搜索。

线性表的基本操作Word格式.docx

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链式存储的线性表和运算

malloc.h>

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<

stdlib.h>

structtagNode

{

intdata;

structtagNode*pNext;

};

typedefstructtagNode*pNode;

//将结点插入到链表的适当位置,这是一个降序排列的链表

//

voidinsertList(pNodehead,//链表头结

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

当前位置:首页 > 考试认证 > 财会金融考试

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

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