栈的实现:https://blog.csdn.net/weixin_41892460/article/details/82973851
SharedStack.h
#ifndef __SHAREDSTACK_H__
#define __SHAREDSTACK_H__
#include<string.h>
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
#include<stdlib.h>
#include<Windows.h>
typedef int DataType;
#define MAX 10
typedef struct SharedStack
{
DataType a[MAX];
int top1;
int top2;
}SharedStack;
void SharedStackInit(SharedStack* s);//
void SharedStackPush(SharedStack* s, int which, DataType x);//
void SharedStackPop(SharedStack* s, int which);//
size_t SharedStackSize(SharedStack* s, int which);//
int SharedStackEmpty(SharedStack* s, int which);
void SharedStackDestory(SharedStack* s, int which);
DataType SharedStackTop(SharedStack* s, int which);//
void text();//
#endif
SharedStack.c
#include "SharedStack.h"
void SharedStackInit(SharedStack* s)
{
assert(s);
s->top1 = 0;//偶数
s->top2 = 1;//奇数
}
void SharedStackDestory(SharedStack* s, int which)
{
assert(1 == which || 2 == which);
if (1 == which)
{
assert(s);
s->top1 = 0;
free(s);
s = NULL;
}
else if (2 == which)
{
assert(s);
s->top2 = 0;
free(s);
s = NULL;
}
}
void SharedStackPush(SharedStack* s, int which, DataType x)
{
assert(1 == which || 2 == which);
if (1 == which)
{
assert(s);
if (s->top1 < MAX)
{
s->a[s->top1] = x;
s->top1 += 2;
}
}
else if (2 == which)
{
assert(s);
if (s->top2 < MAX)
{
s->a[s->top2] = x;
s->top2 += 2;
}
}
}
void SharedStackPop(SharedStack* s, int which)
{
assert(1 == which || 2 == which);
if (1 == which)
{
assert(s);
assert(s->top1 > 0);
s->top1 -= 2;
}
else if (2 == which)
{
assert(s);
assert(s->top2 > 0);
s->top2 -= 2;
}
}
DataType SharedStackTop(SharedStack* s, int which)
{
assert(1 == which || 2 == which);
if (1 == which)
{
assert(s);
assert(s->top1 > 0);
return s->a[s->top1 - 2];
}
else if (2 == which)
{
assert(s);
assert(s->top2 > 0);
return s->a[s->top2 - 2];
}
}
size_t SharedStackSize(SharedStack* s, int which)
{
assert(s);
assert(1 == which || 2 == which);
return which == 1 ? (s->top1) / 2 : (s->top2 - 1) / 2;
}
int SharedStackEmpty(SharedStack* s, int which)
{
assert(1 == which || 2 == which);
if (1 == which)
{
assert(s);
return s->top1 == 0 ? 0 : 1;
}
else if (2 == which)
{
assert(s);
return s->top2 == 1 ? 0 : 1;
}
}
Test.c
#include "SharedStack.h"
void text()
{
SharedStack s;
SharedStackInit(&s);
//SharedStackPush(&s, 1, 1);
//SharedStackPush(&s, 1, 2);
//SharedStackPush(&s, 1, 3);
//SharedStackPush(&s, 1, 4);
SharedStackPush(&s, 2, 7);
SharedStackPush(&s, 2, 8);
SharedStackPush(&s, 2, 9);
SharedStackPush(&s, 2, 6);
/*printf("%d\n", SharedStackEmpty(&s, 1));*/
printf("%d\n", SharedStackEmpty(&s, 2));
/* SharedStackDestory(&s, 2);*/
//printf("%d\n", SharedStackTop(&s, 1));
//printf("%d\n", SharedStackTop(&s, 2));
//printf("%d\n", SharedStackSize(&s, 1));
//printf("%d\n", SharedStackSize(&s, 2));
//while (SharedStackEmpty(&s, 1) != 0)
//{
// printf("%d ", SharedStackTop(&s,1));
// SharedStackPop(&s, 1);
//}
//printf("\n");
while (SharedStackEmpty(&s, 2) != 0)
{
printf("%d ", SharedStackTop(&s, 2));
SharedStackPop(&s, 2);
}
printf("\n");
}
int main()
{
text();
system("pause");
return 0;
}