• [C/C++]数据结构 栈和队列()


    一:栈

    1.1 栈的概念及结构

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

    •         压栈:栈的插入操作叫做进栈/压栈/入栈,将数据插入栈顶
    •         出栈:栈的删除操作也叫出栈,出数据也在栈顶

    1.2栈的实现

            栈的实现一般可以用数组或者链表实现,相对而言数组的结构更优一点,因为数组在尾上插入数据的代价更小 ,链表则需从头遍历到尾

    支持动态增长的栈:

    1. typedef int STDataType;
    2. typedef struct stack
    3. {
    4. int* a;
    5. int top; //用于维护栈顶
    6. int capacity;//栈的容量
    7. }ST;

    常用功能接口:

    1. //栈的初始化
    2. void STInit(ST* ps);
    3. //压栈
    4. void STPush(ST* ps,STDataType x);
    5. //出栈
    6. void STPop(ST* ps);
    7. //取栈顶元素
    8. STDataType STTop(ST* ps);
    9. //判断栈是否为空
    10. bool STEmpty(ST* ps);
    11. //求栈的大小
    12. int STSize(ST* ps);
    13. //摧毁栈
    14. void STDestroy(ST* ps);

    1.栈的初始化

            要注意栈结构中的top可以初始化为0也可以初始化为-1,这里以初始化为0为例

    • 初始化为0: top的值可以表示栈元素的个数
    • top初始化位-1: top指向栈顶元素
    1. void STInit(ST* ps)
    2. {
    3. assert(ps);
    4. ps->a = NULL;
    5. ps->capacity = 0;
    6. ps->top = 0;
    7. }

    2.压栈

    1. void STPush(ST* ps, STDataType x)
    2. {
    3. assert(ps);
    4. //扩容
    5. if (ps->top == ps->capacity)
    6. {
    7. int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
    8. STDataType* ret = (STDataType*)realloc(ps->a,sizeof(STDataType)*newcapacity);
    9. if (ret == NULL)
    10. {
    11. perror("realloc");
    12. return;
    13. }
    14. ps->a = ret;
    15. ps->capacity = newcapacity;
    16. }
    17. ps->a[ps->top] = x;
    18. ps->top++;
    19. }

    3.出栈

    1. void STPop(ST* ps)
    2. {
    3. assert(ps);
    4. assert(ps->top); //确保栈中还有元素
    5. ps->top--;
    6. }

    4.取栈顶元素

    1. STDataType STTop(ST* ps)
    2. {
    3. assert(ps);
    4. assert(ps->top);
    5. return ps->a[ps->top - 1];
    6. }

    5.判断栈是否为空

    1. bool STEmpty(ST* ps)
    2. {
    3. assert(ps);
    4. return ps->top == 0;
    5. }

    6.求栈的大小

    1. int STSize(ST* ps)
    2. {
    3. assert(ps);
    4. return ps->top;
    5. }

    7.摧毁栈

    1. void STDestroy(ST* ps)
    2. {
    3. assert(ps);
    4. free(ps->a);
    5. ps->a == NULL;
    6. ps->top = ps->capacity = 0;
    7. }

    二. 队列

    2.1 队列的概念及结构

            队列只允许一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表,队列具有先进先出的特点,进行插入操作的一端称为队尾,进行删除操作的一端称为队头.

    2.2 队列的实现

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

    队列的结构:

    1. typedef int QDataType;
    2. //链式结构:表示队列
    3. typedef struct QueueNode {
    4. QDataType x;
    5. struct QueueNode* next;
    6. }Node;
    7. //队列的结构:队头和队尾分别用head和tail指针维护
    8. typedef struct Queue
    9. {
    10. Node* head;
    11. Node* tail;
    12. int size;
    13. }Queue;

    接口:

    1. //队列的初始化
    2. void QueueInit(Queue* ps);
    3. //入队列
    4. void QueuePush(Queue* ps,QDataType x);
    5. //出队列
    6. void QueuePop(Queue* ps);
    7. //判断队列是否为空
    8. bool QueueEmpty(Queue* ps);
    9. //取队头元素
    10. QDataType QueueFront(Queue* ps);
    11. //取队尾元素
    12. QDataType QueueTail(Queue* ps);
    13. //求队列大小
    14. int QueueSize(Queue* ps);
    15. //摧毁队列
    16. void QueueDestory(Queue* ps);

    1.队列的初始化

    1. void QueueInit(Queue* ps)
    2. {
    3. assert(ps);
    4. ps->head = ps->tail = NULL;
    5. ps->size = 0;
    6. }

    2.入队列

    1. void QueuePush(Queue* ps, QDataType x)
    2. {
    3. assert(ps);
    4. //创建新节点
    5. Node* newnode = (Node*)malloc(sizeof(Node));
    6. if (newnode == NULL)
    7. {
    8. perror("malloc");
    9. return;
    10. }
    11. newnode->next = NULL;
    12. newnode->x = x;
    13. //尾插
    14. if (ps->tail == NULL)
    15. {
    16. ps->head = ps->tail = newnode;
    17. }
    18. else
    19. {
    20. ps->tail->next = newnode;
    21. ps->tail = ps->tail->next;
    22. }
    23. ps->size++;
    24. }

    3.出队列

    1. void QueuePop(Queue* ps)
    2. {
    3. assert(ps);
    4. assert(ps->head);
    5. if (ps->head->next == NULL)
    6. {
    7. ps->head = ps->tail = NULL;
    8. }
    9. else
    10. {
    11. Node* next = ps->head->next;
    12. free(ps->head);
    13. ps->head = next;
    14. }
    15. ps->size--;
    16. }

    4.判断队列是否为空

    1. bool QueueEmpty(Queue* ps)
    2. {
    3. assert(ps);
    4. return ps->tail == NULL;
    5. }

    5.取队头元素

    1. QDataType QueueFront(Queue* ps)
    2. {
    3. assert(ps);
    4. assert(ps->head);
    5. return ps->head->x;
    6. }

    6.取队尾元素

    1. QDataType QueueTail(Queue* ps)
    2. {
    3. assert(ps);
    4. assert(ps->tail);
    5. return ps->tail->x;
    6. }

    7.求队列大小

    1. int QueueSize(Queue* ps)
    2. {
    3. assert(ps);
    4. return ps->size;
    5. }

    8.摧毁队列

    1. void QueueDestory(Queue* ps)
    2. {
    3. assert(ps);
    4. Node* cur = ps->head;
    5. while (cur)
    6. {
    7. Node* next = cur->next;
    8. free(cur);
    9. cur = next;
    10. }
    11. ps->head=ps->tail = NULL;
    12. }


     

  • 相关阅读:
    「津津乐道播客」#386 原汤话原食:谁还不是个“白字先生”?
    spring-01-依赖注入
    SOC验证环境的启动方式
    第6周学习:Vision Transformer & Swin Transformer
    Hafnium之分区运行时模型
    Python2与Python3的字符编码
    MyBatis中的ResultMap有什么作用
    Vue学习:Vue中的数据代理
    EmguCV-C#版本Opencv图像识别和处理
    《Linux》day2--tmux和vim
  • 原文地址:https://blog.csdn.net/m0_74910646/article/details/134477080