顺序栈和链栈代码
1. 顺序栈
seq_stack.h文件
#ifndef __SEQ_STRACK_H__
#define __SEQ_STRACK_H__
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int top;
}Stack,*Stack_p;
//创建栈
Stack_p create_stack();
//判空
int isEmpty_stack(Stack_p S);
//判满
int isFull_stack(Stack_p S);
//入栈/压栈
void push_stack(Stack_p S,ElemType data);
//打印栈中元素
void print_stack(Stack_p S);
//出栈
void pop_stack(Stack_p S);
//释放顺序栈
void free_stack(Stack_p *S);
#endif
seq_stack.c文件
#include "seq_stack.h"
//创建栈
Stack_p create_stack()
{
Stack_p S=(Stack_p)malloc(sizeof(Stack));
if(NULL==S)
{
printf("空间申请失败\n");
return NULL;
}
S->top =-1;
return S;
}
//判空
int isEmpty_stack(Stack_p S)
{
if(NULL==S)
{
printf("栈不存在\n");
return -1;
}
return S->top==-1?1:0;
}
//判满
int isFull_stack(Stack_p S)
{
if(NULL==S)
{
printf("栈不存在\n");
return -1;
}
return S->top==MAXSIZE-1?1:0;
}
//入栈/压栈
void push_stack(Stack_p S,ElemType data)
{
if(NULL==S)
{
printf("栈不存在\n");
return;
}
if(isFull_stack(S))
{
printf("栈满\n");
return;
}
S->data[++S->top]=data;
return;
}
//打印栈中元素
void print_stack(Stack_p S)
{
if(NULL==S)
{
printf("栈不存在\n");
return;
}
if(isEmpty_stack(S))
{
printf("栈空\n");
return;
}
int i;
for(i=S->top;i>=0;i--)
{
printf("%3d",S->data[i]);
}
putchar(10);
return;
}
//出栈/弹栈
void pop_stack(Stack_p S)
{
if(NULL==S)
{
printf("栈不存在\n");
return;
}
if(isEmpty_stack(S))
{
printf("栈空\n");
return;
}
ElemType e=S->data[S->top--];//如果需要出栈元素的值
return;
}
//释放顺序栈
void free_stack(Stack_p *S)
{
//入参为空的判断
if(NULL==S || NULL==*S)
{
printf("入参为空\n");
return;
}
free(*S);
*S=NULL;
return;
}
main.c文件
#include "seq_stack.h"
int main()
{
Stack_p S=create_stack();//创建栈
printf("压栈5个元素:\n");
push_stack(S,1);//压栈
push_stack(S,2);
push_stack(S,3);
push_stack(S,4);
push_stack(S,5);
print_stack(S);
printf("出栈2个元素:\n");
pop_stack(S);//出栈
pop_stack(S);
print_stack(S);
free_stack(&S);//释放顺序栈
printf("%p\n",S);
return 0;
}
2. 链栈
Link_Stack.h文件
#ifndef __LINK_STACK_H__
#define __LINK_STACK_H__
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
}SNode,*SNode_p;
//申请结点
SNode_p creat_node(ElemType data);
//判空
int isEmpty(SNode_p S);
//入栈
void push(SNode_p *S,ElemType data);
//打印栈中元素
void print_stack(SNode_p S);
//出栈
void pop(SNode_p *S);
#endif
Link_Stack.c文件
#include "Link_Stack.h"
//申请结点
SNode_p creat_node(ElemType data)
{
SNode_p s=(SNode_p)malloc(sizeof(SNode));
if(NULL==s)
{
printf("空间申请失败\n");
return NULL;
}
s->data=data;
return s;
}
//判空
int isEmpty(SNode_p S)
{
return S==NULL?1:0;
}
//入栈
void push(SNode_p *S,ElemType data)
{
SNode *s= creat_node(data);
s->next=*S;
*S=s;
}
//出栈
void pop(SNode_p *S)
{
if(isEmpty(*S))
{
printf("栈空\n");
return;
}
//ElemType e=S->data;//若需要获取出栈元素
*S=(*S)->next;
return;
}
//打印栈中元素
void print_stack(SNode_p S)
{
if(isEmpty(S))
{
printf("栈为空\n");
return;
}
SNode_p p=S;
while(p!=NULL)
{
printf("%3d",p->data);
p=p->next;
}
putchar(10);
}
main.c文件
#include "Link_Stack.h"
int main()
{
//申请栈顶指针
SNode_p S=NULL;
printf("入栈5个元素:\n");
push(&S,1);//入栈
push(&S,2);
push(&S,3);
push(&S,4);
push(&S,5);
print_stack(S);
printf("出栈2个元素:\n");
pop(&S);//出栈
pop(&S);
print_stack(S);
return 0;
}