1.顺序栈
(1)功能函数
#include"sqstack.h"
sqstack* stack_create(int len){
sqstack *s=(sqstack *)malloc(sizeof(sqstack));
if(s==NULL){
return NULL;
}
s->data=(int *)malloc(len*sizeof(int));
if(s->data==NULL){
free(s);
return NULL;
}
memset(s->data,0,len*sizeof(int));
s->maxlen=len;
s->top=-1;
return s;
}
int stack_empty(sqstack*s){
return (s->top==-1?1:0);
}
int stack_full(sqstack*s){
return (s->top==(s->maxlen-1)?1:0);
}
//入栈
int stack_push(sqstack*s,data_t value){
if(s==NULL){
return -1;
}
if(s->top == (s->maxlen-1)){
printf("栈满了\n");
return -1;
}
s->top++;
s->data[s->top]=value;
}
//出栈
int stack_pop(sqstack*s){
s->top--;
return s->data[s->top+1];
}
//查看栈顶元素
int stack_top(sqstack*s){
if(s==NULL){
return -1;
}
return s->data[s->top];
}
//清空栈
int stack_clear(sqstack*s){
if(s==NULL){
return -1;
}
s->top=-1;
return 0;
}
//释放栈
int stack_free(sqstack*s){
if(s==NULL){
return -1;
}
if(s->data==NULL){
free(s->data);}
free(s);
return 0;
}
(2)主函数
#include"sqstack.h"
int main(){
sqstack *s;
s=stack_create(100);
if(s==NULL)
return -1;
stack_push(s,10);
stack_push(s,20);
stack_push(s,30);
while(!stack_empty(s)){
printf("pop %d \n",stack_pop(s));
}
stack_free(s);
return 0;
}
(3)头文件
#ifndef _SQSTACK_H
#define _SQSTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <string.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_pop(sqstack*s);//出栈
int stack_top(sqstack*s);//查看栈顶元素
int stack_clear(sqstack*s);
int stack_free(sqstack*s);
int stack_empty(sqstack*s);
int stack_full(sqstack*s);
#endif
2.链栈
(1)功能函数
#include "linkstack.h"
linkstack stack_create(){
linkstack s=(linkstack)malloc(sizeof(listnode));
if(s == NULL){
printf("malloc failed\n");
return NULL;}
s->data=0;
s->next=NULL;
return s;
}
int stack_push(linkstack s,data_t value){
if(s == NULL){
printf("malloc failed\n");
return -1;}
linkstack p=(linkstack)malloc(sizeof(listnode));
if(p == NULL){
printf("malloc failed\n");
return -1;}
p->data=value;
p->next=NULL;
p->next=s->next;
s->next=p;
return 0;
}
int stack_pop(linkstack s){
if(s == NULL){
printf("malloc failed\n");
return -1;}
linkstack p=s->next;
int num =p->data;
s->next=p->next;
free(p);
p=NULL;
return num;
}
int stack_empty(linkstack s){
if(s == NULL){
printf("malloc failed\n");
return -1;}
return (s->next==NULL?1:0);
}
int stack_top(linkstack s){
if(s == NULL){
printf("malloc failed\n");
return -1;}
return (s->next->data);
}
linkstack stack_free(linkstack s){
if(s == NULL){
printf("malloc failed\n");
return NULL;}
while(s!=NULL){
linkstack p=s;
s=s->next;
printf("free data%d\n",p->data);
free(p);
return NULL;
}
}
(2)主函数
#include"linkstack.h"
int main(){
linkstack 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));
}
s=stack_free(s);
return 0;
}
(3)头文件
/*===============================================
* 文件名称:linkstact.h
* 创 建 者:
* 创建日期:2023年08月13日
* 描 述:
================================================*/
#ifndef _LINKSTACK_H
#define _LINKSTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <string.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);
int stack_pop(linkstack s);
int stack_empty(linkstack s);
int stack_top(linkstack s);
linkstack stack_free(linkstack s);
#endif