泗阳做网站公司,阿里做外贸的网站,js搜索网站开发,成都高新seo目录前言一、原题二、解题思路三、代码实现#xff08;c/c#xff09;C语言代码C代码实现结语前言
目前博主在处于秋招求职的关键时期#xff0c;在暑假这段时间会频繁更新博客#xff0c;想在暑假期间把一些常考的面试和笔试题过一下#xff0c;利用这两个月沉淀一下技术…
目录前言一、原题二、解题思路三、代码实现c/cC语言代码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;
}结语
在做项目之前我们的基础一定要打扎实尤其是这些简单的线性数据结构你们学到后面会发现好多存储结构都逃不掉顺序存储结构和链式存储结构一定要自己动手多敲只有脑子有料到后面做项目才会得心应手否则你到后面根本学不下。 希望各位靓仔靓女点赞收藏关注多多支持我们共同进步后续我会更新更多的面试真题你们的支持将是我前进最大的动力。