日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用队列实现栈 AND 用栈实现队列

發布時間:2023/11/30 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用队列实现栈 AND 用栈实现队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用隊列實現棧

思路

  • 入隊列就是入棧
  • 出隊列的時候,就是把前面size-1個隊列中的元素先出,這樣最后一個元素就成隊首元素了,再把出去的元素再次入隊列
  • 讀棧頂元素,過程和第二步是一樣的,就是彈出后,再把它入隊列
  • typedef struct Node{int value;struct Node *next; }Node;//頭刪+尾插typedef struct Queue{Node *front; //鏈表第一個結點Node *rear; //鏈表最后一個結點int size; //數據個數 }Queue;//初始化 void QueueInit(Queue *queue){queue->front = NULL;queue->rear = NULL;queue->size = 0; } //銷毀 void QueueDestroy(Queue *queue){Node *node, *next;for (node = queue->front; node != NULL; node = next){next = node->next;free(node);}queue->front = queue->rear = NULL;queue->size = 0; }//Push,隊尾插入,即鏈表的尾插 void QueuePush(Queue *queue, int val){Node *node = (Node*)malloc(sizeof(Node));node->value = val;node->next = NULL;if (queue->rear != NULL){queue->rear->next = node;queue->rear = node;}else{queue->front = queue->rear = node;}queue->size++; }//Pop 隊尾出列,即鏈表的頭刪 void QueuePop(Queue* queue){assert(queue->size > 0);queue->size--;Node *front = queue->front;queue->front = queue->front->next;free(front);if (queue->front == NULL){//處理只有一個結點的情況queue->rear = NULL;} } //Front 返回隊首元素 int QueueFront(const Queue *queue){return queue->front->value; } //Rear 返回隊尾元素 int QueueRear(const Queue *queue){return queue->rear->value; } //Empty 是否為空 int QueueEmpty(const Queue *queue){return queue->size == 0 ? 1 : 0; } //Size int QueueSize(const Queue *queue){return queue->size ; }typedef struct{Queue queue;int maxSize; }MyStack;/** Initialize your data structure here. */MyStack* myStackCreate() {MyStack *stack = (MyStack *)malloc(sizeof(MyStack));QueueInit(&(stack->queue));stack->maxSize = sizeof(MyStack);return stack; }/** Push element x onto stack. */ void myStackPush(MyStack* obj, int x) {QueuePush(&(obj->queue), x); }/** Removes the element on top of the stack and returns that element. */ int myStackPop(MyStack* obj) {int size = QueueSize(&(obj->queue));for (int i = 0; i < size - 1; i++){int val = QueueFront(&(obj->queue));QueuePop(&(obj->queue));QueuePush(&(obj->queue), val);}int val = QueueFront(&(obj->queue));QueuePop(&(obj->queue));return val; }/** Get the top element. */ int myStackTop(MyStack* obj) {int size = QueueSize(&(obj->queue));for (int i = 0; i < size - 1; i++){int val = QueueFront(&(obj->queue));QueuePop(&(obj->queue));QueuePush(&(obj->queue), val);}int val = QueueFront(&(obj->queue));QueuePop(&(obj->queue));QueuePush(&(obj->queue),val);return val; }/** Returns whether the stack is empty. */ bool myStackEmpty(MyStack* obj) {return QueueEmpty(&(obj->queue)); }void myStackFree(MyStack* obj) {QueueDestroy(&(obj->queue));free(obj); }/*** Your MyStack struct will be instantiated and called as such:* MyStack* obj = myStackCreate();* myStackPush(obj, x);* int param_2 = myStackPop(obj);* int param_3 = myStackTop(obj);* bool param_4 = myStackEmpty(obj);* myStackFree(obj); */

    用棧實現隊列

    思路

  • 要用兩個棧實現隊列
  • 一個為左邊棧,一個為右邊棧,入隊列就相當于給左邊棧壓棧,而出隊列,則是看右邊棧空不空,如果為空,則將左邊棧的所有元素全部依次壓入右邊棧,然后取右邊棧頂元素,出棧(也就是出隊列)
  • 返回隊首元素,過程跟第二步一樣,唯一區別就是不用出棧,直接返回就行
  • 兩邊棧都為空時,隊列為空
  • class MyQueue { public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {left.push(x);}/** Removes the element from in front of queue and returns that element. */int pop() {if (right.empty()){while (!left.empty()){int top = left.top();left.pop();right.push(top);}}int top = right.top();right.pop();return top;}/** Get the front element. */int peek() {if (right.empty()){while (!left.empty()){int top = left.top();left.pop();right.push(top);}}int top = right.top();return top;}/** Returns whether the queue is empty. */bool empty() {return left.empty() && right.empty();}stack<int>left;stack<int>right; };/*** Your MyQueue object will be instantiated and called as such:* MyQueue* obj = new MyQueue();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->peek();* bool param_4 = obj->empty();*/

    總結

    以上是生活随笔為你收集整理的用队列实现栈 AND 用栈实现队列的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。