【数据结构】顺序栈的实现(函数功能实现:创建栈,进栈,出栈,释放内存等操作)

问题描述:顺序栈的实现(功能实现:创建栈,进栈,出栈,释放内存等操作)。

代码实现:

实现一个功能函数需要三个文件(三个文件在同一目录)。
sqstack.h(定义顺序表)
sqstack.c(实现接口函数)
test.c(主函数实现)

sqstack.h

定义各种函数。包括创建栈sqstack * stack_create(int len);
入栈int stack_push(sqstack * s, data_t value);
**出栈data_t stack_pop(sqstack s);
释放内存int stack_free(sqstack s);

typedef int data_t;

typedef struct {
    data_t *data;
    int maxlen;
    int top;
}sqstack;

sqstack * stack_create(int len);
int stack_push(sqstack * s, data_t value);
int stack_empty(sqstack *s);
int stack_full(sqstack *s);
data_t stack_pop(sqstack *s);
data_t stack_top(sqstack *s);
int stack_clear(sqstack *s);
int stack_free(sqstack *s);

sqstack.c

上述定义的函数实现。

#include "sqstack.h"
#include <stdio.h>
#include  <stdlib.h>
#include <string.h>
sqstack * stack_create(int len) {
    sqstack * s;

    if ((s =(sqstack *)malloc(sizeof(sqstack))) == NULL) {
        printf("malloc sqstack failed\n");
        return NULL;
    }
    if ((s->data = (data_t *)malloc(len * sizeof(data_t)))==NULL) {
        printf("malloc data failed\n");
        free(s);
        return NULL;
    }
    memset(s->data, 0, len*sizeof(data_t));
    s->maxlen = len;
    s->top = -1;

    return s;
}
/*
 * @ret 1-empty
 */
int stack_empty(sqstack *s){
    if (s == NULL){
        printf("s is NULL\n");
    }
    return (s->top == -1 ? 1 : 0);
}
/*
 * @ret 1-full
 */
int stack_full(sqstack *s) {
    if (s == NULL){
        printf("s is NULL\n");
    }
    return (s->top == s->maxlen-1 ? 1 : 0);
}

int stack_push(sqstack * s, data_t value) {
    if (s == NULL) {
        printf("s is NULL\n");
        return -1;
    }
    if (s->top == s->maxlen-1) {
        printf("stack is full\n");
        return -1;
    }

    s->top++;
    s->data[s->top] = value;
    return 0;
}

data_t stack_pop(sqstack *s) {
    s->top--;
    return (s->data[s->top+1]);
}

data_t stack_top(sqstack *s){
    return (s->data[s->top]);
}

int stack_clear(sqstack *s) {
    if (s == NULL) {
        printf("s is NULL\n");
        return -1;
    }
    s->top = -1;
    return 0;
}

int stack_free(sqstack *s) {
    if (s == NULL) {
        printf("s is NULL\n");
        return -1;
    }
    if (s->data != NULL)
        free(s->data);
    free(s);
    return 0;
}

test.c

主函数程序:首先创建了一个大小为100的栈,依次入栈4个整数值,分别是10,20,30,40。调用出栈函数,打印元素。最后调用stack_free释放栈所占内存。

#include <stdio.h>
#include "sqstack.h"
int main(int argc, const char *argv[])
{
    sqstack *s;
    s = stack_create(100);
    if (s == NULL)
        return -1;

    stack_push(s, 10);
    stack_push(s, 20);
    stack_push(s, 30);
    stack_push(s, 40);
    while (!stack_empty(s)) {
        printf("pop: %d \n", stack_pop(s) );
    }
    stack_free(s);

    return 0;
}

运行结果:

在这里插入图片描述
由上述结果图可知,成功运行创建栈,入栈4个整数,出栈及释放内存等操作。(后进先出)

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值