0
点赞
收藏
分享

微信扫一扫

数据结构--栈,队列

juneyale 2022-08-30 阅读 63

前言

        这篇文章主要是关于栈与队列,为什么一般都是将栈与队列挨着讲,因为他们两个像一对欢喜冤家一样,在争锋相对的同时也互相成就,彼此通过不同的性质又可以联系在一起。本章节主要成分部分:什么是栈和队列,栈和队列的构造,栈和队列的习题。通过基础知识完成练习,通过练习巩固知识与理解栈与队列的联系。相信这篇文章会对大家有帮助,因为计算机就是解决我们生活中的问题,栈与队列的性质也是来源于生活中,比如栈--手枪的弹夹,队列--排队。

目录

前言

栈的概念及结构

 栈的实现

实现代码

队列

队列的概念及结构

 队列的实现

 实现代码

循环队列

 栈和队列面试题

括号匹配问题---链接

用队列实现栈--链接

用栈实现队列--链接

设计循环队列--链接


栈的概念及结构

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

动态演示

出栈:栈的删除操作叫做出栈。出数据也在栈顶。

动态演示

 栈的实现

        栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为压栈与出栈正好满足数组的尾插与头删。数组的代价是及小的,操作相当于链表也更加简便。

动态演示

                                                                        压栈

出栈 

实现代码

Stack.h

#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>

typedef int STDataType;

//#define N 10
//typedef struct Stack
//{
// STDataType _a[N];
// int _top; // 栈顶
//}Stack;

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);

Stack.c

#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"

// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);//断言传入地址是否为空
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}

// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int newCapacity = ps->_capacity == 0 ? 4 : ps->_capacity*2;//判断容量是否为空并设置增加容量数量

STDataType* temp = (STDataType*)realloc(ps->_a, newCapacity*sizeof(STDataType));//增加容量
if (temp == NULL)//判断地址是否开辟成功
{
perror("realloc fail");
exit(-1);
}
ps->_a = temp;//赋址与结构体中
ps->_capacity = newCapacity;//更新容量
}
ps->_a[ps->_top] = data;//数据入栈
ps->_top++;//栈顶++
}

// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//断言栈是否为空
--ps->_top;//栈顶--
}

// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));

return ps->_a[ps->_top-1];
}

// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);

return ps->_top;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top==0;
}

// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);//清除数组地址
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}

Test.c

#define _CRT_SECURE_NO_WARNINGS
#include "Stack.h"


//进栈--出栈演示
void TestStack()
{
Stack ps;
StackInit(
StackPush(
StackPush(
StackPush(
// printf("%d ", StackTop(
StackPop(
// printf("%d ", StackTop(
StackPop(

while (!StackEmpty(&ps))
{
printf("%d ", StackTop(
StackPop(
}
printf("\n");
}

int main()
{
TestStack();

return 0;
}

队列

队列的概念及结构

入队列:进行插入操作的一端称为队尾

出队列:进行删除操作的一端称为队头

动态演示

 队列的实现

        队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低。

动态演示

 实现代码

Queue.h

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

// 链式结构:表示队列
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;

// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
QDataType _size;
}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
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);

Queue.c

#include "Queue.h"

// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->_front = q->_rear = NULL;
q->_size = 0;
}

// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);

QNode* cur = (QNode*)malloc(sizeof(QNode));
if (cur == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
cur->_data = data;
cur->_next = NULL;
}

if (q->_rear == NULL)
{
q->_front = q->_rear = cur;
}
else
{
q->_rear->_next = cur;
q->_rear = cur;
}
q->_size++;
}

// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
if (q->_front->_next==NULL)
{
free(q->_front);
q->_front = q->_rear = NULL;
}
else
{
QNode* cur = q->_front;
q->_front = q->_front->_next;
free(cur);
cur = NULL;
}
q->_size--;
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->_front->_data;
}

// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->_rear->_data;
}

// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);

return q->_size;
}

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q)
{
assert(q);

return q->_front == NULL && q->_rear == NULL;
}

// 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->_front;
while (cur)
{
QNode* del = cur;
cur = cur->_next;
free(del);
del = NULL;
}
q->_front = q->_rear = NULL;
}

Test.c

#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"

void TestQueue()
{
Queue q;
QueueInit(
QueuePush(
QueuePush(
QueuePush(
QueuePop(
printf("%d ", QueueFront(
// printf("%d ", QueueBack(
QueuePush(
printf("%d ", QueueBack(
QueuePush(
printf("%d ", QueueBack(
QueuePush(
QueuePush(
QueuePush(

while (!QueueEmpty(&q))
{
printf("%d ", QueueFront(
QueuePop(
}

QueueDestroy(
}

int main()
{
TestQueue();

return 0;
}

循环队列

        另外扩展了解一下,实际中我们有时还会使用一种队列叫循环队列。如操作系统课程讲解生产者消费者模型 时可以就会使用循环队列。环形队列可以使用数组实现,也可以使用循环链表实现。

 

 栈和队列面试题

括号匹配问题---链接

动态演示

 

 两种特殊错误情况

实现代码

typedef char STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);

// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);//断言传入地址是否为空
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}

// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int newCapacity = ps->_capacity == 0 ? 4 : ps->_capacity*2;//判断容量是否为空并设置增加容量数量

STDataType* temp = (STDataType*)realloc(ps->_a, newCapacity*sizeof(STDataType));//增加容量
if (temp == NULL)//判断地址是否开辟成功
{
perror("realloc fail");
exit(-1);
}
ps->_a = temp;//赋址与结构体中
ps->_capacity = newCapacity;//更新容量
}
ps->_a[ps->_top] = data;//数据入栈
ps->_top++;//栈顶++
}

// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//断言栈是否为空
--ps->_top;//栈顶--
}

// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));

return ps->_a[ps->_top-1];
}

// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);

return ps->_top;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top==0;
}

// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);//清除数组地址
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}

bool isValid(char * s){
Stack st;
StackInit(
while(*s)
{
if(*s=='('||*s=='{'||*s=='[')
{
StackPush(
}
else
{
if(StackEmpty(&st))
{
StackDestroy(
return false;
}
char tem= StackTop(
StackPop(
if(*s=='}' '{' || *s==')' '(' || *s==']' '[')
{
StackDestroy(
return false;
}
}
s++;
}
bool flag=StackEmpty(
StackDestroy(
return flag;
}

用队列实现栈--链接

动态演示

 

 实现代码

// 链式结构:表示队列 
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* _next;
QDataType _data;
}QNode;

// 队列的结构
typedef struct Queue
{
QNode* _front;
QNode* _rear;
QDataType _size;
}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
bool QueueEmpty(Queue* q);
// 销毁队列
void QueueDestroy(Queue* q);


// 初始化队列
void QueueInit(Queue* q)
{
assert(q);
q->_front = q->_rear = NULL;
q->_size = 0;
}

// 队尾入队列
void QueuePush(Queue* q, QDataType data)
{
assert(q);

QNode* cur = (QNode*)malloc(sizeof(QNode));
if (cur == NULL)
{
perror("malloc fail");
exit(-1);
}
else
{
cur->_data = data;
cur->_next = NULL;
}

if (q->_rear == NULL)
{
q->_front = q->_rear = cur;
}
else
{
q->_rear->_next = cur;
q->_rear = cur;
}
q->_size++;
}

// 队头出队列
void QueuePop(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
if (q->_front->_next==NULL)
{
free(q->_front);
q->_front = q->_rear = NULL;
}
else
{
QNode* cur = q->_front;
q->_front = q->_front->_next;
free(cur);
cur = NULL;
}
q->_size--;
}
// 获取队列头部元素
QDataType QueueFront(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->_front->_data;
}

// 获取队列队尾元素
QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->_rear->_data;
}

// 获取队列中有效元素个数
int QueueSize(Queue* q)
{
assert(q);

return q->_size;
}

// 检测队列是否为空,如果为空返回非零结果,如果非空返回0
bool QueueEmpty(Queue* q)
{
assert(q);

return q->_front == NULL && q->_rear == NULL;
}

// 销毁队列
void QueueDestroy(Queue* q)
{
assert(q);
QNode* cur = q->_front;
while (cur)
{
QNode* del = cur;
cur = cur->_next;
free(del);
del = NULL;
}
q->_front = q->_rear = NULL;
}

typedef struct {
Queue q1;
Queue q2;
} MyStack;

//创建队列形成的栈
MyStack* myStackCreate() {
MyStack* obj=(MyStack*)malloc(sizeof(MyStack));
QueueInit(
QueueInit(

return obj;
}

//压栈
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(
}
else
{
QueuePush(
}
}

//出栈 QueuePop
int myStackPop(MyStack* obj) {
QNode* empty=
QNode* nonempty=
if(!QueueEmpty(&obj->q1))
{
empty=
nonempty=
}

while(QueueSize(nonempty)>1)
{
QueuePush(empty,QueueFront(nonempty));
QueuePop(nonempty);
}
int top=QueueFront(nonempty);
QueuePop(nonempty);
return top;
}

//栈顶值
int myStackTop(MyStack* obj) {
if(!QueueEmpty(&obj->q1))
{
return QueueBack(
}
else
{
return QueueBack(
}
}
//判断栈为空
bool myStackEmpty(MyStack* obj) {

return QueueEmpty(&obj->q1)&&QueueEmpty(
}

//释放栈
void myStackFree(MyStack* obj) {
QueueDestroy(
QueueDestroy(
free(obj);
obj==NULL;
}

/**
* 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);
*/

用栈实现队列--链接

动态演示

  实现代码

typedef int STDataType; 

//#define N 10
//typedef struct Stack
//{
// STDataType _a[N];
// int _top; // 栈顶
//}Stack;

// 支持动态增长的栈
typedef int STDataType;
typedef struct Stack
{
STDataType* _a;
int _top; // 栈顶
int _capacity; // 容量
}Stack;
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);

// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);//断言传入地址是否为空
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}

// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
int newCapacity = ps->_capacity == 0 ? 4 : ps->_capacity*2;//判断容量是否为空并设置增加容量数量

STDataType* temp = (STDataType*)realloc(ps->_a, newCapacity*sizeof(STDataType));//增加容量
if (temp == NULL)//判断地址是否开辟成功
{
perror("realloc fail");
exit(-1);
}
ps->_a = temp;//赋址与结构体中
ps->_capacity = newCapacity;//更新容量
}
ps->_a[ps->_top] = data;//数据入栈
ps->_top++;//栈顶++
}

// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));//断言栈是否为空
--ps->_top;//栈顶--
}

// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));

return ps->_a[ps->_top-1];
}

// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);

return ps->_top;
}

// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps)
{
assert(ps);
return ps->_top==0;
}

// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);//清除数组地址
ps->_a = NULL;
ps->_top = ps->_capacity = 0;
}

typedef struct {
Stack push;
Stack pop;

} MyQueue;


MyQueue* myQueueCreate() {
MyQueue* obj=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(
StackInit(

return obj;
}

void myQueuePush(MyQueue* obj, int x) {

StackPush(&obj->push,x);
}

int myQueuePop(MyQueue* obj) {
if(StackEmpty(&obj->pop))
{
while(!StackEmpty(&obj->push))
{
StackPush(&obj->pop,StackTop(&obj->push));
StackPop(
}
}

STDataType tem =StackTop(
StackPop(

return tem;
}

int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&obj->pop))
{
while(!StackEmpty(&obj->push))
{
StackPush(&obj->pop,StackTop(&obj->push));
StackPop(
}
}

return StackTop(
}

bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->push)&&StackEmpty(
}

void myQueueFree(MyQueue* obj) {
StackDestroy(
StackDestroy(
free(obj);
}

/**
* Your MyQueue struct will be instantiated and called as such:
* MyQueue* obj = myQueueCreate();
* myQueuePush(obj, x);

* int param_2 = myQueuePop(obj);

* int param_3 = myQueuePeek(obj);

* bool param_4 = myQueueEmpty(obj);

* myQueueFree(obj);
*/

设计循环队列--链接

用数组更好还是链表

在考虑用谁更好时,有个问题已经来了,如何判断满和空

判断空只需要front==rear就为空

 判断满 有两个方法: 1.用size计数 2.增加一个空节点

这里如果用size的话我们发现实现循环还是需要加节点,那如果不加呢?

不加节点我们发现,先走的rear,如果满了rear与front就重复了到底是空还是满就不好判断。所以这里我们选择加节点。

当加了节点我们发现循环之后需要获取队列最后一个元素时,需要重新遍历或者加一个指向前面的指针

当我们决定用数组时我们需要注意什么呢?

 当rear进行循环时,rear+1会出现越界的问题,如何解决呢?

我们只需要(rear+1)%rear 即可,就可以进行循环。

动态演示

 实现代码

typedef struct {
int *a;
int front;
int rear;
int N;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue* obj=(MyCircularQueue*)malloc(sizeof(MyCircularQueue));
obj->a=(int*)malloc((k+1)*sizeof(int));
obj->front=obj->rear=0;
obj->N=k+1;

return obj;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return obj->front==obj->rear;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->rear+1)%obj->N==obj->front;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if( myCircularQueueIsFull(obj))
return false;

obj->a[obj->rear]=value;
obj->rear++;
obj->rear %= obj->N;

return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return false;

obj->front++;

obj->front %= obj->N;
return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
else
return obj->a[obj->front];
}

int myCircularQueueRear(MyCircularQueue* obj) {
if(myCircularQueueIsEmpty(obj))
return -1;
else
return obj->a[(obj->rear+obj->N-1)%obj->N];
}



void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
free(obj);
}

/**
* Your MyCircularQueue struct will be instantiated and called as such:
* MyCircularQueue* obj = myCircularQueueCreate(k);
* bool param_1 = myCircularQueueEnQueue(obj, value);

* bool param_2 = myCircularQueueDeQueue(obj);

* int param_3 = myCircularQueueFront(obj);

* int param_4 = myCircularQueueRear(obj);

* bool param_5 = myCircularQueueIsEmpty(obj);

* bool param_6 = myCircularQueueIsFull(obj);

* myCircularQueueFree(obj);
*/
举报

相关推荐

0 条评论