当前位置:网站首页>Space shared by two stacks
Space shared by two stacks
2022-07-23 11:59:00 【Look, that's a licking dog】
#include <stdio.h>
#include <malloc.h>
#define MAXSIZE 20
typedef int T;
typedef struct DoubleStack
{
T data[MAXSIZE];
int top1;// head
int top2;// tail
}*dostack;
bool InitStack(dostack s)// initialization
{
if (!s)
return false;
s->top1 = 0;
s->top2 = MAXSIZE - 1;
return true;
}
bool ClearStack(dostack s)// Clear the stack
{
if (!s)
return false;
s->top1 = 0;
s->top2 = MAXSIZE - 1;
return true;
}
bool Is_Empty(dostack s)// Sentenced to empty
{
if (s->top1 == 0 && s->top2 == MAXSIZE - 1)
return true;
else return false;
}
bool Is_Full(dostack s)// Full sentence
{
if (s->top1 + 1 == s->top2)
return true;
else return false;
}
int GetLength(dostack s)// The number of data
{
return s->top1 + MAXSIZE - s->top2;
}
bool Push(dostack s, T val, int num)// Select stack ,1 To the left ,2 For the right
{
if (Is_Full(s))
return false;
if (num == 1)
{
s->data[s->top1++] = val;
}
else if (num == 2)
{
s->data[s->top2--] = val;
}
else false;
return true;
}
T Pop(dostack s,int num)// Select stack ,1 Bit left ,2 For the right
{
if (Is_Empty(s))
return -1;
T tmp;
if (num == 1)
{
if (s->top1 == 0)
return -1;
tmp = s->data[--s->top1];
}
else if (num == 2)
{
if (s->top2 == MAXSIZE - 1)
return -1;
tmp = s->data[++s->top2];
}
else return -1;
return tmp;
}
T GetVal(dostack s, int num)// Get the desired position stack top element
{
if (Is_Empty(s))
return -1;
T tmp;
if (num == 1)
{
if (s->top1 == 0)
return -1;
tmp = s->data[s->top1];
}
else if (num == 2)
{
if (s->top2 == MAXSIZE - 1)
return -1;
tmp = s->data[s->top2];
}
else return -1;
return tmp;
}
void Show(dostack s)// Traverse
{
int i = 0;
while (i < s->top1)
{
printf("%d ", s->data[i++]);
}
printf("\t");
i = s->top2 + 1;
while (i < MAXSIZE)
{
printf("%d ", s->data[i++]);
}
printf("\n");
}
int main()
{
dostack s;
s = (dostack)malloc(sizeof(dostack));
InitStack(s);
for (int i = 1; i <= 5; i++)
{
Push(s, i, 1);
Push(s, i, 2);
}
Show(s);
printf("%d\t", Pop(s, 1));
printf("%d\n", Pop(s, 2));
Show(s);
return 0;
}
边栏推荐
猜你喜欢
随机推荐
10. I/o input / output stream
百变冰冰!使用飞桨的PaddleGAN实现妆容迁移
静态链表
Ten year structure five year Life-02 first job
LearnOpenGL - Introduction
双端队列
两个栈共用空间
Modify the root password of MySQL
Data warehouse 4.0 notes - user behavior data collection I
The user logs in continuously (interruption is allowed) to query SQL
1、MySQL初体验
Introduction to the process structure used by activiti workflow
MySQL backup
MySQL视图
九、实用类
NT68661-屏参升级-RK3128-开机自己升级屏参
Kubesphere ha install (II)
Mysql database
高德定位---权限弹框不出现的问题
Service服务







