C 언어 양 방향 공유 스 택 실현

4241 단어 데이터 구조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 
#include 
#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:
"); p = S.l_top; while(p-- != S.base) { f(*(p)); } } else if (stack_num == RIGHT) { printf("stack 1:
"); p = S.r_top; while (p++!=S.base+MAX_SIZE-1) { f(*(p)); } } else { exit(ERROR); } }//

main. c (테스트 용)
#include 
#include 
#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:
"); p = S.l_top; while(p-- != S.base) { f(*(p)); } } else if (stack_num == RIGHT) { printf("stack 1:
"); p = S.r_top; while (p++!=S.base+MAX_SIZE-1) { f(*(p)); } } else { exit(ERROR); } }//

좋은 웹페이지 즐겨찾기