#include <stdio.h>
#include <stdlib.h>
const int MAX_SIZE = 10;
typedef int DataType;
typedef struct
{
DataType data[MAX_SIZE];
int top1;
int top2;
}BothStack;
void Initial(BothStack* bs);
void push(BothStack* bs, int i, DataType x);
DataType pull(BothStack* bs, int i);
void print(BothStack* bs, int i);
int main()
{
BothStack bs;
Initial(&bs);
push(&bs, 2, 2);
print(&bs, 2);
push(&bs, 2, 3);
print(&bs, 2);
system("pause");
return 0;
}
void Initial(BothStack* bs)
{
bs->top1 = -1;
bs->top2 = MAX_SIZE;
}
void push(BothStack* bs, int i, DataType x)
{
if (bs->top1 + 1 == bs->top2)
{
printf("栈已满\n");
}
else
{
if (1 == i)
{
bs->top1++;
bs->data[bs->top1] = x;
printf("栈1插入成功!\n");
}
else if (2 == i)
{
bs->top2--;
bs->data[bs->top2] = x;
printf("栈2插入成功!\n");
}
else
{
printf("请输入正确的栈标号!\n");
}
}
return;
}
DataType pull(BothStack* bs, int i)
{
DataType x;
if (1 == i)
{
if (bs->top1 == -1)
{
printf("栈1为空\n");
}
else
{
x = bs->data[bs->top1];
bs->top1--;
}
}
else if (2 == i)
{
if (bs->top2 == MAX_SIZE)
{
printf("栈2为空!\n");
}
else
{
x = bs->data[bs->top2];
bs->top2++;
}
}
else
{
printf("请输入正确的栈标号!\n");
}
return x;
}
void print(BothStack* bs, int i)
{
if (1 == i)
{
if (bs->top1 == -1)
{
printf("栈1为空栈,不可遍历!\n");
}
else
{
while (bs->top1 != -1)
{
printf("%d\n", bs->data[bs->top1]);
bs->top1--;
}
}
}
else
{
if (bs->top2 == MAX_SIZE)
{
printf("栈2为空栈,不可遍历!\n");
}
else
{
while (bs->top2 != MAX_SIZE)
{
printf("%d\n", bs->data[bs->top2]);
bs->top2++;
}
}
}
}
|