顺序栈

 /*注意  声明结构体时,struct node l 函数用f(&l)参数必须带取地址符

  传参时 传的是结构体首地址

  若要使用指针指向结构体那必须为这个结构体分配动态内存  否则 无法使用 

顺序栈:利用一组连续的存储单元依次存放自栈底到栈顶的数据元素;
由于栈顶元素是经常变动的,所以附设top指示栈顶元素在顺序表中
的位置

基本操作

  1.初始化

  2.判断栈是否为空

  3.取栈顶元素

  4.入栈

  5.出栈

*/ 

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define maxsize 100
typedef struct
{
int stack[maxsize];
int top;
}seqstack;

void init(seqstack *l)
{
l->top=-1;
}
int empty(seqstack *l)
{
if(l->top ==-1 )
{
printf("表为空 ");
return 1;
}
else
return 0;
}
int gettop(seqstack *l)
{
int temp;
if(l->top<0)
{
printf("栈为空 ");
return 0;
}
else
{
temp=l->stack[l->top];
return temp;
}

}

int push(seqstack *l,int e)
{
if(l->top==maxsize)
{
printf("数组已满 ");
return 0;
}
else
{ l->top++;
l->stack[l->top]=e;

return 1;
}


}
int pop(seqstack *l)
{
int temp;
if(l->top==0)
{
printf("栈中已无元素");
return 0;
}
else
{
temp=l->stack[l->top];
l->top--;

} return temp;
}
int main()
{ seqstack l,*q;
//q=(seqstack *)malloc(sizeof(seqstack)) ;
int temp,temp1;
// init(q);
init(&l);
empty(&l);
printf("入栈元素为1,2,3,4,5 ");
push(&l,1);
push(&l,2);
push(&l,3);
push(&l,4);
push(&l,5);
temp=gettop(&l);
printf("栈顶元素为:%d ",temp);
pop(&l);
temp1=gettop(&l);
printf("出栈后的栈顶元素为:%d",temp1);

return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define max 100
typedef struct node
{
int data[max];
int top;
} stack;

void init(stack *l);
int get(stack *l,int * e);
int empty(stack *l);
int push(stack *l,int x);
int pop(stack *l,int *x);

int main()
{
stack *l;int x;int e;
l=(stack *)malloc(sizeof(stack));

init(l);
push(l,1);
push(l,2);
push(l,3);
push(l,4);
push(l,5);
get(l,&x);
printf("栈顶元素为%d: ",x);
printf(" ");
pop(l,&e);
printf("被取元素是%d: ",e);
get(l,&x);
printf("栈顶元素为%d: ",x);
/* */ printf(" ");
return 0;
}

void init(stack *l)
{
l->top=-1;
}
int empty(stack *l)
{
if(l->top==-1)
return 1;
else
return 0;
}
int get(stack *s,int *x)
{
if(empty(s))
{
printf("栈为空");
return 0;
}
else
{
*x=s->data[s->top-1];//top指向的是最上面元素的上面的元素
return *x;
}
}
int push(stack *l,int x)
{
if(l->top>=max)
{
printf("栈已满");
return 0;
}
else
{
l->data[l->top]=x;
l->top++;
return 1;
}
}
int pop(stack *l,int *x)
{
if(empty(l))
{
printf("栈为空");
return 0;
}
else
{
*x=l->data[l->top-1];
l->top--;
return *x;
}
}

#include<iostream>
#include<stdlib.h>
#include<malloc.h>
#include<stdio.h>
#define max 100
using namespace std;
typedef struct
{
int stack[max];
int top;
}seqstack;
void init(seqstack *l)
{
l->top=-1;
}
int empty(seqstack s)
{
if(s.top==-1)
{
printf("栈空");
return 1;
}

else
{
printf("栈没空");
return 0;
}
}
int top(seqstack l,int *s)
{
if(l.top<0)
{
printf("栈已空! ");
return 0;
}
{
*s=l.stack[l.top];
return 1;
}
}

int push(seqstack *s,int x)
{
if(s->top>=max)
{
printf("栈已满,不能进栈! ");
return 0;
}
else
{
s->top++;
s->stack[s->top]=x;
return 1;
}
}

int pop(seqstack *s,int *e)
{
if(s->top==0)
{
printf("栈已空");
return 0;
}
else
{
*e=s->stack[s->top];
s->top--;
return 1;
}
}
void dis(seqstack l)
{
while(l.top>-1)
{
printf("%d",l.stack[l.top]);
l.top--;
}
}
int main()
{
seqstack l;
//seqstack *s
//s=(seqstack*)malloc(sizeof(seqstack))
//必须要动态分配否则指针用完被释放
//
//使用指针只是复制一份形参 在被调函数运行完就会失效
//但是取址是址传递 改变了值
init(&l);
push(&l,1);
push(&l,2);
push(&l,3);
push(&l,4);
push(&l,5);
dis(l);
return 0;
}

原文地址:https://www.cnblogs.com/mykonons/p/5867045.html