栈的操作(顺序栈,链栈)

本文详细介绍了顺序栈和链栈的数据结构及其基本操作,包括初始化、压栈、弹栈、获取栈顶元素等。通过具体的C语言代码实现,帮助读者深入理解这两种栈的工作原理。
摘要由CSDN通过智能技术生成

//顺序栈
typedef struct
{
int data[100];
int top;
}sqstack;
void jianzhan(sqstack*s)
{
s=(sqstack*)malloc(sizeof(sqstack));
s->top=-1;
}
int pan(sqstack*s)
{
if(s->top==-1)
{
return 1;
}
else return 0;
}
void push(sqstack*s,int key)
{
s->data[s->top++]=key;
}
void pop(sqstack*s)
{
int e;
if(!pan(s))
{
e=s->data[s->top];
s->top–;
}
return e;
}
void gettop(sqstack*s)
{
int e;
if(!pan(s))
{
e=s->data[s->top];//取栈顶的元素不是删除栈顶的元素,不用top–;
}
return e;
}

//课本上的比较详细的

include

include

define sqstackchu 100

define sqstackjia 10

typedef struct
{
int *base;
int *top;
int sqstacksize;
}sqstack;
int initstack(sqstack &s)
{
s.base=(int *)malloc(sqstackchu*sizeof(int));
if(!s.base)exit(-1);
s.top=s.base;
s.sqstacksize=sqstackchu;
return 1;
}
int gettop(sqstack &s,int e)
{
if(s.top==s.base)return 0;
e=*(s.top-1);
return e;
}
int push(sqstack&s,int e)
{
if(s.top-s.base>=s.sqstacksize)
{
s.base=(int *)malloc(s.base,s.sqstacksize+sqstackjia)*sizeof(int);
if(!s.base) exit(-1);
s.top=s.base+s.sqstacksize;
s.sqstacksize+=sqstackjia;
}
*s.top++=e;
return 1;
}
void pop(sqstack &s)
{
if(s.top==s.base)return ;
s.top–;
}
int empty(sqstack &s)
{
if(s.top==s.base)return 1;
else return 0;
}
typedef struct node
{
int data;
struct node*next;
}lnode;
typedef struct
{
lnode *front,*rear;
}lqueue;
void intitqueue(lqueue &q)
{
q.front=q.rear=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
q.front->next=NULL;
}
void enqueue(lqueue&q,int n)
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
p->data=n;
p->next=NULL;
q.rear->next=p;
q.rear=p;
}

include

include

define sqstackchu 100

define sqstackjia 10

typedef struct
{
int *base;
int *top;
int sqstacksize;
}sqstack;
int initstack(sqstack &s)
{
s.base=(int *)malloc(sqstackchu*sizeof(int));
if(!s.base)exit(-1);
s.top=s.base;
s.sqstacksize=sqstackchu;
return 1;
}
int gettop(sqstack &s,int e)
{
if(s.top==s.base)return 0;
e=*(s.top-1);
return e;
}
int push(sqstack&s,int e)
{
if(s.top-s.base>=s.sqstacksize)
{
s.base=(int *)malloc(s.base,s.sqstacksize+sqstackjia)*sizeof(int);
if(!s.base) exit(-1);
s.top=s.base+s.sqstacksize;
s.sqstacksize+=sqstackjia;
}
*s.top++=e;
return 1;
}
void pop(sqstack &s)
{
if(s.top==s.base)return ;
s.top–;
}
int empty(sqstack &s)
{
if(s.top==s.base)return 1;
else return 0;
}
typedef struct node
{
int data;
struct node*next;
}lnode;
typedef struct
{
lnode *front,*rear;
}lqueue;
void intitqueue(lqueue &q)
{
q.front=q.rear=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
q.front->next=NULL;
}
void enqueue(lqueue&q,int n)
{
lnode *p;
p=(lnode*)malloc(sizeof(lnode));
if(!q.front)exit(0);
p->data=n;
p->next=NULL;
q.rear->next=p;
q.rear=p;
}

“`

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值