加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_郴州站长网 (http://www.0735zz.com/)- 云通信、区块链、物联设备、云计算、站长网!
当前位置: 首页 > 教程 > 正文

用C语言达成简单的栈

发布时间:2021-11-19 18:40:15 所属栏目:教程 来源:互联网
导读:栈是一种常见的数据结构,主要特点是后进先出。以下是用C语言实现的简单的栈。 头文件 stack.h ,定义栈的结构体和相关的操作: #ifndef STACK_H #define STACK_H enum { STACK_OK = 0, STACK_OVERFLOW, STACK_ERROR, }; typedef int ElemType; struct stack

栈是一种常见的数据结构,主要特点是“后进先出”。以下是用C语言实现的简单的栈。
 
头文件 stack.h ,定义栈的结构体和相关的操作:
 
#ifndef STACK_H
#define STACK_H
 
enum { STACK_OK = 0, STACK_OVERFLOW, STACK_ERROR, };
 
typedef int ElemType;
 
struct stack {
    ElemType *data;
    ElemType *top;
    int capability;
};
 
 
void stack_init(struct stack *st, int capability);
void stack_destroy(struct stack *st);
int stack_push(struct stack *st, ElemType elem);
int stack_pop(struct stack *st);
ElemType stack_top(const struct stack *st);
int stack_size(const struct stack *st);
int stack_capability(const struct stack *st);
int stack_full(const struct stack *st);
int stack_empty(const struct stack *st);
void stack_clear(struct stack *st);
 
#endif
 
C文件 stack.c,实现stack的相关操作。
 
#include "stack.h"
#include <assert.h>
#include <stdlib.h>
 
void stack_init(struct stack *st, int capability)
{
    assert(st && capability > 0);
    st->data = (ElemType *)malloc(sizeof(ElemType) * capability);
    assert(st->data);
    st->top = st->data;
    st->capability = capability;
}
 
void stack_destroy(struct stack *st)
{
    assert(st);
    if (st->data)
        free(st->data);
    st->data = 0;
    st->top = 0;
    st->capability = 0;
}
 
int stack_push(struct stack *st, ElemType elem)
{
    assert(st);
    if (stack_full(st))
        return STACK_OVERFLOW;
    *(st->top++) = elem;
    return STACK_OK;
}
 
int stack_pop(struct stack *st)
{
    assert(st);
    if (stack_empty(st))
        return STACK_OVERFLOW;
    st->top--;
    return STACK_OK;
}
 
ElemType stack_top(const struct stack *st)
{
    assert(st);
    if (stack_empty(st))
        return (ElemType)0;
    else
        return *(st->top - 1);
}
 
int stack_size(const struct stack *st)
{
    assert(st);
    return (st->top - st->data);
}
 
int stack_capability(const struct stack *st)
{
    assert(st);
    return st->capability;
}
 
int stack_full(const struct stack *st)
{
    return (stack_size(st) == stack_capability(st));
}
 
int stack_empty(const struct stack *st)
{
    return (stack_size(st) == 0);
}
 
void stack_clear(struct stack *st)
{
    assert(st);
    st->top = st->data;
}
 
上面在stack.h 中定义了ElemType为int,是简单的数据类型。上面的实现也是针对简单数据库类型的,对于一些稍复杂的数据类型,上面的实现不适用。例如,如果栈元素需要用某个函数来销毁,则stack_clear就不适用了。
 
实现中也用到了assert,不过我不大喜欢用assert,因为会使程序中止。其实可以用if来作检测而不中止,或是不作检测,靠使用者自行判断。
 
上面实现也没有用到 STACK_ERROR 这个值。
 
下面是提供测试的main.c
 
#include <stdio.h>
#include "stack.h"
 
int main()
{
    struct stack st;
    int i;
    stack_init(&st, 5);
 
    printf("-------------- init stack ----------------n");
    printf("stack capability: %dn", stack_capability(&st));
    printf("stack size: %dn", stack_size(&st));
    printf("stack empty ? %sn", stack_empty(&st) ? "Y" : "N");
    printf("stack full ? %sn", stack_full(&st) ? "Y" : "N");
 
    printf("-------------- pushing elements ----------------n");
    for (i = 0; i < 10; ++i) {
        printf("push %i OK ? %sn", i,
            (stack_push(&st, i) == STACK_OK ? "Y" : "N"));
        printf("stack size: %dn", stack_size(&st));
        printf("stack empty ? %sn", stack_empty(&st) ? "Y" : "N");
        printf("stack full ? %sn", stack_full(&st) ? "Y" : "N");
    }
 
    printf("-------------- poping elements ----------------n");
    while (!stack_empty(&st)) {
        printf("top: %dn", stack_top(&st));
        printf("pop OK? %sn", stack_pop(&st) == STACK_OK ? "Y" : "N");
        printf("stack size: %dn", stack_size(&st));
        printf("stack empty ? %sn", stack_empty(&st) ? "Y" : "N");
        printf("stack full ? %sn", stack_full(&st) ? "Y" : "N");
    }
 
    printf("-------------- clear stack ----------------n");
    stack_clear(&st);
    printf("stack capability: %dn", stack_capability(&st));
    printf("stack size: %dn", stack_size(&st));
    printf("stack empty ? %sn", stack_empty(&st) ? "Y" : "N");
    printf("stack full ? %sn", stack_full(&st) ? "Y" : "N");
 
    printf("-------------- destroy stack --------------n");
    stack_destroy(&st);
 
    return 0;
}

(编辑:开发网_郴州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读