栈、队列常规操作

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

 

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!\n");
        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 \n",StackTop(&stack));
    printf("StackSize:%d \n",StackSize(&stack));
    printf("StackTop:%d\n",StackTop(&stack));
    printf("StackEmpty?:%d\n",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 \n",QueueFront(&q));
    printf("QueueBack: %d\n",QueueBack(&q));
    printf("QueueSize:%d\n",QueueSize(&q));
QueuePop(&q);
QueuePop(&q); printf(
"QueueEmpty?:%d\n",QueueEmpty(&q)); }

简单测试一下:

 

 

posted @ 2018-01-09 16:44  tp_16b  阅读(411)  评论(0编辑  收藏  举报