数据结构编程实例文档格式.docx
《数据结构编程实例文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构编程实例文档格式.docx(10页珍藏版)》请在冰豆网上搜索。
a[i]);
}
voidprint(structsqlist*sq)/*输出顺序表*/
{inti;
%d"
\n"
voidinsert(structsqlist*sq,intpos,intx)/*顺序表插入元素*/
for(i=sq->
i>
=pos;
i--)
a[i+1]=sq->
a[i];
a[pos]=x;
length=sq->
length+1;
intdelete(structsqlist*sq,intpos)/*顺序表删除元素*/
{inti,x;
x=sq->
a[pos];
for(i=pos+1;
a[i-1]=sq->
length-1;
return(x);
main()
{intposition,x;
structsqlist*list;
structsqlistslist;
intxz=0;
list=&
slist;
while
(1)
{printf("
1.init\n"
2.creat\n"
3.insert\n"
4.delete\n"
5.locate_value\n"
6.locate_pos\n"
7.print\n"
0.exit\n"
pleaseinputyourchoice"
xz);
switch(xz)
{case1:
init(list);
break;
case2:
creat(list);
case3:
printf("
pleastinputinsetposition(pos)andvalue(x)"
%d%d"
position,&
x);
if(position<
1||position>
list->
length+1||list->
length>
=LEN)
printf("
positionerror\n"
elseinsert(list,position,x);
break;
case4:
pleastinputdeleteposition(pos)"
position);
length||list->
length==0)
else
deleteposition=%d,deletedata=%d\n"
position,delete(list,position));
;
case5:
case6:
case7:
print(list);
case0:
exit(0);
}}}
2.三种方法建立链表
#include<
stdio.h>
typedefstructnode
{intdata;
structnode*link;
}NODE;
NODE*creat1()
/*按输入数据的顺序建立链表,输入数据通过个数控制*/
{inti,data,n;
NODE*h=NULL,*p,*last=NULL;
pleaseinputthenum:
"
scanf("
n);
pleaseinput%ddatas:
n);
for(i=1;
=n;
{p=(NODE*)malloc(sizeof(NODE));
p->
data);
if(i==1)h=p;
elselast->
link=p;
last=p;
last->
link=NULL;
return(h);
NODE*creat2()
/*按输入数据的逆序建立链表,输入数据以0结束*/
NODE*h=NULL,*p;
pleaseinputdatas(0end)\n"
while(data)
p->
data=data;
if(h==NULL){h=p;
h->
else{p->
link=h;
h=p;
NODE*creat3()
/*按输入数据的大小顺序建立带头结点的链表,输入数据以0结束*/
NODE*h,*p,*q,*r;
h=(NODE*)malloc(sizeof(NODE));
if(h->
link==NULL)h->
{r=h;
q=r->
link;
while(p->
data>
q->
data&
&
q)
{r=q;
q=q->
if(q)
link=q;
r->
return(h->
link);
{NODE*h,*p;
intx;
do
{printf("
=====================\n"
1.zhengxujianlianbiao\n"
2.nixujianlianbiao\n"
3.jianliyouxulianbiao\n"
0.tuichu\n"
pleaseinputyourchosice"
switch(x)
h=creat1();
h=creat2();
h=creat3();
return;
p=h;
while(p)
%5d"
p->
p=p->
\n\n"
while(x);
3.试写出逆转线性单链表的算法
要逆转一个线性单链表,只需从头指针指向的结点开始扫描该链表,在扫描过程中改变各结点的指针(由指向后件改为指向原来的前件)即可。
Structnode/*ET位数据元素类型*/
{ETd;
structnode*next
invlst(head)
structnode**head;
structnode*p,*q,*r;
{if(*head==NULL)return;
p=*head;
q=p->
next;
next=NULL;
while(q!
=NULL)
{r=q->
q->
next=p;
p=q;
q=r;
*head=p;
return;
4.设有两个有序线性单链表,头指针分别为AH和BH。
试写出将两个有序线性单链表合并为一个头指针为CH的有序线性单链表的算法,要求去掉重复元素。
#include“stdio.h”
mglst1(ah,bh,ch)
structnodeah,bh,**ch;
{structnode*i,*j,*k,*p;
etx;
i=ah;
j=bh;
*ch=NULL;
k=NULL;
while((i!
=NULL)&
(j!
=NULL))
{if(j->
d>
=i->
d){x=i->
d;
i=i->
else{x=j->
j=j->
if(*ch==NULL)
{p=(structnode*)malloc(sizeof(structnode));
d=x;
*ch=p;
k=p;
elseif(d!
=k->
d)
k->
}}}
if(j==NULL)
while(i!
=NULtructL)
{x=i->
}}
else
while(j!
{x=j->
if(k!
=NULL)k->
5.试编写在二叉排序树中插入一个元素的算法。
#include“stdlib.h”
structbtnode
structbtnode*lchild;
structbtnode*rchild;
structbtnode*insort(bt,b)
structbtnode*bt;
ETb;
{structbtnode*p,*q;
p=(structbtnode*)malloc(sizeof(structbtnode));
d=b;
lchild=NULL;
rchild=NULL;
q=bt;
if(q==NULL)bt=p;
{while((q->
lchild!
=p)&
(q->
rchild!
=p))
{if(b<
{if(q->
=NULL)q=q->
lchild;
elseq->
lchild=p;
rchild;
rchild=p;
return(bt);
6.先序(递归)建立二叉树并中序(递归)输出。
typedefstructbitree
{chardata;
structbitree*lchild,*rchild;
}BTREE;
BTREE*creatree()
{BTREE*t;
charch;
%c"
ch);
if(ch=='
'
)t=NULL;
{t=(BTREE*)malloc(sizeof(BTREE));
t->
data=ch;
lchild=creatree();
rchild=creatree();
return(t);
voidinorder(BTREE*bt)
{if(bt!
{inorder(bt->
lchild);
%c"
bt->
inorder(bt->
rchild);
main()
{BTREE*root;
root=creatree();
inorder(root);
Welcome!
!
欢迎您的下载,
资料仅供参考!