最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言中用棧+隊(duì)列實(shí)現(xiàn)隊(duì)列中的元素逆置

 更新時(shí)間:2022年02月11日 09:55:15   作者:MyDreamingCode  
這篇文章主要介紹了C語言中用利用棧和隊(duì)列實(shí)現(xiàn)隊(duì)列中的元素逆置的相關(guān)資料,對正在學(xué)習(xí)的小伙伴有一定的參考價(jià)值,需要的可以參考一下,希望對你有所幫助

下面舉例代碼:

提到的Q是一個(gè)隊(duì)列,S是一個(gè)空棧,實(shí)現(xiàn)將隊(duì)列中的元素逆置的算法

#include<stdio.h>
#define MaxSize 10
typedef int ElemType;
typedef struct{
?? ?ElemType data[MaxSize];
?? ?int front,rear;
}Queue;
typedef struct{
?? ?ElemType data[MaxSize];
?? ?int top;
}SqStack;
?
void InitStack(SqStack &S) //初始化棧
{
?? ?S.top = -1;
}
?
bool EmptyStack(SqStack S) //判斷???
{
?? ?if(S.top == -1)
?? ??? ?return true;
?? ?else
?? ??? ?return false;
}
?
bool OverflowStack(SqStack S) //判斷棧是否滿
{
?? ?if(S.top == MaxSize-1)
?? ??? ?return true;
?? ?else
?? ??? ?return false;
}
?
bool Push(SqStack &S,ElemType x) //進(jìn)棧
{
?? ?if(OverflowStack(S))
?? ??? ?return false;
?? ?S.data[++S.top] = x;
?? ?return true;
}
?
bool Pop(SqStack &S,ElemType &x) //出棧
{
?? ?if(EmptyStack(S))
?? ??? ?return false;
?? ?x = S.data[S.top--];
?? ?return true;
}
?
void InitQueue(Queue &Q) //初始化隊(duì)列
{
?? ?Q.front = Q.rear = 0;
}
?
bool IsEmpty(Queue Q) //判斷隊(duì)列是否為空
{
?? ?if(Q.front == Q.rear)
?? ??? ?return true;
?? ?else
?? ??? ?return false;
}
?
bool IsOverflow(Queue Q) //判斷隊(duì)列是否滿
{
?? ?if((Q.rear+1)%MaxSize == Q.front)
?? ??? ?return true;
?? ?else
?? ??? ?return false;
}
?
bool EnQueue(Queue &Q,ElemType x) //進(jìn)隊(duì)列
{
?? ?if(IsOverflow(Q))
?? ??? ?return false;
?? ?Q.data[Q.rear] = x;
?? ?Q.rear = (Q.rear+1)%MaxSize;
?? ?return true;
}
?
bool DeQueue(Queue &Q,ElemType &x) //出隊(duì)列
{
?? ?if(IsEmpty(Q))
?? ??? ?return false;
?? ?x = Q.data[Q.front];
?? ?Q.front = (Q.front+1)%MaxSize;
?? ?return true;
}
?
bool ReverseQueue(Queue &Q)?
{
?? ?SqStack S;
?? ?ElemType x;
?? ?InitStack(S);
?? ?while(!IsEmpty(Q)) //當(dāng)隊(duì)列不為空時(shí),將隊(duì)列中的元素依次放入棧中
?? ?{
?? ??? ?DeQueue(Q,x);
?? ??? ?Push(S,x);
?? ?}
?? ?while(!EmptyStack(S)) //當(dāng)棧不為空時(shí),再將棧中元素依次放入隊(duì)列中
?? ?{
?? ??? ?Pop(S,x);
?? ??? ?EnQueue(Q,x);
?? ?}
?? ?return true;
}
?
void main()
{
?? ?Queue Q;
?? ?InitQueue(Q);
?? ?EnQueue(Q,1);
?? ?EnQueue(Q,2);
?? ?EnQueue(Q,3);
?? ?ReverseQueue(Q);
?? ?printf("%d ",Q.data[Q.front]);
}

(根據(jù)主函數(shù)中代碼)演示:

例如一開始隊(duì)列中元素為3 2 1 ->出

1. 將隊(duì)列中元素依次放到棧中,此時(shí)棧:1 2 3 ->出

2. 再將棧中的元素依次放入隊(duì)列中,此時(shí)隊(duì)列:1 2 3 ->出 

到此這篇關(guān)于C語言中用棧+隊(duì)列實(shí)現(xiàn)隊(duì)列中的元素逆置的文章就介紹到這了,更多相關(guān)用棧+隊(duì)列實(shí)現(xiàn)隊(duì)列中的元素逆置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

毕节市| 沙河市| 白银市| 东莞市| 肥东县| 山西省| 监利县| 山丹县| 乐平市| 栖霞市| 西乡县| 饶河县| 韶关市| 博罗县| 梁河县| 温宿县| 科技| 泗水县| 灵山县| 象州县| 噶尔县| 凌源市| 二连浩特市| 崇信县| 聂拉木县| 子洲县| 宣威市| 汕尾市| 宜丰县| 嵩明县| 四会市| 丰城市| 平泉县| 剑川县| 郁南县| 杨浦区| 斗六市| 大竹县| 连州市| 芮城县| 高州市|