前言
喜欢的老铁,留下你们宝贵的三连!
用队列实现栈
题目链接
重点说下入栈,入栈是入空队列,然后再将另一个队列的数据倒进去,
出栈就直接对有数据的队列进行出队列即可.
具体代码:
typedef int QNodeType;
typedef struct QueueNode// 节点
{
QNodeType data;
struct QueueNode* next;
}QueueNode;
typedef struct Queue
{
int size;//记录个数
QueueNode* head;
QueueNode* tail;
}Queue;
void QueueInit(Queue* q);//初始化
void QueueDestory(Queue* q);//销毁队列
void QueuePush(Queue* q, QNodeType x);//入列
void QueuePop(Queue* q);//出列
QNodeType QueueFront(Queue* q);//队列的头
QNodeType QueueBack(Queue* q);//队列的尾
bool QueueEmpty(Queue* q);//判断是否为空
int QueueSize(Queue* q);//输出个数
void QueueInit(Queue* q)//初始化
{
assert(q);
q->head = q->tail = NULL;
q->size = 0;
}
void QueuePush(Queue* q, QNodeType x)//入列
{
assert(q);
QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
if (newNode == NULL)
{
perror("malloc fail");
exit(-1);
}
newNode->next = NULL;
newNode->data = x;
if (q->head == NULL)
{
q->head = q->tail = newNode;
q->size++;
}
else
{
q->tail->next = newNode;
q->size++;
q->tail = newNode;
}
}
void QueuePop(Queue* q)//出列
{
assert(q);
if (q->head->next == NULL)//一个数据
{
free(q->head);
q->head = NULL;
}
else//多个数据
{
QueueNode* next = q->head->next;
free(q->head);
q->head = next;
}
q->size--;
}
QNodeType QueueFront(Queue* q)//队列的头
{
assert(q);
assert(!QueueEmpty(q));
return q->head->data;
}
QNodeType QueueBack(Queue* q)//队列的尾
{
assert(q);
assert(!QueueEmpty(q));
return q->tail->data;
}
bool QueueEmpty(Queue* q)//判断是否为空
{
if (q->head == NULL)
return 1;
else
return 0;
}
int QueueSize(Queue* q)//输出个数
{
return q->size;
}
void QueueDestory(Queue* q)//销毁队列
{
assert(q);
QueueNode* cur = q->head;
while (cur)
{
if (cur->next == NULL)
{
free(cur);
cur = NULL;
}
else
{
QueueNode* next = cur->next;
free(cur);
cur = next;
}
}
QueueInit(q);
}
typedef struct {
Queue Queue1;
Queue Queue2;
} MyStack;
MyStack* myStackCreate() {
MyStack* obj = (MyStack*)malloc(sizeof(MyStack));
QueueInit(&obj->Queue1);
QueueInit(&obj->Queue2);
return obj;
}
void myStackPush(MyStack* obj, int x) {
if (QueueEmpty(&obj->Queue1))
{
QueuePush(&obj->Queue2, x);
}
else
{
QueuePush(&obj->Queue1, x);
}
}
int myStackPop(MyStack* obj) {
if (QueueEmpty(&obj->Queue1))
{
while (obj->Queue2.head->next)
{
QueuePush(&obj->Queue1, QueueFront(&obj->Queue2));
QueuePop(&obj->Queue2);
}
QNodeType Front = QueueFront(&obj->Queue2);
QueuePop(&obj->Queue2);
return Front;
}
else
{
while (obj->Queue1.head->next)
{
QueuePush(&obj->Queue2, QueueFront(&obj->Queue1));
QueuePop(&obj->Queue1);
}
QNodeType Front = QueueFront(&obj->Queue1);
QueuePop(&obj->Queue1);
return Front;
}
}
int myStackTop(MyStack* obj) {
if (QueueEmpty(&obj->Queue1))
{
return QueueBack(&obj->Queue2);
}
else
{
return QueueBack(&obj->Queue1);
}
}
bool myStackEmpty(MyStack* obj) {
return QueueEmpty(&obj->Queue1) && QueueEmpty(&obj->Queue2);
}
void myStackFree(MyStack* obj) {
if (QueueEmpty(&obj->Queue1))
QueueDestory(&obj->Queue2);
else
QueueDestory(&obj->Queue1);
}
用栈实现队列
题目链接
重点说一下入队列:入队列就直接将数据存储到PushStack中即可
出列:向PopStack中取数据,如果没有数据就将PushStack中的数据导入PopStack中
具体代码:
typedef int STDataType;
typedef struct Stack
{
STDataType* data;//数据
int top;//栈顶
int capacity;//容量
}Stack;
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->capacity = 0;
ps->data = NULL;
ps->top = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->top == ps->capacity)//判断容量
{
int newcapacity = (ps->capacity == 0 ? 4 : 2 * ps->capacity);
STDataType*tmp=realloc(ps->data,sizeof(STDataType)*newcapacity);//扩容
if (tmp == NULL)
{
perror("realloc fail");
exit(-1);
}
ps->capacity = newcapacity;
ps->data = tmp;
}
ps->data[ps->top] = data;//压入数据
ps->top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
if(ps->top==0)return NULL;
ps->top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
if(ps->top==0)return NULL;
return ps->data[ps->top-1];//top位置是压入数据的位置,所以栈顶元素也就是它前一个元素
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
return ps->top;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->data);
ps ->data= NULL;
ps->top = 0;
ps->capacity = 0;
}
int StackEmpty(Stack* ps)
{
if (ps->top == 0)return 1;
else return 0;
}
typedef struct {
Stack PushST;
Stack PopST;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&(obj->PushST));
StackInit(&(obj->PopST));
return obj;
}
void myQueuePush(MyQueue* obj, int x) {
StackPush(&(obj->PushST),x);
}
int myQueuePop(MyQueue* obj) {
if(StackEmpty(&(obj->PopST)))
{
while(!StackEmpty(&(obj->PushST)))
{
StackPush(&(obj->PopST),StackTop(&(obj->PushST)));
StackPop(&(obj->PushST));
}
}
STDataType Front=StackTop(&(obj->PopST)) ;
StackPop(&(obj->PopST));
return Front;
}
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&(obj->PopST)))
{
while(!StackEmpty(&(obj->PushST)))
{
StackPush(&(obj->PopST),StackTop(&(obj->PushST)));
StackPop(&(obj->PushST));
}
}
return StackTop(&(obj->PopST));
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&(obj->PushST))&&StackEmpty(&(obj->PopST));
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&(obj->PopST));
StackDestroy(&(obj->PushST));
free(obj);
}