标签:共享栈
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int dataType;
#define MAXSIZE 8
typedef struct shareStack
{
dataType data[MAXSIZE];
int leftTop;
int rightTop;
}shareStack;
void initshareStack(shareStack *s)
{
s->leftTop=-1;
s->rightTop=MAXSIZE;
memset(s->data, 0, sizeof(int)*MAXSIZE);
}
void push(shareStack *s,int e,int stackType)
{
if (s->leftTop+1==s->rightTop)
{
return;
}
if (stackType==1)
{
s->leftTop++;
s->data[s->leftTop]=e;
}
if (stackType==2) {
s->rightTop--;
s->data[s->rightTop]=e;
printf("%d",e);
}
}
void pop(shareStack *s,int *e,int stackType)
{
if (stackType==1)
{
if (s->leftTop==-1)
{
return;
}
*e=s->data[s->leftTop];
s->leftTop--;
}
if (stackType==2)
{
if (s->rightTop==MAXSIZE)
{
return;
}
*e=s->data[s->rightTop];
s->rightTop++;
}
}
int main(void)
{
shareStack s;
initshareStack(&s);
int temp;
printf("Push Order:\n");
for (int i=0; i<8; i++) {
push(&s, i, 2);
}
printf("\n");
printf("Pop Order:\n");
for (int i=0; i<8; i++) {
pop(&s, &temp, 2);
printf("%d",temp);
}
printf("\n");
return 0;
}本文出自 “网络学习总结” 博客,请务必保留此出处http://8947509.blog.51cto.com/8937509/1550952
标签:共享栈
原文地址:http://8947509.blog.51cto.com/8937509/1550952