C实现栈与队列

C实现栈与队列

做了个栈和队列的基础demo,写得比较快,就没有什么注释,其实看各个函数的名字就可以知道函数的作用了。

栈的实现

#include <stdio.h>
#include <stdlib.h>

typedef struct stack{
    int *nums;
    int top;
    int size;
}*stack;

void changeSize(stack s,int size){
    int *p = (int *)realloc(s->nums,size*sizeof(int));
    s->nums = p;
    s->size = size;
    printf("Size has changed\n");
}

void push(stack s, int x){
    if(s->top < s->size - 1){
        s->top ++;
    }else{
        printf("The stack is full\n");
        changeSize(s,2*s->size);
    }
    s->nums[s->top] = x;
}

void pop(stack s){
    if(s->top < 0){
        printf("No enough data\n");
        exit(0);
    }
    s->top--;
}

int peek(stack s){
    if(s->top < 0){
        printf("The stack is empty");
        return 0;
    }
    return s->nums[s->top];
}

_Bool isempty(stack s){
    if(s->top == -1){
        return 0;
    }else{
        return 1;
    }
}

void clearstack(stack s){
    free(s->nums);
    s->nums = NULL;
    s->top = -1;
    s->size = 0;
}

int main(){
    stack s = (stack)malloc(sizeof(struct stack));
    int size = 10;
    if(size < 1){
        printf("Error in size\n");
        exit(0);
    }
    s->nums = (int *)malloc(sizeof(int)*size);
    s->top = -1;
    s->size = size;

    int a[12] = {3,2,1,4,6,5,8,7,0,9,6,4};
    for(int i = 0; i < 12; i++){
        push(s,a[i]);
        printf("The num on the top is %d\n",peek(s));
    }
    pop(s);
    printf("The num on the top after pop  is %d\n",peek(s));
    printf("The stack is %s\n",isempty==0?"empty":"not empty");
    clearstack(s);
}

队列

#include <stdio.h>
#include <stdlib.h>

typedef struct queue{
    int *nums;
    int front,rear;
    int size;
}*queue;

void addSize(queue s,int size){
    int *p = (int *)realloc(s->nums,size*sizeof(int));
    if(s->rear > s->front){
        for(int i=0; i < s->front; i++){
            if(i+s->size < size){
                p[i+s->size] = s->nums[i];
            }else{
                p[(i+s->size)%size] = s->nums[i];
            }
        }
    }
    s->nums = p;
    s->size = size;
    printf("Size has changed\n");
}

void push(queue s, int x){
    if((s->front+1)%(s->size) == s->rear){
        printf("The queue is full!\n");
        addSize(s,2*(s->size));
    }else{
        s->nums[(s->front)%(s->size)] = x;
    }
    s->front ++;
}

void pop(queue s){
    if(s->rear == s->front){
        printf("The queue is empty\n");
        exit(0);
    }
    s->rear ++;
    if(s->rear >= s->size){
        s->rear = (s->rear)%(s->size);
    }
}

int peekfront(queue s){
    if(s->front == s->rear){
        printf("The queue is empty\n");
        return 0;
    }
    return s->nums[s->front-1];
}

int peekrear(queue s){
    if(s->front == s->rear){
        printf("The queue is empty\n");
        return 0;
    }
    return s->nums[s->rear];
}

_Bool isempty(queue s){
    if(s->front == s->rear){
        return 0;
    }else{
        return 1;
    }
}

void clearqueue(queue s){
    free(s->nums);
    s->nums = NULL;
    s->front = 0;
    s->rear = 0;
    s->size = 0;
}

int main(){
    queue s = (queue)malloc(sizeof(struct queue));
    int size = 10;
    if(size < 1){
        printf("Error in size\n");
        exit(0);
    }
    s->nums = (int *)malloc(sizeof(int)*size);
    s->front = 0;
    s->rear = 0;
    s->size = size;

    int a[12] = {3,2,1,4,5,7,6,9,8,0,8,9};
    for(int i = 0; i < 12; i++){
        push(s,a[i]);
        printf("The num on the front is %d\n",peekfront(s));
    }
    pop(s);
    printf("The num on the top after push is %d\n",peekfront(s));
    printf("The num on the rear after pop is %d\n",peekrear(s));
    pop(s);
    printf("The num on the rear after pop is %d\n",peekrear(s));
    printf("The queue is %s\n",isempty==0?"empty":"not empty");
    clearqueue(s);
}
posted @ 2019-06-27 21:38  wangha  阅读(1018)  评论(0编辑  收藏  举报