目录
顺序栈
栈的特点:只能在一个端进行数据的插入和删除,先入栈的数据后出栈,后入栈的数据先出栈。
FILO:first in last out
LIFO:last in first out
顺序栈特点
内存空间连续开辟,长度固定
逻辑结构:线性结构
存储结构:顺序结构
操作
定义结构体
//定义结构体
typedef int dataty;//重定义了一下int,以后用程序方便修改
//秒票数顺序栈的结构体
typedef struct
{
dataty *data;//栈的首地址
int len;//栈的长度
int top;//栈针
}stack;
创建一个空的顺序栈
stack *creat_stack(int len)//传入参数长度
{
//创建一个用于保存栈的信息的结构体
stack *p=(stack *)malloc(sizeof(stack));
if(NULL == p)//判断开辟空间是否成功
{
printf("creat_stack error\n");
return NULL;
}
//创建栈
//对结构体内容的初始化
p->data=(dataty *)malloc(sizeof(dataty)*len);//顺序存储
if(NULL == p->data)
{
printf("malloc error\n");
return NULL;
}
p->len=len;//开辟长度
p->top=-1;//空栈
return p;
}
增
入栈
int in_stack(stack *p,dataty data)//入栈
{
if(if_full_stack(p))//判满
{
printf("in_stack error\n");
return -1;
}
p->top++;//栈顶指针+1
return p->data[p->top]=data;
}
判满
int if_full_stack(stack *p)
{
return p->top==p->len+1;
}
删
出栈
int out_stack(stack *p)
{
if(if_empty_stack(p))//判空
{
printf("out_stack error\n");
return -1;
}
return p->data[p->top--];//先返回p->data[p->top]的值在p->top--
}
判空
int if_empty_stack(stack *p)
{
return p->top==-1;//1为空,0为非空
}
计算长度
int length_stack(stack *p)
{
return p->top+1;//栈顶数+1即实际长度
}
获取栈顶数据
dataty top_stack(stack *p)
{
if(if_empty_stack(p))//判空
{
printf("top_stack error\n");
return -1;
}
return p->data[p->top];//返回栈顶数据
}
清空顺序栈
void clear_stack(stack *p)
{
p->top=-1;
}
销毁
int des_stack(stack **sp)
{
if(if_empty_stack(*sp)
{
free(*sp);
*sp=NULL;
printf("success\n");
}
else
printf("please make stack empty\n");
return 0;
}
完整代码
//stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
stack *creat_stack(int len)
{
//创建一个用于保存栈的信息的结构体
stack *p=(stack *)malloc(sizeof(stack));
if(NULL == p)
{
printf("creat_stack error\n");
return NULL;
}
//创建栈
//对结构体内容的初始化
p->data=(dataty *)malloc(sizeof(dataty)*len);
if(NULL == p->data)
{
printf("malloc error\n");
return NULL;
}
p->len=len;
p->top=-1;//空栈
return p;
}
int in_stack(stack *p,dataty data)
{
if(if_full_stack(p))
{
printf("in_stack error\n");
return -1;
}
p->top++;
// p->data[p->top]=data;
return p->data[p->top]=data;
}
int if_full_stack(stack *p)
{
// return p->top < p->len;
return p->top==p->len+1;
}
int out_stack(stack *p)
{
if(if_empty_stack(p))
{
printf("out_stack error\n");
return -1;
}
return p->data[p->top--];
}
int if_empty_stack(stack *p)
{
return p->top==-1;//1为空,0为非空
}
int length_stack(stack *p)
{
return p->top+1;
}
dataty top_stack(stack *p)
{
if(if_empty_stack(p))
{
printf("top_stack error\n");
return -1;
}
return p->data[p->top];
}
void clear_stack(stack *p)
{
p->top=-1;
}
int des_stack(stack **sp)
{
if(if_empty_stack(*sp))
{
free(*sp);
*sp=NULL;
printf("success\n");
}
else
printf("please make stack empty\n");
return 0;
}
//stack.h
#ifndef _STACK_H_
#define _STACK_H_
typedef int dataty;
typedef struct
{
dataty *data;
int len;
int top;
}stack;
stack *creat_stack(int len);
int in_stack(stack *p,dataty data);
int if_full_stack(stack *p);
int out_stack(stack *p);
int if_empty_stack(stack *p);
int top_stack(stack *p);
int length_stack(stack *p);
void clear_stack(stack *p);
int des_stack(stack **sp);
#endif