顺序存储结构栈
#include<stdio.h>
//栈的顺序结构
#define MAXSIZE 100
#define SIZE sizeof(stack)
typedef struct {
int data[MAXSIZE];
int top;
}stack;
stack *initStack();
int Empty(stack *s);
int pushStack(stack *s, int d);
int popStack(stack *s, int *d);
int topStack(stack *s, int *d);
int main(){
stack *s = initStack();
pushStack(s,1);
pushStack(s,2);
pushStack(s,3);
while (!Empty(s)) {
int data;
topStack(s,&data);
printf("top=%d,", data);
popStack(s,&data);
printf("pop=%d\n", data);
}
return 0;
}
//栈初始化
stack *initStack(){
stack *p;
p = (stack*)malloc(SIZE);
p->top = -1;
return p;
}
//判空
int Empty(stack *s){
if(s->top == -1)
return 1;
return 0;
}
//入栈
int pushStack(stack *s, int d){
if (s->top == MAXSIZE-1)
return 0;
s->top++;
s->data[s->top] = d;
return 1;
}
//出栈
int popStack(stack *s, int *d){
if(Empty(s))
return 0;
*d = s->data[s->top];
s->top--;
return 1;
}
//读取栈顶元素
int topStack(stack *s, int *d){
if(Empty(s))
return 0;
*d = s->data[s->top];
return 1;
}
共享栈
#include<stdio.h>
#define MAXSIZE 100
#define SIZE sizeof(dupsqstack)
typedef struct {
int data[MAXSIZE];
int lefttop;
int righttop;
}dupsqstack;
dupsqstack *initDupsqstack();
int Empty(dupsqstack *s, char status);
int pushDupStack(dupsqstack *s, char status, int d);
int popDupStack(dupsqstack *s, char status, int *d);
int topDupStack(dupsqstack *s, char status, int *d);
int main(){
dupsqstack *s = initDupsqstack();
printf("emptyL-%d,emptyR-%d\n", Empty(s,'L'), Empty(s,'R'));
pushDupStack(s,'L',1);
pushDupStack(s,'L',2);
pushDupStack(s,'L',3);
pushDupStack(s,'R',1);
pushDupStack(s,'R',2);
pushDupStack(s,'R',3);
while (!Empty(s,'L') && !Empty(s,'R')) {
int l,r;
topDupStack(s,'L', &l);
topDupStack(s,'R', &r);
printf("topL=%d,topR=%d\n", l, r);
popDupStack(s,'L', &l);
popDupStack(s,'R', &r);
printf("popL=%d,popR=%d\n", l, r);
}
return 0;
}
//栈初始化
dupsqstack *initDupsqstack(){
dupsqstack *s = (dupsqstack*)malloc(SIZE);
s->lefttop = -1;
s->righttop = MAXSIZE;
return s;
}
//栈判空
int Empty(dupsqstack *s, char status){
switch (status) {
case 'L':
if (s->lefttop == -1)
return 1;
break;
case 'R':
if (s->righttop == MAXSIZE)
return 1;
break;
default:
printf("输入错误\n");
break;
}
return 0;
}
//入栈
int pushDupStack(dupsqstack *s, char status, int d){
if (s->lefttop + 1 == s->righttop)
return 0;
switch (status) {
case 'L':
s->data[++s->lefttop] = d;
break;
case 'R':
s->data[--s->righttop] = d;
break;
default:
printf("输入错误\n");
return 0;
break;
}
return 1;
}
//出栈
int popDupStack(dupsqstack *s, char status, int *d){
if (Empty(s,status))
return 0;
switch (status) {
case 'L':
*d = s->data[s->lefttop--];
break;
case 'R':
*d = s->data[s->righttop++];
break;
default:
printf("输入错误\n");
return 0;
break;
}
return 1;
}
//栈顶元素
int topDupStack(dupsqstack *s, char status, int *d){
if (Empty(s,status))
return 0;
switch (status) {
case 'L':
*d = s->data[s->lefttop];
break;
case 'R':
*d = s->data[s->righttop];
break;
default:
printf("输入错误\n");
return 0;
break;
}
return 1;
}
链式存储结构栈
#include<stdio.h>
#define SIZE sizeof(stackNode)
typedef struct stackNode{
int data;
struct stackNode *next;
}stackNode;
stackNode *initStackList();
int stackEmpty(stackNode *head);
int pushStack(stackNode *head, int d);
int popStack(stackNode *head, int *d);
int topStack(stackNode *head, int *d);
int main(){
stackNode *head = initStackList();
printf("isEmpty = %d\n", stackEmpty(head));
pushStack(head,1);
pushStack(head,2);
pushStack(head,3);
while (!stackEmpty(head)) {
int d;
topStack(head,&d);
printf("top=%d,", d);
popStack(head,&d);
printf("pop=%d\n", d);
}
return 0;
}
//栈链表初始化
stackNode *initStackList(){
stackNode *p = (stackNode*)malloc(SIZE);
p->next = NULL;
return p;
}
//栈链表判空
int stackEmpty(stackNode *head){
if(head->next == NULL)
return 1;
return 0;
}
//栈链表入栈
int pushStack(stackNode *head, int d){
stackNode *new;
if ((new = (stackNode*)malloc(SIZE)) == NULL) {
return 0;
}
new->data = d;
new->next = head->next;
head->next = new;
return 1;
}
//栈链表出栈
int popStack(stackNode *head, int *d){
if (stackEmpty(head)) {
return 0;
}
stackNode *p = head->next;
*d = p->data;
head->next = p->next;
// free(p);
return 1;
}
//栈链表栈顶元素
int topStack(stackNode *head, int *d){
if (stackEmpty(head)) {
return 0;
}
stackNode *p = head->next;
*d = p->data;
return 1;
}