目录
栈是限制在一端进行插入操作和删除操作的线性表(俗称堆栈)。
允许进行操作的一端称为“栈顶”
另一固定端称为“栈底”
当栈中没有元素时称为空栈。特点:后进先出
顺序栈
顺序栈是顺序表的一种,具有顺序表同样的存储结构,由数组定义,配合用数组下标表示的栈顶指针top(相对指针)完成各种操作。
顺序栈常用代码
sqstack.c(常用函数的实现)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqstack.h"
/*
创建一个顺序栈
*/
sqstack *stack_create(int len)
{
sqstack *s;
if((s=(sqstack *)malloc(sizeof(sqstack)))==NULL)//先申请结构体的内存,并不是实际存放数据的内存
{
printf("malloc sqstack failed\n");
return NULL;
}
if((s->data=(data_t *)malloc(len*sizeof(data_t)))==NULL)//实际存放数据的内存申请
{
printf("malloc data failed\n");
free(s);// 假如结构体申请内存成功但是此处数据申请不成功的话可以在这里释放掉结构体的内存
return NULL;
}
memset(s->data,0,len*sizeof(data_t));
s->maxlen=len;
s->top=-1;
return NULL;
}
/*
入栈操作
*/
int stack_push(sqstack *s,data_t value)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
if(s->top==s->maxlen-1)
{
printf("stack is full\n");
return -1;
}
s->top++;
s->data[s->top]=value;
return 0;
}
/*
检验顺序栈是否为空,为空返回1,不为空返回0
*/
int stack_empty(sqstack *s)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
return(s->top==-1?1:0);
}
/*
检验顺序栈是否已满,已满返回1,不满返回0
*/
int stack_full(sqstack *s)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
return(s->top==s->maxlen-1?1:0);
}
/*
出栈操作 返回栈顶的值
*/
int stack_pop(sqstack *s)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
s->top--;
return(s->data[s->top+1]);
}
int stack_top(sqstack *s)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
return (s->data[s->top+1]);
}
int stack_clear(sqstack *s)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
s->top=-1;
return 0;
}
/*
要先释放数据存储位置的内存,再释放结构体的空间
*/
int stack_free(sqstack *s)
{
if(s==NULL)
{
printf("s is NULL\n");
return -1;
}
if(s->data!=NULL)
{
free(s->data);
}
free(s);
return 0;
}
sqstack.h(头文件)
typedef int data_t;
typedef struct
{
data_t *data;
int maxlen;
int top;
}sqstack;
sqstack *stack_create(int len);
int stack_push(sqstack *s,data_t value);
int stack_empty(sqstack *s);
int stack_full(sqstack *s);
int stack_pop(sqstack *s);
int stack_top(sqstack *s);
int stack_clear(sqstack *s);
int stack_free(sqstack *s);
test.c(测试代码)
#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
int main()
{
sqstack *s;
int len=10;
s=stack_create(len);
return 0;
}
链式栈
链式栈的插入操作和删除操作均在链表头部进行,链表尾部就是栈底,栈顶指针就是头指针。
常用代码
linkstack.c(常用函数实现)
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
linkstack stack_create()
{
linkstack s;
s=(linkstack)malloc(sizeof(listnode));
if(s==NULL)
{
printf("malloc failed");
return NULL;
}
s->data=0;
s->next=NULL;
return s;
}
int stack_push(linkstack s,data_t value)
{
linkstack p;
if(s==NULL)
{
printf("s is NULL");
return -1;
}
p=(linkstack)malloc(sizeof(listnode));
if(p==NULL)
{
printf("p malloc failed");
return -1;
}
p->data=value;
p->next=s->next;
s->next=p;
return 0;
}
int stack_pop(linkstack s)
{
linkstack p;
data_t t;
p=s->next;
s->next=p->next;
t=p->data;
free(p);
p=NULL;
return t;
}
int stack_empty(linkstack s)
{
if(s==NULL)
{
printf("s is NULL");
return -1;
}
return(s->next==NULL?1:0);
}
int stack_top(linkstack s)
{
if(s==NULL)
{
printf("s is NULL");
return -1;
}
return (s->next->data);
}
linkstack stack_free(linkstack s)
{
linkstack p;
if(s==NULL)
{
printf("s is NULL");
return NULL;
}
while(s!=NULL)
{
p=s;
s=s->next;
printf("free:%d",p->data);
free(p);
}
return NULL;
}
linkstack.h(头文件)
typedef int data_t;
typedef struct node
{
data_t data;
struct node *next;
}listnode,*linkstack;
linkstack stack_create();
int stack_push(linkstack s,data_t value);
int stack_pop(linkstack s);
int stack_empty(linkstack s);
int stack_top(linkstack s);
linkstack stack_free(linkstack s);
test.c(测试代码)
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
int main()
{
linkstack s;
s=stack_create();
if(s==NULL)
{
return -1;
}
stack_push(s,10);
stack_push(s,20);
stack_push(s,30);
stack_push(s,40);
while(!stack_empty(s))
{
printf("pop:%d\n",stack_pop(s));
}
return 0;
}