2、數據結構之棧、隊列、循環隊列

2、數據結構之棧、隊列、循環隊列

順序棧

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;
          }
      }
相關文章
相關標籤/搜索