C语言实现顺序栈(SeqStack)

发布时间:2020-07-01 11:03:17 作者:暮色伊人
来源:网络 阅读:848

使用数组来保存数据,使用二级指针来保存栈中元素的地址

seqStack.h

#ifndef SEQ_STACK_H
#define SEQ_STACK_H
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include<memory.h>

typedef void SeqStack;

SeqStack* Stack_Create(int cap);

void Stack_Destroy(SeqStack* stack);

void Stack_Clear(SeqStack* stack);

int Stack_Push(SeqStack* stack, void* item);

void* Stack_Pop(SeqStack* stack);

void* Stack_Top(SeqStack* stack);

int Stack_Size(SeqStack* stack);

bool Stack_isFull(SeqStack* stack);

bool Stack_isEmpty(SeqStack* stack);
#endif //SEQ_STACK_H

seqStack.c

#include"seqStack.h"

typedef struct SeqStackStruct {
    int size;
    int cap;
    unsigned int **data;
}MySeqStack;

SeqStack* Stack_Create(int cap) {
    MySeqStack* myStack = (MySeqStack*)malloc(sizeof(MySeqStack));
    if (myStack == NULL) {
        fprintf(stderr,"malloc stack space error.\n");
        return NULL;
    }
    myStack->data = (unsigned int**)malloc(cap * sizeof(unsigned int*));
    memset(myStack->data, 0, cap * sizeof(unsigned int*));
    myStack->size = 0;
    myStack->cap = cap;
    return (void *)myStack;
}

void Stack_Destroy(SeqStack* stack){
    MySeqStack *myStack = (MySeqStack *)stack;
    myStack->size = 0;
    myStack->cap = 0;
    free(myStack);
    myStack = NULL;
}

void Stack_Clear(SeqStack* stack) {
    MySeqStack *myStack = (MySeqStack *)stack;
    myStack->size = 0;
}

int Stack_Push(SeqStack* stack, void* item) {
    if (stack == NULL) {
        return -1;
    }
    MySeqStack *myStack = (MySeqStack *)stack;
    if (myStack->size >= myStack->cap) {    
        fprintf(stderr, "stack is Full.\n");
        return -2;
    }
    myStack->size++;
    myStack->data[myStack->size-1] = (unsigned int*)item;

}

bool Stack_isFull(SeqStack* stack) {
    if (stack == NULL) {
        return false;
    }
    MySeqStack *myStack = (MySeqStack *)stack;
    return myStack->size == myStack->cap;
}

bool Stack_isEmpty(SeqStack* stack) {
    if (stack == NULL) {
        return false;
    }
    MySeqStack *myStack = (MySeqStack *)stack;
    return myStack->size == 0;
}

void* Stack_Pop(SeqStack* stack) {
    if (stack == NULL) {
        return;
    }
    MySeqStack *myStack = (MySeqStack *)stack;
    void *item = (void *)myStack->data[myStack->size - 1];
    myStack->data[myStack->size - 1] = NULL;
    myStack->size--;
    return  item;
}

void* Stack_Top(SeqStack* stack) {
    if (stack == NULL) {
        return;
    }
    MySeqStack *myStack = (MySeqStack *)stack;
    return (void *)myStack->data[myStack->size - 1];
}

int Stack_Size(SeqStack* stack) {
    if (stack == NULL) {
        return 0;
    }
    MySeqStack *myStack = (MySeqStack *)stack;
    return myStack->size;
}
推荐阅读:
  1. 【C语言数据结构】顺序栈
  2. PHP实现简单顺序栈

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言 数据结构

上一篇:robotframework 常用快捷键

下一篇:java中实现输入的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》