顺序栈和链栈的基本操作(C语言)

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50//栈最大容量
#define ERROR -1
typedef struct sqstack{
    int data[MAXSIZE];
    int top;
}SqStack;//顺序栈
typedef struct linknode{
    int data;
    struct linknode*next;
}LNode,*LiStack;//链栈

// <-----顺序栈基本操作------->
void init_sqstack(SqStack *S);//初始化栈
int push_sqstack(SqStack*S,int e);//入栈
int pop_Sqstack(SqStack*S,int *e);//出栈
int IsEmpty_sqstack(SqStack S);//判断栈空
int GetTopElem_sqstack(SqStack S,int*e);//获取栈顶元素

// <-----链栈基本操作------->
void init_listack(LiStack*S);//初始化栈
int push_listack(LiStack*S,int e);//入栈
int pop_listack(LiStack*S,int*e);//出栈
int IsEmpty_listack(LiStack S);//判断栈空
int GetTopElem_listack(LiStack S,int*e);//获取栈顶元素

int main()
{   
    LiStack LS;
    init_listack(&LS);
    int e=0;
    for(int i=1;i<5;i++)
    {
        push_listack(&LS,i);
    }
    for(int i=1;i<5;i++)
    {
        pop_listack(&LS,&e);
        printf("%d ",e);
    }
    system("pause");
    return 0;
}

void init_sqstack(SqStack *S)
{
    S->top=0;
}

int push_sqstack(SqStack *S, int e)
{   
    if(S->top==MAXSIZE)
        return ERROR;
    S->data[S->top++]=e;
    
    return 0;
}

int pop_Sqstack(SqStack *S, int *e)
{   
    if(S->top==0)
        return ERROR;
    *e=S->data[--S->top];
    return 0;
}

int IsEmpty_sqstack(SqStack S)
{   
    if(0==S.top)
    {
        return 1;
    }
    return 0;
}

int GetTopElem_sqstack(SqStack S, int*e)
{   
    if(0==S.top)
    {
        return ERROR;
    }
    *e=S.data[--S.top];
    return 0;
}

void init_listack(LiStack *S)
{
    *S=NULL;
}

int push_listack(LiStack *S, int e)
{   
    LNode*p=(LNode*)malloc(sizeof(LNode));
    if(p)
    {
        p->data=e;
        p->next=*S;
        *S=p; 
    }
    return 0;
}

int pop_listack(LiStack *S, int *e)
{   
    if(NULL==(*S))
        return ERROR;
    *e=(*S)->data;
    (*S)=(*S)->next;
    return 0;
}

int IsEmpty_listack(LiStack S)
{   
    if(NULL==S)
        return 1;
    return 0;
}

int GetTopElem_listack(LiStack S, int *e)
{   if(NULL==S)
        return ERROR;
    *e=S->data;
    return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值