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;
}