数据结构-线性表之用队列实现栈用栈实现队列
生活随笔
收集整理的這篇文章主要介紹了
数据结构-线性表之用队列实现栈用栈实现队列
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- **********用隊(duì)列實(shí)現(xiàn)棧
- 一:思路
- 二:實(shí)現(xiàn)
- (1)結(jié)構(gòu)體定義
- (2)初始化和銷(xiāo)毀
- (3)進(jìn)“棧”
- (4)出“棧”
- 三:代碼
- **********用棧實(shí)現(xiàn)隊(duì)列
- (1)思路
- (2)實(shí)現(xiàn)
- (3)代碼
**********用隊(duì)列實(shí)現(xiàn)棧
一:思路
二:實(shí)現(xiàn)
(1)結(jié)構(gòu)體定義
(2)初始化和銷(xiāo)毀
注意:在測(cè)試文件外新建一個(gè)Mystack結(jié)構(gòu)體時(shí),不能只有一句Mystack ms,因?yàn)槭褂藐?duì)列實(shí)現(xiàn)棧,那么該棧的底層是隊(duì)列,所以必須同時(shí)要?jiǎng)?chuàng)建隊(duì)列結(jié)構(gòu)體,然后進(jìn)行賦值才可以
(3)進(jìn)“棧”
注意:進(jìn)棧和出棧本質(zhì)是在操作隊(duì)列,所以這里僅僅使用的隊(duì)列操作的接口,如果想要了解隊(duì)列的出隊(duì)和入隊(duì)操作,請(qǐng)看:
數(shù)據(jù)結(jié)構(gòu)-線性表之棧和隊(duì)列
(4)出“棧”
三:代碼
QueueToStack.h
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h>typedef int DataType;typedef struct QNode {DataType _val;struct QNode* _next; }QNode;typedef struct Queue {QNode* _front;QNode* _rear; }Queue;typedef struct MyStack {Queue* _q1;//儲(chǔ)備元素的棧Queue* _q2;//進(jìn)元素的棧 }MyStack;void MyStackInit(MyStack* ps); void MyStackPush(MyStack* ps, DataType x);//進(jìn)棧 DataType MystackPop(MyStack* ps);//出棧QueueToStack.c
#include "QueueToStack.h"void MyStackInit(MyStack* ps) {assert(ps);ps->_q1->_front = NULL;ps->_q1->_rear = NULL;ps->_q2->_front = NULL;ps->_q2->_rear = NULL;printf("初始化成功\n"); } void MystackDestory(MyStack* ps) {assert(ps);//釋放儲(chǔ)存棧QNode* cur = ps->_q1->_front;QNode* pre;while (cur){pre = cur;cur = cur->_next;free(pre);}free(ps); }//入隊(duì) void QueuePush(Queue* pq, DataType x) {assert(pq);QNode* NewNode = (QNode*)malloc(sizeof(QNode));NewNode->_val = x;NewNode->_next = NULL;if (pq->_rear == NULL){pq->_rear = NewNode;pq->_front = NewNode;}else{pq->_rear->_next = NewNode;pq->_rear = NewNode;} }// //出隊(duì) DataType QueuePop(Queue* pq) {assert(pq);if (pq->_front == pq->_rear){DataType rec = pq->_front->_val;free(pq->_rear);pq->_rear = NULL;pq->_front = NULL;return rec;}else{QNode* cur = pq->_front;pq->_front = pq->_front->_next;return cur->_val;free(cur);} }void MyStackPush(MyStack* ps,DataType x) {//如果隊(duì)列1不空就先把隊(duì)列1中的元素出隊(duì)列然后進(jìn)入隊(duì)列2if (ps->_q1->_front != NULL){DataType save = QueuePop(ps->_q1);//出隊(duì)列1QueuePush(ps->_q2,save);//進(jìn)隊(duì)列2}QueuePush(ps->_q2, x);//而后在正常進(jìn)“棧” }DataType MystackPop(MyStack* ps) {if (ps->_q2->_front == NULL && ps->_q1->_front !=NULL)//如果隊(duì)列2為空,隊(duì)列1不空,將隊(duì)1所有元素出隊(duì)1,依次進(jìn)隊(duì)2{while (ps->_q1->_front){DataType save = QueuePop(ps->_q1);QueuePush(ps->_q2, save);}}//出“棧”時(shí),第一個(gè)元素就是隊(duì)列2的尾元素while(ps->_q2->_front!=ps->_q2->_rear)//出隊(duì)2直到隊(duì)列2剩余最后一個(gè)元素{DataType save = QueuePop(ps->_q2);QueuePush(ps->_q1, save);}DataType rec = QueuePop(ps->_q2);return rec; }test.c
#include "QueueToStack.h"void test() {MyStack ms;Queue q1;Queue q2;ms._q1 = &q1;ms._q2 = &q2;MyStackInit(&ms);MyStackPush(&ms, 1);MyStackPush(&ms, 2);MyStackPush(&ms, 3);MyStackPush(&ms, 4);MyStackPush(&ms, 5);printf("%d ", MystackPop(&ms));printf("%d ", MystackPop(&ms));printf("%d ", MystackPop(&ms));printf("%d ", MystackPop(&ms));printf("%d ", MystackPop(&ms)); } int main() {test(); }**********用棧實(shí)現(xiàn)隊(duì)列
(1)思路
(2)實(shí)現(xiàn)
1:結(jié)構(gòu)體定義
2:初始化
注意:下面的入隊(duì)和出隊(duì)要使用到入棧和出棧的接口
3:入“隊(duì)”和出“隊(duì)”
(3)代碼
StackToQueue.h
#pragma once #include <stdio.h> #include <stdlib.h> #include <assert.h>typedef int DataType;typedef struct {DataType* _arr;int _top;int _capacity;}Stack; typedef struct MyQueue {Stack* _S1;Stack* _S2;}MyQueue;void MyQueueInit(MyQueue* pq);//初始化 void MyQueuePush(MyQueue*pq, DataType x);//入“隊(duì)” DataType MyQueuePop(MyQueue* pq);//出“隊(duì)”StackToQueue.c
#pragma once #include "StackToQueue.h"void MyQueueInit(MyQueue* pq) {assert(pq);assert(pq->_S1);assert(pq->_S2);pq->_S1->_arr = (DataType*)malloc(sizeof(DataType)*4);pq->_S1->_top = -1;pq->_S1->_capacity = 4;pq->_S2->_arr = (DataType*)malloc(sizeof(DataType) * 4);pq->_S2->_top = -1;pq->_S2->_capacity = 4; }DataType StackPop(Stack* ps)//出棧 {assert(ps);if (ps->_top == -1){printf("棧空格\n");exit(-1);}DataType rec = ps->_arr[ps->_top];ps->_top--;return rec; } void StackPush(Stack* ps, DataType x)//入棧 {assert(ps);if (ps->_top == ps->_capacity - 1){ps->_capacity *= 2;DataType* temp = (DataType)realloc(ps->_arr, sizeof(DataType)*ps->_capacity);ps->_arr = temp;}ps->_top++;ps->_arr[ps->_top] = x; }void MyQueuePush(MyQueue*pq, DataType x) {StackPush(pq->_S1, x);} DataType MyQueuePop(MyQueue* pq) {while (pq->_S1->_top != -1){DataType save = StackPop(pq->_S1);StackPush(pq->_S2,save);}DataType rec= StackPop(pq->_S2);while (pq->_S2->_top != -1){DataType save = StackPop(pq->_S2);StackPush(pq->_S1, save);}return rec; }3:test.c
#pragma once #include "StackToQueue.h"void test() {MyQueue q;Stack s1;Stack s2;q._S1 = &s1;q._S2 = &s2;MyQueueInit(&q);MyQueuePush(&q, 1);MyQueuePush(&q, 2);MyQueuePop(&q);MyQueuePush(&q, 3);printf("%d ", MyQueuePop(&q));printf("%d ", MyQueuePop(&q)); }總結(jié)
以上是生活随笔為你收集整理的数据结构-线性表之用队列实现栈用栈实现队列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C语言重难点总结:printf和scan
- 下一篇: HDU5863 cjj's string