数据结构.docx

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

数据结构.docx

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

数据结构.docx

数据结构

1、二叉树的顺序存储

#include"stdio.h"

#include"string.h"

typedefstructsqBiTree

{

intn;

charbt[80];

}sqBiTree;

sqBiTreesqbta;

voidcreateSqBiTree(sqBiTree&sqbt,intm)

{

sqbt.n=m;

inti;

intj;

charch;

for(i=0;i<80;i++)

{

sqbt.bt[i]='#';

}

for(i=0;i

{

printf("请输结点编号和值:

\n");

scanf("%d",&j);

ch=getchar();

sqbt.bt[j]=ch;

}

}

voidoutputsqbitree(sqBiTree&sqbt)

{

inti;

printf("二叉树的结点总数是%d\n",sqbt.n);

for(i=0;i<80;i++)

{

if(sqbt.bt[i]!

='#')

printf("位置:

%d,结点值:

%c,",i,sqbt.bt[i]);

}

printf("\n");

}

intexistnode(sqBiTree&sqbt,inti)

{//判断是否存在编号为i的结点

if(sqbt.bt[i]=='#')

{

printf("不存在!

\n");

return0;

}

else

{

printf("存在!

\n");

return1;

}

}

/*

charparent(inti)

{//求编号为i的结点parent

}

charleftchild(inti)

{//编号为i的结点leftchild

 

}

charrightchild(inti)

{//编号为i的结点rightchild

 

}

*/

voidmain()

{

createSqBiTree(sqbta,5);

outputsqbitree(sqbta);

existnode(sqbta,4);

}

 

2、带头结点的单向链表

#include"stdio.h"

#include"stdlib.h"

typedefstructLnode

{

intdata;

structLnode*next;

}Lnode,*Linklist;

intInit_lnklst(Linklist&L)

{//初始化

L=(Lnode*)malloc(1*sizeof(Lnode));

if(L!

=NULL)

{

printf("初始化成功!

\n");

L->next=NULL;

return1;

}

else

{

printf("初始化失败!

\n");

return0;

}

}

intLnkinsertrear(Linklist&L,inte)

{//在当前链表的末尾插入新元素为e的结点

Linklists,p;

p=L;

while(p->next!

=NULL)

{

p=p->next;

}

s=(Lnode*)malloc(1*sizeof(Lnode));

if(s!

=NULL)

{

s->data=e;

s->next=NULL;

p->next=s;

printf("插入成功!

\n");

return1;

}

else

{

return0;

}

}

voidLnkinserthead(Linklist&L,inte)

{//插入的结点作为链表的第一个结点

Linklistp,s;

p=L;

s=(Lnode*)malloc(1*sizeof(Lnode));

s->data=e;

s->next=p->next;

p->next=s;

}

voidoutputlnklst(Linklist&L)

{//输出链表L中结点的数据元素值

Linklistp;

p=L;

printf("链表中的数据元素值如下:

\n");

while(p->next!

=NULL)

{

p=p->next;

printf("%d,",p->data);

}

}

intlnklength(Linklist&L)

{//求链表长度

intlen=0;

Linklistp;

p=L;

while(p->next!

=NULL)

{

len++;

p=p->next;

}

returnlen;

}

intlnkinsert(Linklist&L,inti,inte)

{//在第i个结点之前插入元素值为e的结点

intj=0;

Linklistp,s;

if(i<1||i>lnklength(L))

{

printf("插入位置不合法!

\n");

return0;

}

else

{

p=L;

while(j

{

p=p->next;

j++;

}

s=(Lnode*)malloc(1*sizeof(Lnode));

s->data=e;

s->next=p->next;

p->next=s;

printf("插入成功!

\n");

return1;

}

}

intLnkdelete(Linklist&L,inti,int&e)

{//删除链表中的第i个数据元素,被删除的元素暂时存储于e中

intj=0;

Linklistp,q;

if(i<1||i>lnklength(L))

{

printf("删除位置不合法!

\n");

return0;

}

else

{

p=L;

while(j

{

p=p->next;

j++;

}

q=p->next;

e=q->data;

p->next=q->next;

free(q);

printf("删除成功!

\n");

return1;

}

}

voidmain()

{

LinklistLa;

inti;

intlen;

intx;

Init_lnklst(La);

/*

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

{//调用尾插法建立单链表

Lnkinsertrear(La,i*10);

}

*/

for(i=10;i>=1;i--)

{//调用头插法建立单链表

Lnkinserthead(La,i*10);

}

outputlnklst(La);

printf("\n");

len=lnklength(La);

printf("len=%d\n",len);

lnkinsert(La,3,25);

outputlnklst(La);

printf("\n");

Lnkdelete(La,4,x);

printf("被删除的元素为:

%d\n",x);

outputlnklst(La);

printf("\n");

}

 

3、二叉链表的链式存储

 

#include"stdio.h"

#include"string.h"

#include"stdlib.h"

#include"windows.h"

typedefstructBTNode

{//二叉链表数据类型构造

chardata;

structBTNode*Lchild,*Rchild;

}BTNode,*BTree;/*BTreeisaddressofroot*/

BTreePre_Create_BT(BTreeBT)

{//返回指针的函数--先序方式创建二叉链表

charch;

ch=getchar();

if(ch=='#')returnNULL;/*makeanemptytree!

*/

else

{

BT=(BTNode*)malloc(sizeof(BTNode));

/*getaspaceofnode*/

BT->data=ch;

BT->Lchild=Pre_Create_BT(BT);/*makelefttree*/

BT->Rchild=Pre_Create_BT(BT);/*makerighttree*/

returnBT;

}

}

voidVisit(BTreeBT)

{//访问结点的数据元素值

if(BT)printf("%c",BT->data);

}

voidPreOrder(BTreeBT)

{//先序遍历二叉树

if(BT)

{

Visit(BT);

PreOrder(BT->Lchild);

PreOrder(BT->Rchild);

}

}

voidInOrder(BTreeBT)

{//中序遍历二叉树

if(BT)

{

InOrder(BT->Lchild);

Visit(BT);

InOrder(BT->Rchild);

}

}

voidPostOrder(BTreeBT)

{//后续遍历二叉树

if(BT)

{

PostOrder(BT->Lchild);

PostOrder(BT->Rchild);

Visit(BT);

}

}

voidmain()

{

BTreepbt;

system("cls");//清屏

printf("inputtreenodeelem(elem-->left-->right):

\n");

pbt=Pre_Create_BT(pbt);

printf("preorderbitree,outputelemis:

\n");

PreOrder(pbt);

printf("\ninorderbitree,outputelemis:

\n");

InOrder(pbt);

printf("\npostorderbitree,outputelemis:

\n");

PostOrder(pbt);

printf("\npbtnodedata:

%c\n",pbt->data);

getchar();

getchar();

}

 

4、顺序表的操作

 

#include"stdio.h"

#include"stdlib.h"

//线性表的顺序存储结构,简称顺序表

//顺序表数据类型构造如下:

#defineLIST_INIT_SIZE100//线性表的初始空间大小

#defineLISTINCREMENT10//增量

 

typedefstruct

{

int*elem;//设线性表中数据元素类型为整型,指针变量elem就是基地址(一段连续存储空间首地址)

intlength;//顺序表的长度

intlistsize;//当前存储空间的大小

}SqList;

//操作1--顺序表的初始化

//功能:

向系统申请一段连续的存储空间,类型是elemtype型,用elem存储基地址

//顺序表的长度设置为0,初始大小设置为LIST_INIT_SIZE

 

//形参类别:

1、普通变量作形参2、指针变量作形参3、引用作形参

intinitSqList(SqList&L)

{

L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));

if(L.elem!

=NULL)

{

L.length=0;

L.listsize=100;

printf("顺序表的初始化成功!

\n");

return1;

}

else

{

printf("顺序表的初始化不成功!

\n");

return0;

}

}

//操作2:

创建具有n个元素的顺序表

intcreatesqlist(SqList&L,intn)

{//创建具有n个元素的顺序表

//将n个元素依次存入存储空间中

inti;

if(n>L.listsize)

{

printf("无法创建顺序表,指定的n太大!

\n");

return0;

}

else

{

printf("input%dnum:

\n",n);

for(i=0;i

scanf("%d",&L.elem[i]);

L.length=n;

return1;

}

}

//操作3:

输出顺序表中的数据元素

voidoutputsqlst(SqList&L)

{

inti;

printf("顺序表中的数据元素如下:

\n");

for(i=0;i

printf("%d",L.elem[i]);

printf("\n");

}

//操作算法4:

删除顺序表中的第i个元素

intdeletesqlst(SqList&L,inti,int&e)

{

intj;

if(i<1||i>L.length)

{

printf("删除位置不合法!

\n");

return0;

}

else

{

e=L.elem[i-1];

for(j=i;j

{

L.elem[j-1]=L.elem[j];

}

L.length--;

printf("删除成功!

\n");

return1;

}

}

//操作算法4:

第i个元素之前插入新元素e

intsqlstinsert(SqList&L,inti,inte)

{

intj;

int*newbase;

if(i<1||i>L.length)

{

printf("删除位置不合法!

\n");

return0;

}

else

{

if(L.length==L.listsize)

{

newbase=(int*)realloc(L.elem,(L.listsize+10)*sizeof(int));

if(newbase==NULL)

{

printf("顺序表满了,而动态增加存储空间失败,不能插入新元素!

\n");

return0;

}

else

{

L.elem=newbase;

L.listsize+=10;

for(j=L.length-1;j>=i;j--)

{

L.elem[j+1]=L.elem[j];

}

L.elem[i-1]=e;

L.length++;

printf("插入成功!

\n");

return1;

}

}

else

{

for(j=L.length-1;j>=i;j--)

{

L.elem[j]=L.elem[j-1];

}

L.elem[i-1]=e;

L.length++;

printf("插入成功!

\n");

return1;

}

}

}

voidmain()

{

SqListLa;

inte;

initSqList(La);

createsqlist(La,5);

outputsqlst(La);

deletesqlst(La,3,e);

outputsqlst(La);

sqlstinsert(La,1,5);

outputsqlst(La);

}

5、顺序栈操作

#include"stdio.h"

#include"stdlib.h"

//顺序栈数据类型构造

typedefstruct

{//设栈中数据元素类型为整型

int*base;//栈底指针

int*top;//栈顶指针

intstacksize;//栈当前存储空间大小

}sqstack;

//操作1:

栈的初始化

intinitsqstack(sqstack&s)

{//功能:

向系统申请一段连续的存储空间,当做栈使用

s.base=(int*)malloc(100*sizeof(int));

if(s.base!

=NULL)

{

printf("栈初始化成功!

\n");

s.top=s.base;

s.stacksize=100;

return1;

}

else

{

printf("栈初始化失败!

\n");

return0;

}

}

//操作2:

数据元素压入堆栈

intpush(sqstack&s,inte)

{

if(s.top-s.base==s.stacksize)

{

s.base=(int*)realloc(s.base,(100+10)*sizeof(int));

if(s.base==NULL)

{

printf("再次追加存储空间失败!

\n");

return0;

}

else

{

printf("再次追加存储空间成功!

\n");

s.top=s.base+s.stacksize;

s.stacksize=110;

*s.top=e;

s.top++;

printf("数据元素入栈成功!

\n");

return1;

}

}

else

{

*s.top=e;

s.top++;

printf("数据元素入栈成功!

\n");

return1;

}

}

//操作3:

弹出堆栈--数据元素出栈

intpop(sqstack&s,int&e)

{

if(s.top==s.base)

{

printf("栈为空,不能弹出数据元素!

\n");

return0;

}

else

{

s.top--;

e=*s.top;

return1;

}

}

//操作4:

求栈的长度

intsqstacklength(sqstack&s)

{

returns.top-s.base;

}

//操作5:

判断栈是否为空

intemptysqstack(sqstack&s)

{//若为空栈,则返回1,否则返回0

if(s.top==s.base)

{

printf("空栈!

\n");

return1;

}

else

{

printf("非空栈!

\n");

return0;

}

}

voidconversion(intn,intm)

{

intx;

sqstacksb;

initsqstack(sb);

while(n!

=0)

{

push(sb,n%m);

n=n/m;

}

while(sb.top!

=sb.base)

{

pop(sb,x);

switch(x)

{

case10:

printf("A");break;

case11:

printf("B");break;

case12:

printf("C");break;

case13:

printf("D");break;

case14:

printf("E");break;

case15:

printf("F");break;

default:

printf("%d",x);

}

}

printf("\n");

}

voidmain()

{

inti;

intx;

sqstacksa;

initsqstack(sa);

push(sa,10);

for(i=0;i<105;i++)

{

push(sa,10*(i+1));

}

if(sa.top-sa.base==sa.stacksize)

printf("栈满!

\n");

pop(sa,x);

printf("x=%d\n",x);

conversion(255,8);

}

6、循环队列

#include"stdio.h"

#include"stdlib.h"

//构造循环队列的数据结构

typedefstruct

{

int*base;

intfront;

intrear;

}sqQueue;

//算法程序1:

循环队列的初始化

//用户自定义函数的构成:

(1)函数首部:

函数的返回值类型,

函数名,形参列表

(2)函数体

intinitsqQueue(sqQueue&q)

{

q.base=(int*)malloc(100*sizeof(int));

if(q.base!

=NULL)

{

printf("循环队列初始化成功!

\n");

q.front=q.rear=0;

return1;

}

else

{

printf("初始化失败!

\n");

return0;

}

}

//数据元素e进入队列q

intensqQueue(sqQueue&q,inte)

{

if((q.rear+1)%100==q.front)

{

printf("循环队列满,数据元素不能进入队列!

\n");

return0;

}

else

{

q.base[q.rear]=e;

q.rear=(q.rear+1)%100;

printf("数据元素入队列成功!

\n");

return1;

}

}

//数据元素出队列

intdesqQueue(sqQueue&q,int&e)

{

if(q.front==q.rear)

{

printf("循环队列为空,不能出队列!

\n");

return0;

}

else

{

e=q.base[q.front];

q.front=(q.front+

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

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

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

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