目錄
- 前言
- 一、原題
- 二、解題思路
- 三、代碼實現(c/c++)
- C語言代碼
- C++代碼實現
- 結語
前言
目前博主在處于秋招求職的關鍵時期,在暑假這段時間會頻繁更新博客,想在暑假期間把一些常考的面試和筆試題過一下,利用這兩個月沉淀一下技術,做出一,兩個比較大的項目,然后就是封裝一下簡歷,開始投遞了,我期待與26屆所有畢業生一起學習共同進步。
一、原題
二、解題思路
1.s1用作入隊棧,s2用作出隊棧。
2.s1入隊時,判斷兩個棧是否棧滿了,如果滿就算入隊失敗;如果s1滿了,就將s1里的元素出棧入棧到s2,同時也要判斷s2是否滿了,如果滿了就結束s1出棧,s2入棧這個操作,最后s1插入元素。
3.s2出隊時,判斷兩個棧是否為空,如果為空就出隊失敗;如果s2為空,就將s1所有元素出棧入棧到s2,最后出棧元素。
三、代碼實現(c/c++)
C語言代碼
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>#define MaxSize 10
typedef int DataType_t;typedef struct {DataType_t data[MaxSize];size_t top;
} Stack;typedef struct {Stack s1;Stack s2;
} Queue;void InitStack(Stack* stack) {stack->top = 0;
}bool IsFull(Stack* stack) {return stack->top == MaxSize;
}bool IsEmpty(Stack* stack) {return stack->top == 0;
}bool Push(Stack* stack, DataType_t val) {if (IsFull(stack)) {printf("棧滿,插入元素失敗\n");return false;}stack->data[stack->top++] = val;return true;
}bool Pop(Stack* stack, DataType_t* val) {if (IsEmpty(stack)) {printf("棧空,彈出元素失敗\n");return false;}*val = stack->data[--stack->top];return true;
}void InitQueue(Queue* queue) {InitStack(&queue->s1);InitStack(&queue->s2);
}bool IsQueueEmpty(Queue* queue) {return IsEmpty(&queue->s1) && IsEmpty(&queue->s2);
}bool IsQueueFull(Queue* queue) {return (queue->s1.top + queue->s2.top) == MaxSize;
}bool Enque(Queue* queue, DataType_t val) {if (IsQueueFull(queue)) {printf("隊滿,入隊失敗\n");return false;}DataType_t temp;if (IsFull(&queue->s1)) {while (!IsEmpty(&queue->s1) && !IsFull(&queue->s2)) {Pop(&queue->s1, &temp);Push(&queue->s2, temp);}}return Push(&queue->s1, val);
}bool Deque(Queue* queue, DataType_t* val) {if (IsQueueEmpty(queue)) {printf("隊空,出隊失敗\n");return false;}DataType_t temp;if (IsEmpty(&queue->s2)) {while (!IsEmpty(&queue->s1)) {Pop(&queue->s1, &temp);Push(&queue->s2, temp);}}return Pop(&queue->s2, val);
}
C++代碼實現
#include <iostream>
using namespace std;const int MAX_SIZE = 100; // 棧的最大容量// 棧結構定義
struct Stack {int data[MAX_SIZE];int top = -1; // 棧頂指針,初始為-1
};// 棧操作函數
void push(Stack &ST, int x) {if (ST.top < MAX_SIZE - 1) {ST.data[++ST.top] = x; // 棧頂指針先加1,再入棧}
}bool pop(Stack &ST, int &x) {if (ST.top == -1) return false; // 棧空,彈出失敗x = ST.data[ST.top--]; // 取棧頂元素,指針減1return true;
}bool isEmpty(Stack &ST) {return ST.top == -1; // 棧空返回 true
}// 隊列結構(由兩個棧組成)
struct Queue {Stack s1, s2;
};// 元素入隊列
bool enQueue(Queue &q, int x) {if (q.s1.top == MAX_SIZE - 1) {// s1 已滿,嘗試轉移元素到 s2if (!isEmpty(q.s2)) return false; // s2 非空,隊列滿,入隊失敗// 將 s1 的所有元素轉移到 s2(逆序)while (!isEmpty(q.s1)) {int temp;pop(q.s1, temp);push(q.s2, temp);}}push(q.s1, x); // 新元素壓入 s1return true;
}// 元素出隊列
bool deQueue(Queue &q, int &x) {if (!isEmpty(q.s2)) { // s2 非空,直接彈出pop(q.s2, x);return true;}// s2 為空,轉移 s1 的元素到 s2while (!isEmpty(q.s1)) {int temp;pop(q.s1, temp);push(q.s2, temp);}if (isEmpty(q.s2)) return false; // 轉移后仍為空,隊列空pop(q.s2, x); // 彈出 s2 棧頂(即隊首)return true;
}// 判斷隊列是否為空
bool queueEmpty(Queue &q) {return isEmpty(q.s1) && isEmpty(q.s2); // 兩棧均空時隊列為空
}int main() {Queue q;// 測試用例enQueue(q, 1);enQueue(q, 2);enQueue(q, 3);int x;deQueue(q, x); // 輸出 1cout << "Dequeued: " << x << endl;deQueue(q, x); // 輸出 2cout << "Dequeued: " << x << endl;cout << "Queue empty? " << queueEmpty(q) << endl; // 輸出 0(非空)return 0;
}
結語
在做項目之前,我們的基礎一定要打扎實,尤其是,這些簡單的線性數據結構,你們學到后面會發現,好多存儲結構都逃不掉,順序存儲結構和鏈式存儲結構,一定要自己動手多敲,只有腦子有料,到后面做項目才會得心應手,否則你到后面根本學不下。
希望各位靚仔靚女點贊,收藏,關注多多支持,我們共同進步,后續我會更新更多的面試真題,你們的支持將是我前進最大的動力。