【C语言 - 数据结构】浅析栈和队列

简介: 【C语言 - 数据结构】浅析栈和队列

一、栈、队列以及双端队列的概念


1.1栈的概念及结构


栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端 称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。


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


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


image.png


1.2队列的概念及结构


队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出 FIFO(First In First Out)


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


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


1669439306948.jpg


1.3双端队列的概念及结构


双端队列:是一种线性表,又称为双向队列,所有的插入和删除(通常还有所有的访问)都在表的两端进行。


image.png


二、栈的实现和模拟栈


栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的 代价比较小。、


1669439376754.jpg

1669439386909.jpg


2.1 实现一个支持动态增长的栈


头文件:


#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int STDataType;
typedef struct Stack//动态栈
{
  int *a;
  int top;//栈顶的位置
  int capacity;//容量
}ST;
STDataType StackTop(ST* ps);//返回栈顶的值
void StackInit(ST* ps);//初始化栈
void StackDestory(ST* ps);//销毁栈
void StackPop(ST* ps);//弹出
void StackPush(ST* ps, STDataType x);//插入
bool StackEmpty(ST* ps);//判断栈是否为空。

源文件:


#include"Stack.h"
void StackInit(ST* ps)//栈的初始化
{
  assert(ps);
  ps->a = NULL;//a点的值指向空
  ps->top = 0;//栈底为0
  ps->capacity = 0;//空间为0
}
void StackDestory(ST* ps)
{
  assert(ps);
  free(ps->a);//把a释放掉
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)//入数据
{
  assert(ps);
  //满了就扩容
  if (ps->top == ps->capacity)//如果栈的栈顶恰好和容量相等就扩容
  {
  int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
  if (ps->a == NULL)
  {
    printf("realloc fail\n");
    exit(-1);
  }
  ps->capacity = newCapacity;//新的空间赋给旧的
  }
  ps->a[ps->top] = x;//栈顶插入x;
  ps->top++;//top++
}
void StackPop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
  --ps->top;//top--就相当于删除操作
}
bool StackEmpty(ST* ps)
{
  assert(ps);
  //两种写法
  //if (ps->top > 0)
  //{
  //  return false;
  //}
  //else
  //{
  //  return true;
  //}
  return ps->top == 0;
}
STDataType StackTop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
  return ps->a[ps->top - 1];//访问栈顶元素(这里因为top我们设为0,所以访问栈顶元素相当于top-1
}
int StackSize(ST* ps)
{
  assert(ps);
  return ps->top;
}


2.2数组模拟静态栈


1669439532720.jpg

#include<iostream>
using namespace std;
const int N = 1e6 + 10;
int n;
int stk[N];
int top = - 1;
int main ()
{
    cin >> n;
    while(n --)
    {
        string s;
        cin >> s;
        if(s == "push")
        {
            int a;
            cin >> a;
            stk[++top] = a;
        }
        if(s == "pop")
        {
            top--;
        }
        if(s == "empty")
        {
            if(top >= 0) printf("NO\n");
            else printf("YES\n");
        }
        if(s == "query")
        {
            printf("%d\n", stk[top]);
        }
    }
    return 0;
}


三、 队列的实现(动态)和模拟静态队列


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


1669439558653.jpg


3.1 实现一个支持动态增长的栈


头文件:

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int QDataType;//方便改类型
typedef struct QueueNode//保存每个节点的数据
{
  QDataType data;
  struct QueueNode* next;
}QNode;
typedef struct Queue
{
  QNode* head;
  QNode* tail;
}Queue;
//上面的写法等价于:
//typedef struct Queue
//{
//  QNode* head;
//  QNode* tail;
//};
//
//typedef struct Queue 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);//size_t相当于Unsinged int
QDataType QueueFront(Queue* pq);
QDataType QueueBack(Queue* pq);


源文件:


#include"Queue.h"
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);//free掉cur指针
  cur = next;//cur赋值到下一个位置
  }
  pq->head = pq->tail = NULL;//置空
}
void QueuePush(Queue* pq, QDataType x)//队尾插入//插入int类型的参数
{
  assert(pq);
  QNode* newnode = (QNode*)malloc(sizeof(QNode));
  assert(newnode);
  newnode->data = x;//新的节点的值被赋与x
  newnode->next = NULL;//新的节点是在队尾,所以指向的下一个位置是空
  if (pq->tail == NULL)//如果链表的第一个值为空,则head = 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;//QNode* next相当于是QDataType的头指针的下一个位置
  free(pq->head);
  pq->head = next;//头往后走
  }
}
bool QueueEmpty(Queue* pq)
{
  assert(pq);
  //return pq->head == NULL && pq->tail == NULL;
  return pq->head == NULL;//程序调试了快一个小时就是因为pq->head没加后面的== NULL
}
size_t QueueSize(Queue* pq)//size_t相当于Unsinged int
{
  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;
}


3.2数组模拟静态队列


1669439620642.jpg

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int q[N];
int n;
int hh ,tt = -1;//hh表示头,tt表示尾
int main ()
{
    cin >> n;
    while(n --)
    {
        string s;
        cin >> s;
        if(s == "push")
        {
            int x;
            cin >> x;
            q[++tt] = x;
        }
        else if(s == "pop")
        {
            hh ++;
        }
        else if(s == "empty")
            {
                if(hh <= tt) printf("NO\n");//尾在逻辑上要比头更前面
                else printf("YES\n");
            }
        else cout << q[hh] << endl;
    }
    return 0;
}


四、leetcode-栈实现队列和用队列实现栈


225. 用队列实现栈 - 力扣(LeetCode)


1669439648911.jpg


代码:

typedef int QDataType;
typedef struct QueueNode//保存每个节点的数据
{
  QDataType data;
  struct QueueNode* next;
}QNode;
typedef struct Queue
{
  QNode* head;
  QNode* tail;
}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);//size_t相当于Unsinged int
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);//free掉cur指针
  cur = next;//cur赋值到下一个位置
  }
  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)//如果链表的第一个值为空,则head = 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;//QNode* next相当于是QDataType的头指针的下一个位置
  free(pq->head);
  pq->head = next;//头往后走
  }
}
bool QueueEmpty(Queue* pq)
{
  assert(pq);
  //return pq->head == NULL && pq->tail == NULL;
  return pq->head == NULL;//程序调试了快一个小时就是因为pq->head没加后面的== NULL
}
size_t QueueSize(Queue* pq)//size_t相当于Unsinged int
{
  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;
}
typedef struct {
    Queue q1;
    Queue q2;
} MyStack;
MyStack* myStackCreate() {
    MyStack* pst = (MyStack*)malloc(sizeof(MyStack));
    assert(pst);
    QueueInit(&pst->q1);
    QueueInit(&pst->q2);
    return pst;
}
void myStackPush(MyStack* obj, int x) {
    assert(obj);
    if(!QueueEmpty(&obj->q1))
    {
        QueuePush(&obj->q1, x);
    }
    else
    {
        QueuePush(&obj->q2, x);
    }
}
int myStackPop(MyStack* obj) {
    Queue* emptyQ = &obj->q1;//假设q1为空,q2不为空
    Queue* nonEmptyQ = &obj->q2;
    if(!QueueEmpty(&obj->q1))
    {
        emptyQ = &obj->q2;
        nonEmptyQ = &obj->q1;
    }
    //把非空队列的前N个数据导入空队列,剩下一个删掉
    //就实现了后进先出
    while(QueueSize(nonEmptyQ) > 1)
    {
        QueuePush(emptyQ, QueueFront(nonEmptyQ));
        QueuePop(nonEmptyQ);
    }
    int top = QueueFront(nonEmptyQ);//此时那个非空的队列只剩下一个数据
    QueuePop(nonEmptyQ);
    return top;
}
int myStackTop(MyStack* obj) {
    assert(obj);
      if(!QueueEmpty(&obj->q1))//如果q1不为空
    {
     return QueueBack(&obj->q1);  
    }
    else
    {
     return QueueBack(&obj->q2);  
    }
}
bool myStackEmpty(MyStack* obj) {
    return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2);
}
void myStackFree(MyStack* obj) {
    assert(obj); 
    QueueDestory(&obj->q1);
    QueueDestory(&obj->q2);
    free(obj);
}


232. 用栈实现队列 - 力扣(LeetCode)栈是后进先出


思路:设计两个栈,一个栈专门用来入数据,一个栈专门用来出数据。

1669439684975.jpg

typedef int STDataType;
typedef struct Stack//动态链表
{
  int *a;
  int top;//栈顶的位置
  int capacity;//容量
}ST;
STDataType StackTop(ST* ps);
void StackInit(ST* ps);//初始化栈
void StackDestory(ST* ps);
void StackPop(ST* ps);
void StackPush(ST* ps, STDataType x);
bool StackEmpty(ST* ps);
void StackInit(ST* ps)
{
  assert(ps);
  ps->a = NULL;
  ps->top = 0;
  ps->capacity = 0;
}
void StackDestory(ST* ps)
{
  assert(ps);
  free(ps->a);
  ps->a = NULL;
  ps->capacity = ps->top = 0;
}
void StackPush(ST* ps, STDataType x)//入数据
{
  assert(ps);
  //满了就扩容
  if (ps->top == ps->capacity)
  {
  int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
  ps->a = (STDataType*)realloc(ps->a, newCapacity * sizeof(STDataType));
  if (ps->a == NULL)
  {
    printf("realloc fail\n");
    exit(-1);
  }
  ps->capacity = newCapacity;
  }
  ps->a[ps->top] = x;
  ps->top++;
}
void StackPop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
  --ps->top;
}
bool StackEmpty(ST* ps)
{
  assert(ps);
  //两种写法
  //if (ps->top > 0)
  //{
  //  return false;
  //}
  //else
  //{
  //  return true;
  //}
  return ps->top == 0;
}
STDataType StackTop(ST* ps)
{
  assert(ps);
  assert(ps->top > 0);
  return ps->a[ps->top - 1];//访问栈顶元素
}
int StackSize(ST* ps)
{
  assert(ps);
  return ps->top;
}
typedef struct
{
    ST pushST;
    ST popST;
} MyQueue;
MyQueue* myQueueCreate() {
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    assert(obj);
    StackInit(&obj->pushST);//&符要加,要改变结构体里面的内容
    StackInit(&obj->popST);
    return obj;
}
void myQueuePush(MyQueue* obj, int x) {
    assert(obj);
    StackPush(&obj->pushST, x);
}
int myQueuePop(MyQueue* obj) {
    assert(obj);
    //如果popST为空, 把pushST的数据拿过来,就符合先进先出的顺序了
    if(StackEmpty(&obj->popST))//如果ST Pop为空就执行
    {
        while(!StackEmpty(&obj->pushST))
        {
            StackPush(&obj->popST, StackTop(&obj->pushST));
            StackPop(&obj->pushST);//把pushST里的数据删掉
        }
    }
    int front = StackTop(&obj->popST);//记录栈顶的数据
    StackPop(&obj->popST);
    return front;
}
int myQueuePeek(MyQueue* obj) {
    assert(obj);
    //如果popST为空, 把pushST的数据拿过来,就符合先进先出的顺序了
    if(StackEmpty(&obj->popST))//如果ST Pop为空就执行
    {
        while(!StackEmpty(&obj->pushST))
        {
            StackPush(&obj->popST, StackTop(&obj->pushST));
            StackPop(&obj->pushST);//把pushST里的数据删掉
        }
    }
    return StackTop(&obj->popST);
}
bool myQueueEmpty(MyQueue* obj) {
        assert(obj);
    return StackEmpty(&obj->pushST)&&StackEmpty(&obj->popST);
}
void myQueueFree(MyQueue* obj) {
    assert(obj);
    StackDestory(&obj->pushST);
    StackDestory(&obj->popST);
    free(obj);
}
相关文章
|
5月前
|
编译器 C语言 C++
栈区的非法访问导致的死循环(x64)
这段内容主要分析了一段C语言代码在VS2022中形成死循环的原因,涉及栈区内存布局和数组越界问题。代码中`arr[15]`越界访问,修改了变量`i`的值,导致`for`循环条件始终为真,形成死循环。原因是VS2022栈区从低地址到高地址分配内存,`arr`数组与`i`相邻,`arr[15]`恰好覆盖`i`的地址。而在VS2019中,栈区先分配高地址再分配低地址,因此相同代码表现不同。这说明编译器对栈区内存分配顺序的实现差异会导致程序行为不一致,需避免数组越界以确保代码健壮性。
105 0
栈区的非法访问导致的死循环(x64)
232.用栈实现队列,225. 用队列实现栈
在232题中,通过两个栈(`stIn`和`stOut`)模拟队列的先入先出(FIFO)行为。`push`操作将元素压入`stIn`,`pop`和`peek`操作则通过将`stIn`的元素转移到`stOut`来实现队列的顺序访问。 225题则是利用单个队列(`que`)模拟栈的后入先出(LIFO)特性。通过多次调整队列头部元素的位置,确保弹出顺序符合栈的要求。`top`操作直接返回队列尾部元素,`empty`判断队列是否为空。 两题均仅使用基础数据结构操作,展示了栈与队列之间的转换逻辑。
|
10月前
|
存储 C语言 C++
【C++数据结构——栈与队列】顺序栈的基本运算(头歌实践教学平台习题)【合集】
本关任务:编写一个程序实现顺序栈的基本运算。开始你的任务吧,祝你成功!​ 相关知识 初始化栈 销毁栈 判断栈是否为空 进栈 出栈 取栈顶元素 1.初始化栈 概念:初始化栈是为栈的使用做准备,包括分配内存空间(如果是动态分配)和设置栈的初始状态。栈有顺序栈和链式栈两种常见形式。对于顺序栈,通常需要定义一个数组来存储栈元素,并设置一个变量来记录栈顶位置;对于链式栈,需要定义节点结构,包含数据域和指针域,同时初始化栈顶指针。 示例(顺序栈): 以下是一个简单的顺序栈初始化示例,假设用C语言实现,栈中存储
470 77
|
9月前
|
算法 调度 C++
STL——栈和队列和优先队列
通过以上对栈、队列和优先队列的详细解释和示例,希望能帮助读者更好地理解和应用这些重要的数据结构。
221 11
|
9月前
|
DataX
☀☀☀☀☀☀☀有关栈和队列应用的oj题讲解☼☼☼☼☼☼☼
### 简介 本文介绍了三种数据结构的实现方法:用两个队列实现栈、用两个栈实现队列以及设计循环队列。具体思路如下: 1. **用两个队列实现栈**: - 插入元素时,选择非空队列进行插入。 - 移除栈顶元素时,将非空队列中的元素依次转移到另一个队列,直到只剩下一个元素,然后弹出该元素。 - 判空条件为两个队列均为空。 2. **用两个栈实现队列**: - 插入元素时,选择非空栈进行插入。 - 移除队首元素时,将非空栈中的元素依次转移到另一个栈,再将这些元素重新放回原栈以保持顺序。 - 判空条件为两个栈均为空。
|
9月前
|
定位技术 C语言
c语言及数据结构实现简单贪吃蛇小游戏
c语言及数据结构实现简单贪吃蛇小游戏
|
10月前
|
搜索推荐 C语言
数据结构(C语言)之对归并排序的介绍与理解
归并排序是一种基于分治策略的排序算法,通过递归将数组不断分割为子数组,直到每个子数组仅剩一个元素,再逐步合并这些有序的子数组以得到最终的有序数组。递归版本中,每次分割区间为[left, mid]和[mid+1, right],确保每两个区间内数据有序后进行合并。非递归版本则通过逐步增加gap值(初始为1),先对单个元素排序,再逐步扩大到更大的区间进行合并,直至整个数组有序。归并排序的时间复杂度为O(n*logn),空间复杂度为O(n),且具有稳定性,适用于普通排序及大文件排序场景。
|
10月前
|
C++
【C++数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】
【数据结构——栈和队列】括号配对(头歌实践教学平台习题)【合集】(1)遇到左括号:进栈Push()(2)遇到右括号:若栈顶元素为左括号,则出栈Pop();否则返回false。(3)当遍历表达式结束,且栈为空时,则返回true,否则返回false。本关任务:编写一个程序利用栈判断左、右圆括号是否配对。为了完成本关任务,你需要掌握:栈对括号的处理。(1)遇到左括号:进栈Push()开始你的任务吧,祝你成功!测试输入:(()))
231 7
|
C语言
【数据结构】栈和队列(c语言实现)(附源码)
本文介绍了栈和队列两种数据结构。栈是一种只能在一端进行插入和删除操作的线性表,遵循“先进后出”原则;队列则在一端插入、另一端删除,遵循“先进先出”原则。文章详细讲解了栈和队列的结构定义、方法声明及实现,并提供了完整的代码示例。栈和队列在实际应用中非常广泛,如二叉树的层序遍历和快速排序的非递归实现等。
1000 9
|
存储 算法
非递归实现后序遍历时,如何避免栈溢出?
后序遍历的递归实现和非递归实现各有优缺点,在实际应用中需要根据具体的问题需求、二叉树的特点以及性能和空间的限制等因素来选择合适的实现方式。
270 59

热门文章

最新文章