数据结构之栈队列循环队列

Posted black-cobra

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构之栈队列循环队列相关的知识,希望对你有一定的参考价值。

二、数据结构之栈、队列、循环队列

顺序栈

Stack.h

  • 结构类型,函数声明:
  •   #ifndef  _STACK_H_
      #define _STACK_H_
    
      typedef int SElementType;
    
      ///顺序栈
      #define STACK_INIT_SIZE 20
      #define STACK_INCREMENT 10
    
      typedef struct {
          SElementType * base;
          SElementType * top;
          int stackSize;///当前栈的大小
      }SqStack;///SequenceStack
    
      ///function--
      void InitStack(SqStack * pSqStack);
      void DestoryStack(SqStack * pSqStack);
      void ClearStack(SqStack * pSqStack);
      int IsStackEmpty(SqStack * pSqStack);
      int GetStackLength(SqStack * pSqStack);
      void GetStackTop(SqStack * pSqStack, SElementType * elem);
      void PushStack(SqStack * pSqStack, SElementType elem);
      void PopStack(SqStack * pSqStack, SElementType * elem);
    
      #endif // ! _STACK_H_

Stack.cpp

  • 实现:
  •   #include <stdio.h>
      #include <stdlib.h>
    
      #include "Stack.h"
    
      ///初始化栈,创建容量为STACK_INIT_SIZE大小的栈
      void InitStack(SqStack * pSqStack) {
          pSqStack->base = (SElementType *)malloc(STACK_INIT_SIZE * sizeof(SElementType));
          pSqStack->top = pSqStack->base;
          pSqStack->stackSize = STACK_INIT_SIZE;
      }
    
      ///销毁栈
      void DestoryStack(SqStack * pSqStack) {
          if (pSqStack->base != NULL) {
              free(pSqStack->base);
              pSqStack->base = NULL;
              pSqStack->top = NULL;
              pSqStack->stackSize = 0;
          }
      }
    
      ///清空栈
      void ClearStack(SqStack * pSqStack) {
          pSqStack->top = pSqStack->base;
      }
    
      ///判断栈是否为空
      int IsStackEmpty(SqStack * pSqStack) {
          int iRet = -1;
          if (pSqStack->base == pSqStack->top) {
              iRet = 0;
          }
          return iRet;
      }
    
      ///获取栈中元素长度
      int GetStackLength(SqStack * pSqStack) {
          int iRet = -1;
          int length = pSqStack->top - pSqStack->base;
          iRet = length;
          return iRet;
      }
    
      ///获取栈顶元素,但不出栈
      void GetStackTop(SqStack * pSqStack, SElementType * elem) {
          if (pSqStack->base != pSqStack->top) {
              *elem = *(pSqStack->top - 1);
          }
      }
    
      ///压栈
      void PushStack(SqStack * pSqStack, SElementType elem) {
          if (pSqStack->top - pSqStack->base >= pSqStack->stackSize) {
              pSqStack->base = (SElementType *)realloc(pSqStack->base, (pSqStack->stackSize + STACK_INCREMENT) * sizeof(SElementType));
              pSqStack->stackSize += STACK_INCREMENT;
              pSqStack->top = pSqStack->base + pSqStack->stackSize;
          }
          *pSqStack->top++ = elem;
      }
    
      ///出栈
      void PopStack(SqStack * pSqStack, SElementType * elem) {
          if (pSqStack->base != pSqStack->top) {
              *elem = *(--pSqStack->top);
          }
      }

队列

Queue.h

  • 数据结构、函数声明:
  •   #ifndef _QUEUE_H_
      #define _QUEUE_H_
    
      typedef int QElementType;
    
      ///结点结构类型
      typedef struct QNode {
          QElementType elem;
          QNode * next;
      }QNode, * QueuePtr;
    
      ///链表结构
      typedef struct QLink {
          QueuePtr front; //头指针
          QueuePtr rear; //尾指针
      }LinkQueue;
    
    
      ///function--
      void InitLinkQueue(LinkQueue * plQueue);
      void DestoryLinkQueue(LinkQueue * plQueue);
      void ClearLinkQueue(LinkQueue * plQueue);
      int IsLinkQueueEmpty(LinkQueue * plQueue);
      int GetLinkQueueLength(LinkQueue * plQueue);
      void EnLinkQueue(LinkQueue * plQueue, QElementType elem);
      void DeLinkQueue(LinkQueue * plQueue, QElementType * elem);
    
    
      //////////////////////////////////////////////////////
      #define SEQUENCE_QUEUE_INIT_LENGTH 3
      ///循环队列CircularQueue
      typedef struct {
          QElementType * base;//用来保存入队数据
          int front;
          int rear;
      }SqCirQueue;
    
      void InitSqCircularQueue(SqCirQueue * pSqCirQueue);
      int GetSqCircularQueueLength(SqCirQueue * pSqCirQueue);
      int IsSqCircularQueueEmpty(SqCirQueue * pSqCirQueue);
      void EnSqCircularQueue(SqCirQueue * pSqCirQueue, QElementType elem);
      void DeSqCircularQueue(SqCirQueue * pSqCirQueue, QElementType * elem);
    
      #endif // !_QUEUE_H_

Queue.cpp

  • 实现:
  •   #include <stdio.h>
      #include <stdlib.h>
    
      #include "Queue.h"
    
      //static LinkQueue plQueue;
    
      ///返回只有头结点的队列
      void InitLinkQueue(LinkQueue * plQueue) {
          plQueue->front = plQueue->rear = (QueuePtr)malloc(sizeof(QNode));
          plQueue->front->next = NULL;
          plQueue->front->elem = 0;
      }
    
      ///销毁队列
      void DestoryLinkQueue(LinkQueue * plQueue) {
          while (plQueue->front) {
              plQueue->rear = plQueue->front->next;
              free(plQueue->front);
              plQueue->front = plQueue->rear;
          }
          plQueue->front = plQueue->rear = NULL;
      }
    
      ///清空队列,即只保留头结点
      void ClearLinkQueue(LinkQueue * plQueue) {
          QueuePtr head = plQueue->front;
          plQueue->front = plQueue->front->next;
          while (plQueue->front != NULL) {
              plQueue->rear = plQueue->front->next;
              free(plQueue->front);
              plQueue->front = plQueue->rear;
          }
          plQueue->front = plQueue->rear = head;
          plQueue->front->next = NULL;
          plQueue->front->elem = 0;
      }
    
      ///队列的头始终指向链表的头结点
      int IsLinkQueueEmpty(LinkQueue * plQueue) {
          int iRet = -1;
          if (plQueue->front == plQueue->rear) {
              iRet = 0;
          }
          return iRet;
      }
    
      ///队列头结点保存队列中元素的数目
      int GetLinkQueueLength(LinkQueue * plQueue) {
          return plQueue->front->elem;
      }
    
      /// 入队
      void EnLinkQueue(LinkQueue * plQueue,QElementType elem) {
          if (plQueue->front != NULL &&plQueue->rear != NULL) {
              QueuePtr temp = (QueuePtr)malloc(sizeof(QNode));
              temp->elem = elem;
              temp->next = NULL;
    
              plQueue->rear->next = temp;
              plQueue->rear = plQueue->rear->next;
    
              plQueue->front->elem++;
          }
      }
    
      ///出队
      void DeLinkQueue(LinkQueue * plQueue, QElementType * elem) {
          if (plQueue->front->elem > 0) {
              QueuePtr temp = plQueue->front->next;
              *elem = temp->elem;
              plQueue->front->next = temp->next;
              plQueue->front->elem--;
              free(temp);
          }
      }
    
      ///初始化循环队列,0位置不存储元素,作为队列为空的标志
      void InitSqCircularQueue(SqCirQueue * pSqCirQueue) {
          pSqCirQueue->base = (QElementType *)malloc(SEQUENCE_QUEUE_INIT_LENGTH * sizeof(QElementType));
          pSqCirQueue->front = 0;
          pSqCirQueue->rear = 0;
      }
    
      ///获取队列长度,实际模长为SEQUENCE_QUEUE_INIT_LENGTH-1其中有一个位置为标志位
      ///队列头指针在队列尾指针的下一位表示队列已满
      int GetSqCircularQueueLength(SqCirQueue * pSqCirQueue) {
          return (pSqCirQueue->rear - pSqCirQueue->front + SEQUENCE_QUEUE_INIT_LENGTH) % SEQUENCE_QUEUE_INIT_LENGTH;
      }
    
      ///判断队列是否为空
      int IsSqCircularQueueEmpty(SqCirQueue * pSqCirQueue) {
          int iRet = -1;
          if (pSqCirQueue->front == pSqCirQueue->rear) {
              iRet = 0;
          }
          return iRet;
      }
    
      ///入队
      void EnSqCircularQueue(SqCirQueue * pSqCirQueue, QElementType elem) {
          if ((pSqCirQueue->rear + 1) % SEQUENCE_QUEUE_INIT_LENGTH != pSqCirQueue->front) {    //判断队列是否满
              pSqCirQueue->base[pSqCirQueue->rear++] = elem;
              pSqCirQueue->rear = pSqCirQueue->rear % SEQUENCE_QUEUE_INIT_LENGTH;
          }
      }
    
      ///出队
      void DeSqCircularQueue(SqCirQueue * pSqCirQueue, QElementType * elem) {
          if (pSqCirQueue->front != pSqCirQueue->rear) {//判断队列是否为空
              *elem = pSqCirQueue->base[pSqCirQueue->front++];
              pSqCirQueue->front %= SEQUENCE_QUEUE_INIT_LENGTH;
          }
      }

以上是关于数据结构之栈队列循环队列的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode题解——数据结构之栈和队列

数据结构之栈和队列熬夜暴肝,有亿点详细

4-7 Python数据结构常考题之栈与队列

剑指Offer数据结构之栈和队列[Python版]

剑指Offer数据结构之栈和队列[Python版]

数据结构之栈与队列