数据结构Word格式.docx

上传人:b****6 文档编号:17584373 上传时间:2022-12-07 格式:DOCX 页数:15 大小:18.35KB
下载 相关 举报
数据结构Word格式.docx_第1页
第1页 / 共15页
数据结构Word格式.docx_第2页
第2页 / 共15页
数据结构Word格式.docx_第3页
第3页 / 共15页
数据结构Word格式.docx_第4页
第4页 / 共15页
数据结构Word格式.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

数据结构Word格式.docx

《数据结构Word格式.docx》由会员分享,可在线阅读,更多相关《数据结构Word格式.docx(15页珍藏版)》请在冰豆网上搜索。

数据结构Word格式.docx

//查找表尾

while(SL.Nodes[p].link!

SL.Nodes[p].link=q;

//追加

return1;

通过键盘输入一组多项式的系数和指数,以输入系数0为结束标志,并约定建立多项式链表时,总是按指数从大到小的顺序排列。

算法描述:

从键盘接受输入的系数和指数;

用尾插法建立一元多项式的链表。

polylistpolycreate()

{

polynode*head,*rear,*s;

intc,e;

head=(Polynode*)malloc(sizeof(Polynode));

rear=head;

scanf(″%d,%d″,&

c,&

e);

while(c!

=0){

s=(Polynode*)malloc(sizeof(Polynode));

s->

coef=c;

exp=e;

rear->

next=s;

rear=s;

}

next=NULL;

return(head);

}

多项式相加过程

voidpolyadd(Polylistpolya;

Polylistpolyb)

Polynode*p,*q,*pre;

*temp;

intsum;

p=polya->

next;

q=polyb->

pre=polya;

/*pre指向和多项式的尾结点*/

while(p!

=NULL&

&

q!

=NULL)

{

if(p->

exp<

q->

exp)

{pre->

next=p;

pre=pre->

next;

p=p->

elseif(p->

exp==q->

{sum=p->

coef+q->

coef;

if(sum!

=0)

{p->

coef=sum;

pre->

p=p->

else

{temp=p->

free(p);

p=temp;

temp=q->

free(q);

q=temp;

else

next=q;

q=q->

}

if(p!

=NULL)pre->

elsepre->

#include<

stdio.h>

stdlib.h>

typedefstructnode

intnDate;

structnode*pstnext;

}Node;

//链表输出

voidoutput(Node*head)

Node*p=head->

pstnext;

while(NULL!

=p)

{

printf("

%d"

p->

nDate);

p=p->

\r\n"

);

//链表建立

Node*creat()

Node*head=NULL,*p=NULL,*s=NULL;

intDate=0,cycle=1;

head=(Node*)malloc(sizeof(Node));

if(NULL==head)

分配内存失败\r\n"

returnNULL;

head->

pstnext=NULL;

p=head;

while(cycle)

请输入数据且当输入数据为0时结束输入\r\n"

scanf("

%d"

&

Date);

if(0!

=Date)

s=(Node*)malloc(sizeof(Node));

if(NULL==s)

nDate=Date;

p->

pstnext=s;

p=s;

cycle=0;

//单链表测长

voidlength(Node*head)

intj=0;

j++;

%d\r\n"

j);

//链表按值查找

voidresearch_Date(Node*head,intdate)

Node*p;

intn=1;

p=head->

=p&

date!

=p->

nDate)

++n;

if(NULL==p)

链表中没有找到该值"

}elseif(date==p->

要查找的值%d在链表中第%d个位置\r\n"

date,n);

return;

//按序号查找

voidresearch_Number(Node*head,intNum)

Node*p=head;

inti=0;

Num)

i++;

if(p==NULL)

查找位置不合法\r\n"

}elseif(i==0)

查找位置为头结点\r\n"

}elseif(i==Num)

第%d个位置数据为%d\r\n"

i,p->

//在指定元素之前插入新结点

voidinsert_1(Node*head,inti,intNewdate)

Node*pre=head,*New=NULL;

intj=0;

=pre&

j<

i-1)

{

pre=pre->

if(NULL==pre||j>

插入位置不存在\r\n"

}else

New=(Node*)malloc(sizeof(Node));

if(NULL==New)

New->

nDate=Newdate;

pstnext=pre->

pstnext=New;

//在指定元素之后插入新结点

voidinsert_2(Node*head,inti,intNewdate)

=pre->

pstnext&

i)

if(j==i)

//删除第一个结点

voiddelete_list(Node*head)

pstnext=p->

//删除指定结点

voidDelete_1(Node*head,inti3)

Node*p=head,*pre=NULL;

i3)

pre=p;

删除位置不存在\r\n"

//指定删除单链表中某个数据,并统计删除此数据的个数

intDelete_2(Node*head,intDelete_date)

intcount=0;

Node*p=head,*q;

pstnext)

q=p->

if(q->

nDate==Delete_date)

pstnext=q->

++count;

p=q;

returncount;

//链表逆置

voidReverse_list(Node*head)

Node*q,*s;

if(NULL==head->

pstnext||NULL==head->

pstnext->

q=head->

=q)

s=q->

pstnext=head->

pstnext=q;

q=s;

//单链表的连接

voidconnect_list(Node*head,Node*head_New)

Node*p=head;

pstnext=head_New->

voidmain()

intdate,num;

//待查找数据

inti3;

//指定删除元素的位置

inti1,i2,Newdate_1,Newdate_2;

//待插入的新数据

intDelete_date,k;

//待删除的数据与其个数

Node*Head=NULL;

//定义头结点

Node*Head_New=NULL;

//链表建立

Head=creat();

输出建立的单链表\r\n"

output(Head);

//单链表测长

单链表长度为\r\n"

length(Head);

//链表按值查找

请输入待查找的数据\r\n"

date);

research_Date(Head,date);

//链表按序号查找

请输入待查找序号\r\n"

num);

research_Number(Head,num);

//在指定第i1个元素之前插入新元素Newdate

在指定第i个元素之前插入新元素Newdate"

请输入i与元素且以逗号间隔\r\n"

%d,%d"

i1,&

Newdate_1);

insert_1(Head,i1,Newdate_1);

插入后新链表\r\n"

//在指定第i2个元素之后插入新元素Newdate

在指定第i个元素之后插入新元素Newdate"

i2,&

Newdate_2);

insert_2(Head,i2,Newdate_2);

//删除链表第一个结点

delete_list(Head);

输出删除后的链表\r\n"

output_list(Head);

//指定删除i3元素

删除元素的位置\r\n"

i3);

Delete_1(Head,i3);

删除后新链表\r\n"

//指定删除单链表中某个数据,并统计删除此数据的个数

请输入待删除的元素\r\n"

Delete_date);

k=Delete_2(Head,Delete_date);

删除指定元素在链表中的个数为:

"

k);

//单链表逆置

Reverse_list(Head);

逆置后输出\r\n"

//单链表的连接

建立一个新链表\r\n"

Head_New=creat();

输出新链表"

将新链表连接到原来链表的尾部并输出\r\n"

connect_list(Head,Head_New);

二叉树的非递归算法

voidPreOrder(BiNode*root)

top=-1;

//采用顺序栈,并假定不会发生上溢

while(root!

=NULL||top!

=-1)

=NULL)

cout<

<

root->

data;

s[++top]=root;

root=root->

lchild;

if(top!

=-1){

root=s[top--];

rchild;

统计二叉树中叶子结点的个数

voidCountLeaf(BiTreeT,int&

count){

if(T){

if((!

T->

lchild)&

(!

rchild))count++;

//对叶子结点计数

CountLeaf(T->

lchild,count);

rchild,count);

}//if

}//CountLeaf

求二叉树的深度(后序)

intDepth(BiTreeT){//返回二叉树的深度

if(!

T)depthval=0;

else{

depthLeft=Depth(T->

lchild);

depthRight=Depth(T->

rchild);

depthval=1+(depthLeft>

depthRight?

depthLeft:

depthRight);

returndepthval;

生成一个二叉树的结点(其数据域为item,左指针域为lptr,右指针域为rptr)

BiTNode*GetTreeNode(TElemTypeitem,BiTNode*lptr,BiTNode*rptr)

(T=newBiTNode))exit

(1);

T->

data=item;

lchild=lptr;

rchild=rptr;

returnT;

BiTNode*CopyTree(BiTNode*T){

T)returnNULL;

if(T->

lchild)newlptr=CopyTree(T->

lchild);

//复制左子树

elsenewlptr=NULL;

rchild)newrptr=CopyTree(T->

rchild);

//复制右子树

elsenewrptr=NULL;

newT=GetTreeNode(T->

data,newlptr,newrptr);

returnnewT;

}//CopyTree

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

当前位置:首页 > 高中教育 > 高中教育

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

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