C语言实现双向共享栈

ShareStack.h

#pragma once
/*
顺序表的两端分别作为双栈的栈底,当整个线性表没有空间时,双栈满
*/
#define MAX_SIZE 20
#define ERROR -1
#define OVERFLOW -2
#define LEFT 0
#define RIGHT 1
typedef char ElemType;

typedef struct {
	ElemType* base;
	ElemType* l_top;
	ElemType* r_top;
}s_stack;

void InitStack(s_stack& S);//初始化栈
void DestroyStack(s_stack& S);//销毁栈
bool EmptyStack(s_stack S,int stack_num);//判断栈是否为空
void ClearStack(s_stack& S);//清空栈
int SizeStack(s_stack S,int stack_num);//栈内元素数量
void Push(s_stack& S, int stack_num, ElemType e);//入栈
void Pop(s_stack& S, int stack_num, ElemType& e);//出栈
void TraverseStack(s_stack S,int stack_num,void(*f)(ElemType e));//遍历栈

ShareStack.c

#include <stdlib.h>
#include <stdio.h>
#include "ShareStack.h"

void InitStack(s_stack& S) {
	S.base =(ElemType*) malloc(MAX_SIZE * sizeof(ElemType));
	if (!S.base) exit(ERROR);
	S.l_top = S.base;
	S.r_top = S.base + MAX_SIZE - 1;
}//初始化栈
void DestroyStack(s_stack& S) {
	free(S.base);
	S.l_top = S.r_top = NULL;
}//销毁栈
bool EmptyStack(s_stack S, int stack_num) {
	if (stack_num == LEFT) return S.base == S.l_top ? true : false;
	else if (stack_num == RIGHT) return S.base == S.r_top - MAX_SIZE + 1 ? true : false;
	else exit(ERROR);
}//判断栈是否为空
void ClearStack(s_stack& S) {
	S.l_top = S.base;
	S.r_top = S.base + MAX_SIZE - 1;
}//清空栈
int SizeStack(s_stack S, int stack_num) {
	if (stack_num == LEFT) return S.l_top - S.base;
	else if (stack_num == RIGHT) return S.base + MAX_SIZE - 1 - S.r_top;
	else exit(ERROR);
}//栈内元素数量
void Push(s_stack& S, int stack_num, ElemType e) {
	if (S.l_top > S.r_top)//判断共享栈是不是已满
		exit(OVERFLOW);
	if (stack_num == LEFT) {
		*(S.l_top++)=e;
	}
	else if (stack_num == RIGHT) {
		*(S.r_top--) = e;
	}
	else {
		exit(ERROR);
	}
}//入栈
void Pop(s_stack& S, int stack_num, ElemType& e) {
	if (EmptyStack(S, stack_num)) exit(ERROR);
	if (stack_num == LEFT) {
		e = *(--S.l_top);
	}
	else if (stack_num == RIGHT) {
		e = *(++S.r_top);
	}
	else {
		exit(ERROR);
	}
}//出栈
void TraverseStack(s_stack S, int stack_num, void(*f)(ElemType e)) {
	ElemType* p;
	if (stack_num == LEFT) { 
		printf("stack 0:\n");
		p = S.l_top;
		while(p-- != S.base) {
			f(*(p));
		}
	}
	else if (stack_num == RIGHT) { 
		printf("stack 1:\n"); 
		p = S.r_top;
		while (p++!=S.base+MAX_SIZE-1)
		{
			f(*(p));
		}
	}
	else { 
		exit(ERROR); 
	}
}//遍历栈

main.c(用于测试)

#include <stdlib.h>
#include <stdio.h>
#include "ShareStack.h"

void InitStack(s_stack& S) {
	S.base =(ElemType*) malloc(MAX_SIZE * sizeof(ElemType));
	if (!S.base) exit(ERROR);
	S.l_top = S.base;
	S.r_top = S.base + MAX_SIZE - 1;
}//初始化栈
void DestroyStack(s_stack& S) {
	free(S.base);
	S.l_top = S.r_top = NULL;
}//销毁栈
bool EmptyStack(s_stack S, int stack_num) {
	if (stack_num == LEFT) return S.base == S.l_top ? true : false;
	else if (stack_num == RIGHT) return S.base == S.r_top - MAX_SIZE + 1 ? true : false;
	else exit(ERROR);
}//判断栈是否为空
void ClearStack(s_stack& S) {
	S.l_top = S.base;
	S.r_top = S.base + MAX_SIZE - 1;
}//清空栈
int SizeStack(s_stack S, int stack_num) {
	if (stack_num == LEFT) return S.l_top - S.base;
	else if (stack_num == RIGHT) return S.base + MAX_SIZE - 1 - S.r_top;
	else exit(ERROR);
}//栈内元素数量
void Push(s_stack& S, int stack_num, ElemType e) {
	if (S.l_top > S.r_top)//判断共享栈是不是已满
		exit(OVERFLOW);
	if (stack_num == LEFT) {
		*(S.l_top++)=e;
	}
	else if (stack_num == RIGHT) {
		*(S.r_top--) = e;
	}
	else {
		exit(ERROR);
	}
}//入栈
void Pop(s_stack& S, int stack_num, ElemType& e) {
	if (EmptyStack(S, stack_num)) exit(ERROR);
	if (stack_num == LEFT) {
		e = *(--S.l_top);
	}
	else if (stack_num == RIGHT) {
		e = *(++S.r_top);
	}
	else {
		exit(ERROR);
	}
}//出栈
void TraverseStack(s_stack S, int stack_num, void(*f)(ElemType e)) {
	ElemType* p;
	if (stack_num == LEFT) { 
		printf("stack 0:\n");
		p = S.l_top;
		while(p-- != S.base) {
			f(*(p));
		}
	}
	else if (stack_num == RIGHT) { 
		printf("stack 1:\n"); 
		p = S.r_top;
		while (p++!=S.base+MAX_SIZE-1)
		{
			f(*(p));
		}
	}
	else { 
		exit(ERROR); 
	}
}//遍历栈

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值