目前网站开发怎么兼顾手机,建设公司取名字大全最新,网络推广员是什么工作,天津圣辉友联网站建设两个相同类型的栈#xff0c;可能第一个栈已经满了#xff0c;但是第二个栈还是空的#xff0c;将两个相同类型的栈合并在一起#xff0c;可以节省一部分空间。
数组有两个端点#xff0c;分别为两个栈的栈底#xff0c;一个栈的栈底的位置为数组为0的地方#xff0c;另… 两个相同类型的栈可能第一个栈已经满了但是第二个栈还是空的将两个相同类型的栈合并在一起可以节省一部分空间。
数组有两个端点分别为两个栈的栈底一个栈的栈底的位置为数组为0的地方另一个栈的栈底为数组下标为SIZE-1的位置SIZE为数组的长度。两个栈如果增加元素就是两端点向中间延伸。
只要两个栈顶指针不碰面元素就可以一直进栈。 头文件
#pragma once
//顺序栈
#define MAXSIZE 10
typedef struct DoubleStack
{int *elem;//数据int top1;//左边栈的栈顶指针int top2;//右边栈的栈顶指针
}DoubleStack,* PDoubleStack;
//初始化
void InitDoubleStack(PDoubleStack ps);
//入栈
bool Push(PDoubleStack ps,int val,int num);
//出栈
bool Pop(PDoubleStack ps,int *rtval,int num);
//清空
bool Clear(PDoubleStack ps);
//销毁
void Destroy(PDoubleStack ps); cpp文件
#includestdio.h
#includeassert.h
#includestdlib.h
#includeDoubleStack.hvoid InitDoubleStack(PDoubleStack ps)
{ps-elem (int *) malloc (MAXSIZE *sizeof(int));ps-top1 0;ps-top2 MAXSIZE-1;
}static bool IsFull(PDoubleStack ps)
{return ps-top11 ps-top2;
}bool IsEmpty(PDoubleStack ps)
{if(!(ps-top1 0 ps-top2 0 )){return false;}return true;
}bool Push(PDoubleStack ps,int val,int num)
{assert(ps ! NULL);if(ps NULL){return NULL;}if(IsFull(ps)){return false;}if(num 1){ps-elem[ps-top1] val;ps-top1;}else if(num 2){ps-elem[ps-top2] val;ps-top2--;}return true;
}
//num表示的是要进哪个栈
bool Pop(PDoubleStack ps,int *rtval,int num)
{assert(ps ! NULL rtval ! NULL);if(ps NULL){return NULL;}if(!(IsEmpty(ps))){if(num 1){*rtval ps-elem[--ps-top1];}else if(num 2){*rtval ps-elem[ps-top2];}return true;}return false;
}bool Clear(PDoubleStack ps)
{ps-top1 0;ps-top2 MAXSIZE -1;return true;
}void Destroy(PDoubleStack ps)
{free(ps-elem);ps - top1 0;ps - top2 0;
} 主函数
#includestdlib.h
#includeDoubleStack.hint main()
{DoubleStack sta;InitDoubleStack(sta);for(int i 0;i7;i){Push(sta,i,1);}for(int i 0;i2;i){Push(sta,i10,2);}int rtval -1;Pop(sta,rtval,1);printf(%d\n,rtval);for(int i 0;i10;i){printf(%d\n,sta.elem[i]);}Destroy(sta);return 0;
}