zoukankan      html  css  js  c++  java
  • 栈、队列常规操作

    当前学习数据结构内容,由于后面常常需要用到栈和队列,为方便后面学习,在此做个记录。

    StackQueue.h

    #ifndef _STACKQUEUE_H_
    #define _STACKQUEUE_H_ typedef int SDataType; typedef struct Stack { SDataType* _array; size_t _top; //栈顶 size_t _end; }Stack; typedef int QDataType; typedef struct QueueNode { QDataType _data; struct QueueNode* _next; }QueueNode; typedef struct Queue { QueueNode* _head; QueueNode* _tail; }Queue; void StackInit(Stack* s); void StackPush(Stack* s, SDataType x); void StackPop(Stack* s); SDataType StackTop(Stack* s); size_t StackSize(Stack* s); int StackEmpty(Stack* s); void QueueInit(Queue* q); void QueuePush(Queue* q, QDataType x); void QueuePop(Queue* q); QDataType QueueFront(Queue* q); QDataType QueueBack(Queue* q); size_t QueueSize(Queue* q); int QueueEmpty(Queue* q); #endif
    StackQueue.c
    
    #include<stdio.h>
    #include "StackQueue.h"
    #include<assert.h>
    #include<malloc.h> 
    #define MAXSTACKSIZE 5 void StackInit(Stack* s)
    { assert(s);
      s->_array = NULL;
      s->_top = 0;
       s->_end = 0;
    }
    void StackPush(Stack* s, SDataType x){
        assert(s);

          if(s->_top == s->_end)
          {
          size_t capacity = s->_end > 0? s->_end*2 : 4;
          DataType* p = (DataType*)realloc(s->_array, sizeof(DataType) * capacity);
          assert(p);
          s->_array = p;
          s->_end = capacity;
        }
        s->_array[s->_top++] = x;

    void StackPop(Stack* s){
        assert(s);
        
        if(s->_top == 0){
            printf("Stack Is Empty!
    ");
            return ;
        }
        s->_top--;
    } 
    SDataType StackTop(Stack* s){
        assert(s);
        
        return s->_array[s->_top];
    } 
    size_t StackSize(Stack* s){
        assert(s);
        
        return s->_top;
    }
    int StackEmpty(Stack* s){
        assert(s);
        
        if(s->_top == 0) return 0;
        else return 1;
    } 
    
    void TestStack(){
        Stack stack;
        StackInit(&stack);
        
        StackPush(&stack,1);
        StackPush(&stack,2);
        StackPush(&stack,3);
        StackPush(&stack,4);
        StackPush(&stack,5);
        StackPush(&stack,6);
        StackPush(&stack,7);
        
        StackPop(&stack);
        
        printf("Top:%d 
    ",StackTop(&stack));
        printf("StackSize:%d 
    ",StackSize(&stack));
        printf("StackTop:%d
    ",StackTop(&stack));
        printf("StackEmpty?:%d
    ",StackEmpty(&stack));
    }
    
    
    ///////////////////////////////////////////////////
     
    void QueueInit(Queue* q){
        assert(q);
        
        q->_head = NULL;
        q->_tail = NULL;
    }
    void QueuePush(Queue* q, QDataType x){
        assert(q);
        
        QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
        newNode->_next = NULL;    
        if(newNode == NULL){
            perror("malloc fail");
            exit(1);
        }
        newNode->_data = x;
        
        if(q->_tail == NULL)
            q->_head = q->_tail = newNode; 
        else{
            q->_tail->_next = newNode;
            q->_tail = newNode;
        }
    } 
    void QueuePop(Queue* q){
        assert(q);
        
        if (q->_head == q->_tail)
        {
            if (q->_head)
            {
                free(q->_head);
                q->_head = q->_tail = NULL;
            }
        }
        else
        {
            QueueNode* next = q->_head->_next;
            free(q->_head);
            q->_head = next;
        }
    } 
    QDataType QueueFront(Queue* q){
        assert(q && q->_head);        //q->_head不能为NULL
        
        return q->_head->_data;
    }
    QDataType QueueBack(Queue* q){
        assert(q && q->_tail);       //q->_tail不能为NULL
        
        return q->_tail->_data;
    } 
    size_t QueueSize(Queue* q){
        assert(q);
        
        size_t count = 0;
        QueueNode* cur = q->_head;
            while(cur ){
                cur = cur->_next;
                count++;
        }
        return count;
    } 
    int QueueEmpty(Queue* q){
        assert(q);
        
        if(q->_head == NULL){
            return 1;
        }
        else
        return 0;
    } 
    
    void TestQueue(){
        Queue q;
        QueueInit(&q);
        
        QueuePush(&q, 1);
        QueuePush(&q, 2);
        QueuePush(&q, 3);
        QueuePop(&q);
        QueuePop(&q);        
        printf("QueueFront:%d 
    ",QueueFront(&q));
        printf("QueueBack: %d
    ",QueueBack(&q));
        printf("QueueSize:%d
    ",QueueSize(&q));
    QueuePop(&q);
    QueuePop(&q); printf(
    "QueueEmpty?:%d ",QueueEmpty(&q)); }

    简单测试一下:

     

  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/tp-16b/p/8252253.html
Copyright © 2011-2022 走看看