数据结构书中源程序3134文档格式.docx

上传人:b****5 文档编号:21163066 上传时间:2023-01-28 格式:DOCX 页数:12 大小:16.65KB
下载 相关 举报
数据结构书中源程序3134文档格式.docx_第1页
第1页 / 共12页
数据结构书中源程序3134文档格式.docx_第2页
第2页 / 共12页
数据结构书中源程序3134文档格式.docx_第3页
第3页 / 共12页
数据结构书中源程序3134文档格式.docx_第4页
第4页 / 共12页
数据结构书中源程序3134文档格式.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

数据结构书中源程序3134文档格式.docx

《数据结构书中源程序3134文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构书中源程序3134文档格式.docx(12页珍藏版)》请在冰豆网上搜索。

数据结构书中源程序3134文档格式.docx

else

{s->

top++;

s->

datas[s->

top]=e;

}

}//Push

intPop(SqStack*s)

{//若栈空则返回NULL;

否则返回栈顶元素,栈顶指示器递减。

inte;

if(EmptyStack(s))

Underflow\n"

return(0);

}

else

{e=s->

top];

top--;

returne;

}//Pop

main()

{

SqStack*s;

inti,e;

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

InitStack(s);

printf("

请按题目要求输入5个整数:

"

for(i=0;

i<

5;

i++)

{

scanf("

%d"

&

e);

Push(s,e);

栈中所有元素出栈的序列为:

while(!

(EmptyStack(s)))

e=Pop(s);

%4d"

e);

\n"

/*

程序运行结果:

136910

109631

Pressanykeytocontinue

*/

 

3.2

typedefstructSNode

{intdata;

//数据元素的类型为整型

structSNode*next;

}SNode;

SNode*InitStack(SNode*top)

{top=NULL;

return(top);

intEmptyStack(SNode*top)

if(top==NULL)return1;

else

return0;

SNode*PushStack(SNode*top,inte)

{//在链栈的栈顶插入一个值为e的结点

SNode*t;

t=(SNode*)malloc(sizeof(SNode));

t->

data=e;

next=top;

top=t;

returntop;

}//PushStack

SNode*PopStack(SNode*top)

{//将链栈的栈顶元素删除

if(top==NULL)//空栈

//下溢

return0;

t=top;

top=top->

next;

free(t);

returntop;

}//PopStack

intGetTop(SNode*top)//数据元素的类型为整型

{//若栈空则返回0;

否则返回栈顶元素,栈顶指示器不变。

if(EmptyStack(top))

{printf("

Stackisempty.\n"

{e=top->

data;

}//GetTop

voidPRINT(SNode*top)

inte;

while(top!

=NULL)

{e=top->

printf("

top=top->

SNode*top;

intn,i,e;

top=(SNode*)malloc(sizeof(SNode));

top=InitStack(top);

请输入链栈中元素的个数n:

n);

请依次输入%d个从栈底到栈顶的元素的值:

n);

n;

scanf("

top=PushStack(top,e);

该栈从栈顶到栈低的元素分别为:

PRINT(top);

请输入待插入栈顶的元素e:

top=PushStack(top,e);

运行结果:

5

请依次输入5个从栈底到栈顶的元素的值:

13579

97531

25

2597531

3.3

/*问题描述:

已知一个循环队列Q中有5个元素:

a,b,c,d,e,a为队头,e为队尾。

编写程序,输出该队列,

然后让4个字母入队,字母的值由键盘输入,输出该队列,

再让4个字母出队后输出该队列*/

#defineMAXSIZE20

{chardatas[MAXSIZE];

intfront,rear;

}SqQueue;

voidInitQueue(SqQueue*Q)

{Q->

front=Q->

rear=-1;

intEmptyQueue_C(SqQueue*Q)

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

if(Q->

rear==Q->

front)return1;

}//EmptyQueue_C

charGetQueue_C(SqQueue*Q)

{//若队列不为空,则返回队首元素,否则返回NULL

if(EmptyQueue_C(Q))

{printf("

Queueisempty\n"

{e=Q->

datas[(Q->

front+1)%MAXSIZE];

}//GetQueue_C

intEnQueue_C(SqQueue*Q,chare)

{//将元素e插入到队列中,作为新的队尾。

操作成功返回1,否则返回0

if(Q->

front==(Q->

rear+1)%MAXSIZE)//队满

{printf("

Queueisfull.\n"

{Q->

rear=(Q->

rear+1)%MAXSIZE;

Q->

datas[Q->

rear]=e;

return1;

}//EnQueue_C

intDeQueue_C(SqQueue*Q)

{//删除队头元素,若操作成功返回1,否则返回0

if(EmptyQueue_C(Q))

Queueisempty.\n"

front=(Q->

front+1)%MAXSIZE;

}

}//DeQueue_C

voidPRINT(SqQueue*Q)

inti;

front!

=Q->

rear)

当前循环队列中从头到尾的元素为:

i=Q->

front;

while(i!

{

i=(i+1)%MAXSIZE;

printf("

%c"

Q->

datas[i]);

}

当前循环队列为空!

putchar('

\n'

SqQueue*Q;

charch;

Q=(SqQueue*)malloc(sizeof(SqQueue));

InitQueue(Q);

按题目要求输入5个字母:

%c"

ch);

EnQueue_C(Q,ch);

getchar();

PRINT(Q);

请输入4个待入队的元素:

4;

//4个元素出队

DeQueue_C(Q);

4个元素出队\n"

abcde

abcde

fghi

abcdefghi

4个元素出队

efghi

3.4

已知一个链队列中的元素为:

a,b,c,d,e,

编写程序,让所有元素依次出队,输出出队序列*/

#include<

typedefstructQNode

{chardata;

structQNode*next;

}QNode;

//链队列结点的类型

{QNode*front;

//队头指针

QNode*rear;

//队尾指针

}LinkQueue;

voidInitLinkQueue(LinkQueue*Q)

{

QNode*t;

t=(QNode*)malloc(sizeof(QNode));

next=NULL;

Q->

front=t;

rear=t;

}//InitLinkQueue

intEmptyLinkQueue(LinkQueue*Q)

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

front==Q->

rear)return1;

}//EmptyLinkQueue

charGetLinkQueue(LinkQueue*Q)

{//返回队首元素

chare;

if(EmptyLinkQueue(Q))

LinkQueueisempty.\n"

return(0);

front->

next->

}//GetLinkQueue

voidEnLinkQueue(LinkQueue*Q,chare)

{//在队尾插入一个元素e,使之成为新的队尾元素

rear->

next=t;

}//EnLinkQueue

voidDeLinkQueue(LinkQueue*Q)

{QNode*p;

{p=Q->

next=p->

if(p->

next==NULL)Q->

rear=Q->

free(p);

}//DeLinkQueue

LinkQueue*Q;

Q=(LinkQueue*)malloc(sizeof(LinkQueue));

InitLinkQueue(Q);

按题目要求,依次输入链队列中的5个元素:

EnLinkQueue(Q,ch);

链队列的出队序列为:

ch=GetLinkQueue(Q);

while(ch)

ch);

DeLinkQueue(Q);

abcdeLinkQueueisempty.

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

当前位置:首页 > 农林牧渔 > 水产渔业

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

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