数据结构-线性表之用队列实现栈用栈实现队列
生活随笔
收集整理的這篇文章主要介紹了
数据结构-线性表之用队列实现栈用栈实现队列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- **********用隊列實現棧
- 一:思路
- 二:實現
- (1)結構體定義
- (2)初始化和銷毀
- (3)進“棧”
- (4)出“棧”
- 三:代碼
- **********用棧實現隊列
- (1)思路
- (2)實現
- (3)代碼
**********用隊列實現棧
一:思路
二:實現
(1)結構體定義
(2)初始化和銷毀
注意:在測試文件外新建一個Mystack結構體時,不能只有一句Mystack ms,因為使用隊列實現棧,那么該棧的底層是隊列,所以必須同時要創建隊列結構體,然后進行賦值才可以
(3)進“棧”
注意:進棧和出棧本質是在操作隊列,所以這里僅僅使用的隊列操作的接口,如果想要了解隊列的出隊和入隊操作,請看:
數據結構-線性表之棧和隊列
(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;//儲備元素的棧Queue* _q2;//進元素的棧 }MyStack;void MyStackInit(MyStack* ps); void MyStackPush(MyStack* ps, DataType x);//進棧 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);//釋放儲存棧QNode* cur = ps->_q1->_front;QNode* pre;while (cur){pre = cur;cur = cur->_next;free(pre);}free(ps); }//入隊 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;} }// //出隊 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) {//如果隊列1不空就先把隊列1中的元素出隊列然后進入隊列2if (ps->_q1->_front != NULL){DataType save = QueuePop(ps->_q1);//出隊列1QueuePush(ps->_q2,save);//進隊列2}QueuePush(ps->_q2, x);//而后在正常進“棧” }DataType MystackPop(MyStack* ps) {if (ps->_q2->_front == NULL && ps->_q1->_front !=NULL)//如果隊列2為空,隊列1不空,將隊1所有元素出隊1,依次進隊2{while (ps->_q1->_front){DataType save = QueuePop(ps->_q1);QueuePush(ps->_q2, save);}}//出“棧”時,第一個元素就是隊列2的尾元素while(ps->_q2->_front!=ps->_q2->_rear)//出隊2直到隊列2剩余最后一個元素{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(); }**********用棧實現隊列
(1)思路
(2)實現
1:結構體定義
2:初始化
注意:下面的入隊和出隊要使用到入棧和出棧的接口
3:入“隊”和出“隊”
(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);//入“隊” DataType MyQueuePop(MyQueue* pq);//出“隊”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)); }總結
以上是生活随笔為你收集整理的数据结构-线性表之用队列实现栈用栈实现队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言重难点总结:printf和scan
- 下一篇: HDU5863 cjj's string