数据结构代码.docx

上传人:b****5 文档编号:6149939 上传时间:2023-01-04 格式:DOCX 页数:57 大小:46.15KB
下载 相关 举报
数据结构代码.docx_第1页
第1页 / 共57页
数据结构代码.docx_第2页
第2页 / 共57页
数据结构代码.docx_第3页
第3页 / 共57页
数据结构代码.docx_第4页
第4页 / 共57页
数据结构代码.docx_第5页
第5页 / 共57页
点击查看更多>>
下载资源
资源描述

数据结构代码.docx

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

数据结构代码.docx

数据结构代码

//all1.cpp:

Definestheentrypointfortheconsoleapplication.

//带有头结点的链表综合

#include"stdafx.h"

#include"stdio.h"

#include"stdlib.h"

#include

typedefstructLnode

{

chardata[10];

structLnode*next;

}listline;

/*定义头接点(head),前驱接点(r),当前接点(p),零时接点(temp)*/

listline*head,*r,*p,*temp;

voidmenu()

{

puts("");

puts("=============================");

printf("1.创建新链表------------[1]\n");

printf("2.插入新元素------------[2]\n");

printf("3.删除旧元素------------[3]\n");

printf("4.查找旧元素------------[4]\n");

printf("5.倒置原链表------------[5]\n");

printf("6.显示所有--------------[6]\n");

printf("7.退出------------------[7]\n");

puts("=============================");

puts("");

}

voidcreater(inth)

{

inti;

if((head=(listline*)malloc(sizeof(listline)))==NULL)

{

printf("\n不能创建链表");

exit

(1);

}

r=head;

for(i=1;i<=h;i++)

{

if((p=(listline*)malloc(sizeof(listline)))==NULL)

{

printf("\n不能创建链表");

exit

(1);

}

p->next=NULL;

r->next=p;

printf("输入第%d个元素:

",i);

scanf("%s",&p->data);

r=p;

}

}

voidprint(void)

{

temp=head->next;

printf("\n");

while(temp!

=NULL)

{

printf("%s",temp->data);

temp=temp->next;

}

}

voidturnlist()

{/*倒置链表函数*/

r=head->next;

p=r->next;

r->next=NULL;

while(p!

=NULL)

{

temp=p->next;

p->next=r;

r=p;

p=temp;

}

head->next=r;

}

voidinsert(intn,charnewdata[10])

{

intj;

p=head->next;

j=0;

while(p&&j

{

p=p->next;

j++;

}

r=(listline*)malloc(sizeof(listline));

strcpy(r->data,newdata);

r->next=p->next;

p->next=r;

}

voiddel_list(intn)

{

intj;

chare[10];

p=head->next;

j=0;

while(p&&j

{

p=p->next;

j++;

}

r=p->next;

p->next=r->next;

strcpy(e,r->data);

free(r);

}

voidmain(void)

{

intselect,length,n;

charname1[10];

head=NULL;

while

(1)

{

menu();

scanf("%d",&select);

switch(select)

{

case1:

printf("\n请输入你要建立的链表的长度:

");

scanf("%d",&length);

creater(length);

printf("\n以下为你链表中的所有元素:

");

puts("");

print();

puts("");

printf("\n链表已创建,返回\n");

getchar();

puts("");

break;

case2:

if(head==NULL)

{

printf("\n请先建立链表,按任意键返回\n");

getchar();

break;

}

printf("请输入你要在第几个元素后面插入新元素:

\n");

scanf("%s",&n);

printf("请输入你要插入的新元素:

\n");

scanf("%s",&name1);

insert(n,name1);

printf("\n插入成功,以下为插入后的链表:

");

print();

puts("");

break;

case3:

if(head==NULL)

{

printf("\n请先建立链表,按任意键返回\n");

getchar();

break;

}

printf("请输入你要删除第几个元素:

\n");

scanf("%s",&n);

del_list(n);

printf("\n删除成功,以下为插入后的链表:

");

print();

puts("");

break;

case5:

if(head==NULL)

{

printf("\n\n出错,返回\n");

break;

}

else

{

printf("\n以下为原链表:

");

print();

printf("\n\n以下为倒置后的链表:

");

turnlist();

print();

printf("\n\n倒置成功,按任意键返回\n");

getchar();

break;

}

case6:

printf("\n以下为你链表中的所有元素:

");

puts("");

print();

puts("");

break;

case7:

exit(0);

puts("");

default:

printf("\n输入错误,请重新选择,返回\n");

getchar();

}

}

}

//text1.cpp:

Definestheentrypointfortheconsoleapplication.

//

#include"stdafx.h"

#include"stdio.h"

#include"stdlib.h"

#include"iostream"

usingnamespacestd;

#definestack_init_size100

#definestackincreament10

structSqstack

{

char*top;

char*base;

intstacksize;

};

voidinitstack(Sqstack&S)

{

S.base=(char*)malloc(stack_init_size*sizeof(char));

if(!

S.base)

puts("error");

S.top=S.base;

S.stacksize=stack_init_size;

}

voidpush(Sqstack&S,chare)

{

if(S.top-S.base>=S.stacksize)

{

S.base=(char*)realloc(S.base,(S.stacksize+stackincreament)*sizeof(char));

if(!

S.base)

puts("error");

S.top=S.base+S.stacksize;

S.stacksize+=stackincreament;

}

*S.top++=e;

}

voidpop(Sqstack&S,char&e)

{

if(S.top==S.base)

puts("error");

e=*--S.top;

}

intgettop(SqstackS,char&e)

{

if(S.top==S.base)

puts("error");

e=*(S.top-1);

return(e);

}

intempty(Sqstack&S)

{

if(S.top==S.base)

return1;

else

return0;

}

intmain()

{

structSqstackS;

charstr[20]={0};

initstack(S);

puts("输入一串表达式:

");

gets(str);

inti=0,flag1=0,flag2=0;

chare1,e2;

inta=-1;

while(a!

=1)

{

switch(str[i])

{

case'#':

a++;break;

case'(':

push(S,'(');break;

case'[':

push(S,'[');break;

case'{':

push(S,'{');break;

case')':

if(gettop(S,e1)=='(')

pop(S,e2);

else

push(S,')');

flag1=1;

break;

case']':

if(gettop(S,e1)=='[')

pop(S,e2);

else

push(S,']');

flag1=1;

break;

case'}':

if(gettop(S,e1)=='{')

pop(S,e2);

else

push(S,'}');

flag1=1;

break;

default:

break;

}

i++;

}

if(empty(S))

puts("表达式合法");

else

puts("表达式不合法");

}

//text1.cpp:

Definestheentrypointfortheconsoleapplication.

//

#include"stdafx.h"

#include"stdio.h"

#include"stdlib.h"

#include"iostream"

usingnamespacestd;

#definestack_init_size100

#definestackincreament10

structSqstack

{

int*top;

int*base;

intstacksize;

};

voidinitstack(Sqstack&S)

{

S.base=(int*)malloc(stack_init_size*sizeof(int));

if(!

S.base)

puts("error");

S.top=S.base;

S.stacksize=stack_init_size;

}

voidpush(Sqstack&S,inte)

{

if(S.top-S.base>=S.stacksize)

{

S.base=(int*)realloc(S.base,(S.stacksize+stackincreament)*sizeof(int));

if(!

S.base)

puts("error");

S.top=S.base+S.stacksize;

S.stacksize+=stackincreament;

}

*S.top++=e;

}

voidpop(Sqstack&S,int&e)

{

if(S.top==S.base)

puts("error");

e=*--S.top;

}

voidgettop(SqstackS,int&e)

{

if(S.top==S.base)

puts("error");

e=*(S.top-1);

}

intempty(Sqstack&S)

{

if(S.top==S.base)

return1;

else

return0;

}

intmain()

{

structSqstackS;

initstack(S);

inta,b=0;

puts("pleaseenteranumberyouwanttochange");

scanf("%d",&a);

puts("enteraHexadecimalnumbersyouwant");

scanf("%d",&n)

while(a)

{

push(S,a%n);

a=a/n;

}

while(!

empty(S))//用while输入所有的数据

{

pop(S,b);

cout<

}

return1;

}

//text1.cpp:

Definestheentrypointfortheconsoleapplication.

//

#include"stdafx.h"

#include"stdio.h"

#include"stdlib.h"

#include"iostream"

usingnamespacestd;

typedefstructQnode

{

chardata;

structQnode*next;

}Qnode,*QueuePtr;

typedefstruct

{

QueuePtrfront;

QueuePtrrear;

}LinkQueue;

charinitQueue(LinkQueue&Q)

{

Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode));

if(!

Q.front)

printf("error/n");

Q.front->next=NULL;

return1;

}

charDestoryQueue(LinkQueue&Q)

{

while(Q.front)

{

Q.rear=Q.front->next;

free(Q.front);

Q.front=Q.rear;

}

return1;

}

charEnQueue(LinkQueue&Q,chare)

{

p=(QueuePtr)malloc(sizeof(Qnode));

if(!

p)

puts("error");

p->data=e;

p->next=NULL;

Q.rear->next=p;

Q.rear=p

return1;

}

charDeQueue(LinkQueue&Q,char&e)

{

if(Q.front==Q.rear)

puts("error");

p=Q.front->next;

e=p->data;

Q.front->next=p->next;

if(Q.rear==p)

Q.rear=Q.front;

free(p);

return1;

}

voidmian(void)

{

}

二叉树

1.对题目要有需求分析

在需求分析中,将题目中要求的功能进行叙述分析,并且设计解决此问题的数据存储结构,设计或叙述解决此问题的算法。

给出实现功能的一组或多组测试数据,程序调试后,将按照此测试数据进行测试的结果列出来。

如果程序不能正常运行,写出实现此算法中遇到的问题和改进方法;

2.对题目要有相应的源程序

源程序要按照写程序的规则来编写。

要结构清晰,重点函数的重点变量,重点功能部分要加上清晰的程序注释。

(注释量占总代码的四分之一)

程序能够运行,要有基本的容错功能。

尽量避免出现操作错误时出现死循环;

3.最后提供的主程序可以象一个应用系统一样有主窗口,通过主菜单和分级菜单调用课程设计中要求完成的各个功能模块,调用后可以返回到主菜单,继续选择其他功能进行其他功能的选择。

二叉树的建立与遍历

[问题描述]

  建立一棵二叉树,并对其进行遍历(先序、中序、后序),打印输出遍历结果。

[基本要求]

  从键盘接受输入,以二叉链表作为存储结构,建立二叉树,并对其进行遍历(先序、中序、后序),将遍历结果打印输出。

以下是我的数据结构实验的作业:

肯定好用,里面还包括了统计树的深度和叶子数!

记住每次做完一个遍历还要重新输入你的树哦!

#include"stdio.h"

#include"string.h"

#defineNULL0

typedefstructBiTNode{

chardata;

structBiTNode*lchild,*rchild;

}BiTNode,*BiTree;

BiTreeCreate(BiTreeT){

charch;

ch=getchar();

if(ch=='#')

T=NULL;

else{

if(!

(T=(BiTNode*)malloc(sizeof(BiTNode))))

printf("Error!

");

T->data=ch;

T->lchild=Create(T->lchild);

T->rchild=Create(T->rchild);

}

returnT;

}

voidPreorder(BiTreeT){

if(T){

printf("%c",T->data);

Preorder(T->lchild);

Preorder(T->rchild);

}

}

intSumleaf(BiTreeT){

intsum=0,m,n;

if(T){

if((!

T->lchild)&&(!

T->rchild))

sum++;

m=Sumleaf(T->lchild);

sum+=m;

n=Sumleaf(T->rchild);

sum+=n;

}

returnsum;

}

voidzhongxu(BiTreeT){

if(T){

zhongxu(T->lchild);

printf("%c",T->data);

zhongxu(T->rchild);

}

}

voidhouxu(BiTreeT){

if(T){

houxu(T->lchild);

houxu(T->rchild);

printf("%c",T->data);

}

}

intDepth(BiTreeT){

intdep=0,depl,depr;

if(!

T)dep=0;

else{

depl=Depth(T->lchild);

depr=Depth(T->rchild);

dep=1+(depl>depr?

depl:

depr);

}

returndep;

}

main(){

BiTreeT;

intsum,dep;

T=Create(T);

Preorder(T);

printf("\n");

zhongxu(T);

printf("\n");

houxu(T);

printf("\n");

sum=Sumleaf(T);

printf("%d",sum);

dep=Depth(T);

printf("\n%d",dep);

}

二叉树2

/*二叉树的建立与遍历*/

#include

#include

typedefintEtype;

typedefstructBiTNode/*树结点结构*/

{Etypedata;

structBiTNode*lch,*rch;

}BiTNode;

/*函数原形声明*/

BiTNode*creat_bt1();

BiTNode*creat_bt2()

voidinorder(BiTNode*p);

voidnumb(BiTNode*p);

BiTNode*t;intn,n0,n1,n2,;

/*主函数*/

main()

{charch;intk;

do{printf("\n\n\n");

printf("\n\n1.建立二叉树方法1");

printf("\n\n2.建立二叉树方法2");

printf("\n\n3.中序递归遍历二叉树");

printf("\n\n4.计算树中结点个数");

printf("\n\n5.结束程序运行");

printf("\n======================================");

printf("\n请输入您的选择(1,2,3,4,5,6)");scanf("%d",&k);

switch(k)

{case1:

t=creat_bt1();break;/*调用性质5建立二叉树算法*/

case2:

t=creat_bt2();break;/*调用递归建立二叉树算法*/

case3:

{inorder(t);/*调用中序遍历*/

printf("\n\n打回车键,继续。

“);ch=getch();

}break;

case4:

{n=0;n0=0;n1=0;n2

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

当前位置:首页 > 求职职场 > 简历

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

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