在一个数组中实现两个堆栈


Data是指针数组,里头有两个顺序栈(top1,top2为栈顶下标,分别往前往后入栈)

#include <stdio.h>
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;

#define ERROR 1e8
typedef int ElementType;
typedef enum { push, pop, END } Operation;
typedef int Position;
struct SNode {
	//指针Data表示的数组,堆栈并不用结构体封装,而是在数组中根据栈顶下标找元素
	//*(Data+i)=Data[i]
	ElementType *Data;
	//Top1是堆栈1的栈顶下标(在数组中的位置)Top2是堆栈2的的
	Position Top1, Top2;
	int MaxSize;
};
typedef struct SNode *Stack;

Stack CreateStack(int MaxSize);
bool Push(Stack S, ElementType X, int Tag);
ElementType Pop(Stack S, int Tag);

Operation GetOp();  /* details omitted */
void PrintStack(Stack S, int Tag); /* details omitted */

int main()
{
	int N, Tag, X;
	Stack S;
	scanf("%d", &N);
	S = CreateStack(N);
	while (1) {
		switch (GetOp()) {
		case push:
			scanf("%d %d", &Tag, &X);
			if (!Push(S, X, Tag)) printf("Stack %d is Full!\n", Tag);
			break;
		case pop:
			scanf("%d", &Tag);
			X = Pop(S, Tag);
			if (X == ERROR) printf("Stack %d is Empty!\n", Tag);
			break;
		case END:
			PrintStack(S, 1);
			PrintStack(S, 2);
			system("PAUSE");
			return 0;
		}
	}

}

/* 你的代码将被嵌在这里 */
Stack CreateStack(int MaxSize){
	Stack L = (Stack)malloc(sizeof(struct SNode));
	L->MaxSize = MaxSize;
	//Top1按照单链表往后堆堆栈(+),Top2按照链栈往前堆堆栈(-)
	//它们的范围是-1~MaxSize(不包含支点)
	//数组用*Data表示,范围是Data[0]~Data[MaxSize-1]
	//所以当top2==(top1+1)时数组就满了,这样就避免了数组超限
	L->Top1 = -1;
	L->Top2 = MaxSize;
	L->Data = (ElementType *)malloc(sizeof(ElementType)*MaxSize);
	return L;
}
bool Push(Stack S, ElementType X, int Tag){
	if (S == NULL)return false;//未创建栈
	if ((S->Top1 + 1) >= S->Top2){//栈满
		printf("Stack Full\n");
		return false;
	}
	if (Tag == 1){
		++S->Top1;
		*(S->Data + S->Top1) = X;//*(S->Data + S->Top1)等同于S->Data[S->Top1]
	}
	else{
		//可以简化成S->Data[--S->Top2]
		--S->Top2;
		*(S->Data + S->Top2) = X;
	}
	return true;
}
//出栈,如果栈空返回ERROR,否则返回Pop出的那个元素(先返回再改变栈顶下标)
//先返回再改变栈顶下标:只能写S->Data[S->Top1(2)++(--)]这样的的,此处用了两种表达方式(指针和数组)
ElementType Pop(Stack S, int Tag){
	if (S == NULL)return ERROR;//未创建栈
	if (Tag == 1){
		if (S->Top1 == -1){//栈空
			printf("Stack %d Empty\n", Tag);
			return ERROR;
		}

		return *(S->Data + S->Top1--);
	}
	else{
		if (S->Top2 == S->MaxSize){//栈空
			printf("Stack %d Empty\n", Tag);
			return ERROR;
		}

		return S->Data[S->Top2++];
	}
}
Operation GetOp(){
	string op[3] = { "Push", "Pop", "End" }, str;
	cin >> str;
	if (str == op[0])return push;
	if (str == op[1])return pop;
	if (str == op[2])return END;
}
void PrintStack(Stack S, int Tag){
	printf("Pop from Stack %d:", Tag);
	if (Tag == 1){
		while (S->Top1 != -1){
			printf(" %d", S->Data[S->Top1--]);
		}
	}
	else{
		while (S->Top2 != S->MaxSize){
			printf(" %d", S->Data[S->Top2++]);
		}
	}
	printf("\n");
}

输入,用枚举返回不同的指令

enum枚举类型

typedef enum { push, pop, END } Operation;

string输入用cin,重载后可以用>,<,!=,==直接比较字符串
头文件加iostream,string,别忘了using namespace std;

Operation GetOp(){
	string op[3] = { "Push", "Pop", "End" }, str;
	cin >> str;
	if (str == op[0])return push;
	if (str == op[1])return pop;
	if (str == op[2])return END;
}

主函数

switch只能应用于int,char,枚举;如果想指向字符串就得创建返回字符串的枚举类型

int main()
{
	int N, Tag, X;
	Stack S;
	int done = 0;

	scanf("%d", &N);
	S = CreateStack(N);
	while (1) {
		switch (GetOp()) {
		case push:
			
			

			break;
		case pop:
			


			break;
		case END:



			system("PAUSE");
			return 0;
		}
	}

}

0.结构体

Data是指针数组,两个顺序栈就在Data中

struct SNode {
	//指针Data表示的数组,堆栈并不用结构体封装,而是在数组中根据栈顶下标找元素
	//*(Data+i)=Data[i]
	ElementType *Data;
	//Top1是堆栈1的栈顶下标(在数组中的位置)Top2是堆栈2的的
	Position Top1, Top2;
	int MaxSize;
};

1.创建

开辟一个该结构体的存储空间
它的Data开辟一个指针数组的存储空间;
Top1指向-1,Top2指向MaxSize避免了入栈时的数组超限;
L->MaxSize等于MaxSize是为了出栈时判断Top2是否为空

typedef int ElementType;
typedef struct SNode *Stack;


Stack CreateStack(int MaxSize){
	Stack L = (Stack)malloc(sizeof(struct SNode));
	L->MaxSize = MaxSize;
	//Top1按照单链表往后堆堆栈(+),Top2按照链栈往前堆堆栈(-)
	//它们的范围是-1~MaxSize(不包含支点)
	//数组用*Data表示,范围是Data[0]~Data[MaxSize-1]
	//所以当top2==(top1+1)时数组就满了,这样就避免了数组超限
	L->Top1 = -1;
	L->Top2 = MaxSize;
	L->Data = (ElementType *)malloc(sizeof(ElementType)*MaxSize);
	return L;
}

主函数

scanf("%d", &N);
	S = CreateStack(N);

2.入栈Push

判断是否有创建结构体;
判断是否栈满;
若不满栈顶下标移位,再赋值

bool Push(Stack S, ElementType X, int Tag){
	if (S == NULL)return false;//未创建结构体
	if ((S->Top1 + 1) >= S->Top2){//栈满
		printf("Stack Full\n");
		return false;
	}
	if (Tag == 1){
		++S->Top1;
		*(S->Data + S->Top1) = X;//*(S->Data + S->Top1)等同于S->Data[S->Top1]
	}
	else{
		//可以简化成S->Data[--S->Top2]
		--S->Top2;
		*(S->Data + S->Top2) = X;
	}
	return true;
}

主函数

			case push:
			scanf("%d %d", &Tag, &X);
			if (!Push(S, X, Tag)) printf("Stack %d is Full!\n", Tag);
			break;

3.出栈Pop

判断是否有创建结构体;
判断是否栈空;
否则返回出栈的栈顶下标的内容再让栈顶下标移位
因为要先返回再移位,所以只能写S->Data[S->Top1(2)++(–)]这样的的,此处用了两种表达方式(指针和数组)
指针:return *(S->Data + S->Top1–);
数组:return S->Data[S->Top2++];

typedef int ElementType;
typedef struct SNode *Stack;


ElementType Pop(Stack S, int Tag){
	if (S == NULL)return ERROR;//未创建结构体
	if (Tag == 1){
		if (S->Top1 == -1){//栈空
			printf("Stack %d Empty\n", Tag);
			return ERROR;
		}

		return *(S->Data + S->Top1--);
	}
	else{
		if (S->Top2 == S->MaxSize){//栈空
			printf("Stack %d Empty\n", Tag);
			return ERROR;
		}

		return S->Data[S->Top2++];
	}
}

主函数

case pop:
			scanf("%d", &Tag);
			X = Pop(S, Tag);
			if (X == ERROR) printf("Stack %d is Empty!\n", Tag);
			break;

5.结束并输出两个顺序栈

这里也是先输出栈顶下标的元素再移位

void PrintStack(Stack S, int Tag){
	printf("Pop from Stack %d:", Tag);
	if (Tag == 1){
		while (S->Top1 != -1){
			printf(" %d", S->Data[S->Top1--]);
		}
	}
	else{
		while (S->Top2 != S->MaxSize){
			printf(" %d", S->Data[S->Top2++]);
		}
	}
	printf("\n");
}```
## 主函数

```c
case END:
			PrintStack(S, 1);
			PrintStack(S, 2);
			system("PAUSE");
			return 0;
		}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Deosiree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值