首页 > 代码库 > 共享栈
共享栈
#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
共享栈
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。