一个数组实现两个堆栈(顺序存储)

1.结构体定义

#define MaxSize 10//<存储数据元素的最大个数>
typedef int ElementType;

typedef struct DStack *stack1;
struct DStack
{
	ElementType Data[MaxSize];
	int top1;
	int top2;
};
stack1 S;

2.初始化

记牢两个栈为空的条件:
S->top1 =-1;
S->top2 = MaxSize;

stack1 CreatDStack()
{
	S = (stack1)malloc(sizeof(struct DStack));
	S->top1 =-1;
	S->top2 = MaxSize;
	return S;
}

3.入栈

void Push(struct DStack *Ptrs ,ElementType item, int Tag)
{
	if(Ptrs->top1 - Ptrs->top2 == 1)
	{
		printf("栈已满\n");
		return ;
	}
	else
	{
		if(Tag == 1)
		{
			Ptrs->Data[++(Ptrs->top1)] = item;
		}
		else
			Ptrs->Data[--(Ptrs->top2)] = item;
	}
}

4.出栈

ElementType Pop(struct DStack *Ptrs , int Tag)
{
	if(Tag ==1)
	{
		if(Ptrs->top1 ==-1)
		{
			printf("栈1已空,不可出栈\n");
			return NULL;
		}
		else
			return Ptrs->Data[(Ptrs->top1)--];
	}
	else
	{
		if(Ptrs->top2 == MaxSize)
		{
			printf("栈2已空,不可出栈\n");
			return NULL;
		}
		else
			return Ptrs->Data[(Ptrs->top1)++];
	}

		
}

5.输出全部元素

void print()
{
	if(S->top1 ==-1 && S->top2 ==MaxSize)
	{
		printf("栈1为:NULL\n栈2为:NULL\n");
		return;
	}
	if(S->top1 ==-1)
	{
		printf("栈1为:NULL\n");
		return;
	}
		
	else
	{
		int temp = 0;
		printf("栈1为: ");
		while(temp<=S->top1)
		{
			printf("%d ",S->Data[temp++]);
		}
		printf("\n");
	}
	if(S->top2 ==MaxSize)
	{
		printf("栈2为:NULL\n");
		return;
	}
		
	else
	{
		int temp = MaxSize - 1;
		printf("栈2为: ");
		while(temp>=S->top2)
		{
			printf("%d ",S->Data[temp--]);
		}
		printf("\n");
	}
}

6.测试

S = CreatDStack();
	print();
	Push(S ,5, 1);
	Push(S ,12, 1);
	
	Push(S ,325, 1);
	Push(S ,54, 1);
	Push(S ,32, 2);
	Push(S ,623, 2);
	Push(S ,23, 2);
	Push(S ,445, 2);
	//Push(S ,15, 2);
	print();

结果
在这里插入图片描述

7.全部代码

#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>

#define MaxSize 8//<存储数据元素的最大个数>
typedef int ElementType;

typedef struct DStack *stack1;
struct DStack
{
	ElementType Data[MaxSize];
	int top1;
	int top2;
};
stack1 S;

//初始化
stack1 CreatDStack()
{
	S = (stack1)malloc(sizeof(struct DStack));
	S->top1 =-1;
	S->top2 = MaxSize;
	return S;
}

//入栈
void Push(struct DStack *Ptrs ,ElementType item, int Tag)
{
	if(Ptrs->top2 - Ptrs->top1 == 1)
	{
		printf("栈已满\n");
		return ;
	}
	else
	{
		if(Tag == 1)
		{
			Ptrs->Data[++(Ptrs->top1)] = item;
		}
		else
			Ptrs->Data[--(Ptrs->top2)] = item;
	}
}

//出栈
ElementType Pop(struct DStack *Ptrs , int Tag)
{
	if(Tag ==1)
	{
		if(Ptrs->top1 ==-1)
		{
			printf("栈1已空,不可出栈\n");
			return NULL;
		}
		else
			return Ptrs->Data[(Ptrs->top1)--];
	}
	else
	{
		if(Ptrs->top2 == MaxSize)
		{
			printf("栈2已空,不可出栈\n");
			return NULL;
		}
		else
			return Ptrs->Data[(Ptrs->top1)++];
	}

		
}
void print()
{
	if(S->top1 ==-1 && S->top2 ==MaxSize)
	{
		printf("栈1为:NULL\n栈2为:NULL\n");
		return;
	}
	if(S->top1 ==-1)
	{
		printf("栈1为:NULL\n");
		return;
	}
		
	else
	{
		int temp = 0;
		printf("栈1为: ");
		while(temp<=S->top1)
		{
			printf("%d ",S->Data[temp++]);
		}
		printf("\n");
	}
	if(S->top2 ==MaxSize)
	{
		printf("栈2为:NULL\n");
		return;
	}
		
	else
	{
		int temp = MaxSize - 1;
		printf("栈2为: ");
		while(temp>=S->top2)
		{
			printf("%d ",S->Data[temp--]);
		}
		printf("\n");
	}
}

int main()
{
	S = CreatDStack();
	print();
	Push(S ,5, 1);
	Push(S ,12, 1);
	
	Push(S ,325, 1);
	Push(S ,54, 1);
	Push(S ,32, 2);
	Push(S ,623, 2);
	Push(S ,23, 2);
	Push(S ,445, 2);
	//Push(S ,15, 2);
	print();
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值