首页 > 学院 > 开发设计 > 正文

两栈共享空间

2019-11-11 04:25:23
字体:
来源:转载
供稿:网友
#include <stdio.h>#include <malloc.h>#include <stdio.h>#define MAXSIZE 10#define ERROR false#define OK truetypedef int sElemType;typedef struct {	sElemType data[MAXSIZE];	int top1;//栈顶一	int top2;//栈顶二} sqDoubleStack;bool Push(sqDoubleStack* stack, sElemType e, int stackNumber) {	if (stack->top1 + 1 == stack->top2) {		return ERROR;  //栈以满	}	if (stackNumber == 1) {		stack->top1++;		stack->data[stack->top1] = e;	} else if (stackNumber == 2) {		stack->top2--;		stack->data[stack->top2] = e;	}	return OK;}bool Pop(sqDoubleStack* stack, int stackNumber, sElemType* e) {	if (stackNumber == 1) {		if (stack->top1 == -1) {			return ERROR;//空栈		}		*e = stack->data[stack->top1];		stack->top1--;	} else if (stackNumber == 2) {		if (stack->top2 == MAXSIZE) {			return ERROR;  //空栈		}		*e = stack->data[stack->top2];		stack->top2++;	}	return OK;}int main() {	sqDoubleStack* stack = (sqDoubleStack*) malloc(sizeof(sqDoubleStack));	stack->top1 = -1;	stack->top2 = MAXSIZE;	for (int i = 0; i < 5; i++) {		Push(stack, i, 1);	}	for (int i = 5; i < 10; i++) {		Push(stack, i, 2);	}	sElemType e;	//出栈 	PRintf("一号栈/n");	for (int i = 0; i < 5; i++) {		if (Pop(stack, 1, &e))			printf("%d/n", e);	}	printf("二号栈/n");	for (int i = 0; i < 5; i++) {		if (Pop(stack, 2, &e))			printf("%d/n", e);	}	return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表