2024年用队列实现栈,2024年最新C C++电话面试技巧

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

typedef struct Queue
{
QNode* front;
QNode* tail;
}Queue;

// 初始化队列
void QueueInit(Queue* q);
// 队尾入队列
void QueuePush(Queue* q, QDataType data);
// 队头出队列
void QueuePop(Queue* q);
// 获取队列头部元素
QDataType QueueFront(Queue* q);
// 获取队列队尾元素
QDataType QueueBack(Queue* q);
// 获取队列中有效元素个数
int QueueSize(Queue* q);
// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q);
// 销毁队列
void QueueDestory(Queue* pq);

#define _CRT_SECURE_NO_WARNINGS 1

// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->front = NULL;
q->tail = NULL;
}

// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
if (newnode == NULL)
{
perror(“QueuePush:”);
exit(-1);
}
newnode->data = data;
newnode->next = NULL;
if (q->front ==NULL && q->tail == NULL)
{
q->front = q->tail = newnode;
}
else
{
q->tail->next = newnode;
q->tail = newnode;
}
}

int QueueSize(Queue* q)
{
assert(q);
QNode* cur=q->front;
int count=0;
while(cur!=NULL)
{
count++;
cur=cur->next;
}
return count;
}

// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(q->front && q->tail);

QNode* next = q->front->next;
if (next != NULL)
{
free(q->front);
q->front = NULL;
q->front = next;
}
else
{
free(q->front);
q->front = NULL;
q->tail = NULL;
}
}

// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(q->front != NULL);
return q->front->data;
}

// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(q->tail != NULL);
return q->tail->data;
}

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
int QueueEmpty(Queue* q)
{
assert(q);
return q->front ==NULL && q->tail == NULL;
}

// // 销毁队列
// void QueueDestory(Queue* q)
// {
// assert(q);
// assert(q->front == NULL);
// QNode* cur = q->front;
// QNode* next = cur->next;
// while (next != NULL)
// {
// free(cur);
// cur = NULL;
// cur = next;
// next = next->next;
// }
// free(cur);
// cur = NULL;
// }

void QueueDestory(Queue* pq)
{
assert(pq);
QNode* cur = pq->front;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
}
///

// #include <stdio.h>
// #include <stdlib.h>
// #include <stdbool.h>
// #include <assert.h>

// typedef int QDataType;

// //typedef struct QueueNode
// //{
// // QDataType data;
// // struct QueueNode* next;
// //}QNode, *PNode;

// typedef struct QueueNode
// {
// QDataType data;
// struct QueueNode* next;
// }QNode;

// typedef struct Queue
// {
// QNode* head;
// QNode* tail;

// //size_t size;
// }Queue;

// void QueueInit(Queue* pq);
// void QueueDestory(Queue* pq);
// void QueuePush(Queue* pq, QDataType x);
// void QueuePop(Queue* pq);
// bool QueueEmpty(Queue* pq);
// size_t QueueSize(Queue* pq);
// QDataType QueueFront(Queue* pq);
// QDataType QueueBack(Queue* pq);

// void QueueInit(Queue* pq)
// {
// assert(pq);
// pq->head = pq->tail = NULL;
// }

// void QueueDestory(Queue* pq)
// {
// assert(pq);
// QNode* cur = pq->head;
// while (cur)
// {
// QNode* next = cur->next;
// free(cur);
// cur = next;
// }

// pq->head = pq->tail = NULL;
// }

// void QueuePush(Queue* pq, QDataType x)
// {
// assert(pq);
// QNode* newnode = (QNode*)malloc(sizeof(QNode));
// assert(newnode);

// newnode->data = x;
// newnode->next = NULL;

// if (pq->tail == NULL)
// {
// assert(pq->head == NULL);
// pq->head = pq->tail = newnode;
// }
// else
// {
// pq->tail->next = newnode;
// pq->tail = newnode;
// }
// }

// void QueuePop(Queue* pq)
// {
// assert(pq);
// assert(pq->head && pq->tail);

// if (pq->head->next == NULL)
// {
// free(pq->head);
// pq->head = pq->tail = NULL;
// }
// else
// {
// QNode* next = pq->head->next;
// free(pq->head);
// pq->head = next;
// }
// }

// bool QueueEmpty(Queue* pq)
// {
// assert(pq);

// //return pq->head == NULL && pq->tail == NULL;
// return pq->head == NULL;
// }

// size_t QueueSize(Queue* pq)
// {
// assert(pq);
// QNode* cur = pq->head;
// size_t size = 0;
// while (cur)
// {
// size++;
// cur = cur->next;
// }

// return size;
// }

// QDataType QueueFront(Queue* pq)
// {
// assert(pq);
// assert(pq->head);

// return pq->head->data;
// }

// QDataType QueueBack(Queue* pq)
// {
// assert(pq);
// assert(pq->tail);

// return pq->tail->data;
// }

//
//以上是队列

//栈是后进先出 队列是先进后出
//用两个队列来回倒
//思路:把数据入到一个有数据的队列,出的时候把前n-1个数据倒到另一个队列
//然后留下来的数据再出出去就可以了,就这样一直来回倒。

//用两个队列实现
typedef struct {
Queue q1;
Queue q2;
} MyStack;

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

转存中…(img-rujU6IHV-1715676172167)]
[外链图片转存中…(img-SUOEkvA8-1715676172167)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值