一个简单C顺序栈的实现

仅用于记录,加深理解的练习。
水平有限,不规范之处还请包涵指正。
只实现了初始化栈,进栈,出栈,打印数据功能。

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

#define ADDSIZE 10
#define STARTSIZE 10

typedef int ElemType;
typedef struct
{
    ElemType *top;
    ElemType *base;
    int stacksize;
}Stack;                                 //define structure of stack

Stack *InitStack(Stack *);              //initialize the stack
int EmptyStack(Stack *);                //judge if it is a empty or not
int Push(Stack *,ElemType e);           //send e in top of stack
ElemType pop(Stack *);                  //pop data of top-1
void display(Stack *);                  //display the whole data of it

int main()
{
    ElemType data;
    int choice;
    Stack *stack;
    stack=InitStack(stack);
    printf("--One empty stack has builded,choose a operation:");
    while(1)
    {
        printf("\n--1.push stack\n--2.pop stack\n--3.display\n--");
        scanf("%d",&choice);

        if(choice!=1&&choice!=2&&choice!=3)
        {
            printf("\n--invaid choice,try again.\n--");
            scanf("%d",&choice);
        }

        if(choice==1)
        {
            printf("--please input data to push in the top.\n--");
            scanf("%d",&data);
            Push(stack,data);
        }

        if(choice==2)
        {
            if(EmptyStack(stack))
                printf("\n--That stack is empty.No data can pop.\n--try again\n--");
            else
                printf("\n--Operation over,%d leave stack.\n--",pop(stack));
        }

        if(choice==3)
            display(stack);
    }
}

Stack *InitStack(Stack *s)
{                                       
    s=(Stack *)malloc(sizeof(Stack));
    s->base=(ElemType *)malloc((sizeof(ElemType)*STARTSIZE));
    s->top=s->base;
    s->stacksize=STARTSIZE;
    return s;
}

EmptyStack(Stack *s)                    //1 indicate the stack is empty
{
    if(s->top==s->base)
        return 1;
    else
        return 0;
}

//because the data is continuous,judge if full or not by difference between top and base
//if this stack is full,use realloc to get more memory,but s->base may be changed,
//so. we should change s->top to adapt to the change
int Push(Stack *s,ElemType e)
{
    if((s->top)-(s->base)>=s->stacksize)
    {
        s->base=(ElemType *)realloc(s->base,(s->stacksize+ADDSIZE)*sizeof(ElemType));
        s->top=s->base+s->stacksize;
    }
    *s->top=e;
    s->top++;
    return 1;
}

ElemType pop(Stack *s)
{
    ElemType tep;
    tep=*(s->top-1);
    s->top--;
    return tep;
}
//use a temporary variable to display all value of stack
void display(Stack *s)
{
    ElemType *tem=s->base;
    printf("\n-------base of stack-------");
    while(tem<s->top)
        printf("\n-------%13d-------",*tem++);
    printf("\n-------top of stack -------");
}

栈结构只包含两个指针和一个记录数据多少的整型变量,所以数据储存的位置是另行开辟的,realloc函数有重新分配更多空间的能力,用于栈的扩充。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值