导航:首页 > 源码编译 > 编写算法实现顺序栈的出栈操作

编写算法实现顺序栈的出栈操作

发布时间:2023-02-15 15:43:55

⑴ 编写函数实现的顺序栈的初始化、入栈、出栈、判断栈是否为空的算法,并应用与表达式的括号匹配检测

STL的


#include<iostream>
#include<stack>
using namespace std;

stack<int> s;//可放在main内

int main()
{
int i;
for(i=0;i<10;i++)
s.push(i+1);//向栈顶赋值
s.top()++;//对栈顶元素进行操作
while(!s.empty())//非空
{
cout<<s.top()<<endl;//输出栈顶元素
s.pop();
}
return 0;
}

⑵ 建立顺序存储的栈,并对之进行入栈、出栈、取栈顶元素操作的c语言算法

#include "process.h"
#include "stdio.h"
#include "assert.h"

const int stackIncreament=20; //栈溢出时扩展空间的增量

class SeqStack
{
private:
int top; //栈顶指针
int MAX; //栈的最大可容纳个数
int *elements; //存放栈中元素的栈数组
void overflowProcess(); //栈的溢出处理
public:
SeqStack(int sz=50); //构造函数
~SeqStack() { delete[]elements; } //析构函数
bool pop1(int & x); //元素出栈
void push1(const int & x); //新元素进栈
bool IsEmpty1()const; //判断栈空与否
bool IsFull1()const; //判断栈满与否
void output1(); //输出元素进栈顺序
void output(int x); //输出x

};

SeqStack::SeqStack(int sz):top(-1),MAX(sz)
{
elements=new int[MAX]; //创建栈的数组空间
assert(elements!=NULL); //断言:动态存储分配成功与否
}

bool SeqStack::pop1(int & x) //栈顶元素出栈
{
if(IsEmpty1()==true) return false;//判栈空否,若栈空则函数返回
x=elements[top--]; //栈顶指针退1
return true; //退栈成功
}

void SeqStack::push1(const int & x) //新元素进栈
{
if(IsFull1()==true) overflowProcess(); //栈满则溢出处理
elements[++top]=x; //栈顶指针先加1,再进栈
}

bool SeqStack::IsEmpty1() const //判断栈空与否
{
return (top==-1)?true:false;
}

bool SeqStack::IsFull1()const //判断栈满与否
{
return (top==MAX-1)?true:false;
}

void SeqStack::overflowProcess() //栈的溢出处理
{
//私有函数,扩充栈的存储空间。
int *Array=new int[MAX+stackIncreament]; //和课本不一样 ??????????
if(Array==NULL)
{
printf("存贮分配失败 ! \n");
exit(1);
}
for(int i=0;i<=top;i++) Array[i]=elements[i];
MAX=MAX+stackIncreament;
delete []elements;
//elements=Array;
}

void SeqStack::output1() //元素入栈顺序输出
{
int n=0;
int t=top;
for(int i=0;i<top;i++)
{
printf(" %d",elements[i]);
n++;
if(n%10==0)
printf("\n");
}
}

void SeqStack::output(int x) //栈内元素输出
{
printf(" %d",x);
}

//----------------------顺序栈函数--------------------------//
void SeqStack1( SeqStack A)
{
int x=-1;
int X;
printf("请输入要入栈A的元素值,以0结束:\n");
while(x!=0){ //新元素进栈
scanf("%d",&x);
A.push1(x);
}
printf("\n元素进栈顺序是 :");
A.output1();
printf("\n\n");

A.pop1(X); //元素出栈
if(!A.pop1(X))
printf("元素出栈失败 !\n");
else
{
printf("\n栈顶元素是: ");
A.output(X);
printf("\n");
printf("\n元素出栈的结果是 : ");
A.output(X);
while(A.pop1(X))
A.output(X);
}
}

void main()
{
printf("----------顺序栈的调试----------\n");
printf("\n \n");
SeqStack A;
SeqStack1(A);
printf("\n \n");
}

⑶ C语言编程实现顺序栈的初始化,入栈,出栈,取栈顶元素,显示操作

#define STACKSIZE 100
int mstack[STACKSIZE],top,bottom;
void mInitStack() { top=bottom=0; }
void mPush(int x) { if ( top-bottom<=STACKSIZE ) { mstack[top]=x; top++; } }
int mPop() { int r=0; if ( top>bottom ) { r=mstack[top]; top--; } return r; }
void mShowStack() { int i; printf("["); for ( i=bottom;i<top;i++ ) printf("%d ",mstack[i]); printf("] "); }
void main()
{
int i,n,x,loop=1,s;
char buffer[80];
mInitStack();
scanf("%d",&n); for ( i=0;i<n;i++ ) { scanf("%d",&x); mPush(x); }
mShowStack();
while ( loop )
{ buffer[1]=0; gets(buffer); s=1;
switch ( buffer[1] )
{ case 'O':
case 'o': x=mPop(); break;
case 'U':
case 'u': x=atoi(buffer+5); mPush(x); break;
case 'n':
case 'N': loop=0; break;
default: s=0; break;
}
mShowStack();
}
mShowStack();

}

⑷ 建立顺序栈,并实现顺序栈的进栈和出栈

简单的办法就是用一个数组加一个下表就可以了。

publicclassStore
{
pulbic:
Store()
{
Index=0;
Elem=newint[13];
memset(Elem,0,13);
}

~Store()
{
delete[]Elem;
}

Push(intnum)
{
if(Index<0)
Index=0;

if(Index<12)
{
Elem[Index]=num;
Index++;
}
}

intPop()
{
if(Index>=0)
{
intresult=Elem[Index];
Index--;
returnresult;
}
}

intTop()
{
if(Index>=0&&Index<12)
returnElem[Index];
}

private:
intIndex;
int*Elem;
}

差不多这样了。没有测试,应该没什么错。

⑸ 1,2,3,4依次进栈,出栈随时,写一算法求出所有可能出栈序列

代码如下:

#define N 4

int m=0,a=0,b=N;/*m表示种数,a表示栈中元素个数,b表示外面还有需要进栈的个数*/

main()

{

inS(a,b);/*首先入栈*/

printf("%d",m);

getch();

}

int inS(int a,int b)/*入栈*/

{

a++;b--;/*入栈栈中元素+1,栈外元素-1 */

if(b>0)/*若栈外有元素,可以入栈*/

inS(a,b);

if(a>0)/*若栈中有元素,可以出栈*/

outS(a,b);

}

int outS(int a,int b)/*出栈*/

{

a--;/*出栈栈中元素-1*/

if(a==0&&b==0)/*若栈中元素和栈外元素都为0个*/

{

m++;/*则此种情况的序列满足条件,种数+1*/

return;

}

if(b>0)

inS(a,b);

if(a>0)

outS(a,b);

}

(5)编写算法实现顺序栈的出栈操作扩展阅读

栈的相关知识点:

顺序栈内容包含数据和栈顶指针(int),因此采用结构体。

注意:

1、初始时,top指向-1;

2、入栈时,先判断顺序栈是否已满(判断条件:top=maxsize-1);如果没满,则top++,并将元素值赋给s.top;

3、出栈时,先判断顺序栈是否已空(判断条件:top=-1);如果没空,则先返回栈顶元素,再top- -。

共享栈

两个顺序栈共享一个一维数组空间,而栈底位置相对不变,两个栈底分别设为一维数组的两端。

note:

(1)栈空:top1==-1&&top2==maxsize;

(2)栈满:top2-top1= 1(此时两个栈顶指针相邻);

(3)入栈:S.data[++top1]=x或S.data[–top2]=x;

(4)出栈:x=S.data[top1–]或x=S.data[top2++];

(5)共享栈可以更好的利用存储空间,整个存储空间被占满时发生上溢。

⑹ 试编写一个算法,让两个顺序栈共用一个数组stack[N],分别实现入栈\出栈操作

要2个栈公用一个存储空间看来栈顶指针只能从两端开始了(和队列有点像)
设2个栈为s0,s1 ,s1初始的栈顶指针为-1,s2的初始栈顶指针为N
typedef struct
{
elemtype stack[N]; //栈存储空间
int top[2]; //top为两个栈顶指针
}St;
St s;//s为全局变量用于操作
void push(int i,elemtype e)//入栈操作,i代表栈的编号,e为入栈元素
{
if(i!=0||i!=1)exit(0);//栈号不对
if(s.top[1]-s.top[0]==1)//栈满
{
printf("FULL!");
return;
}
if(i==0)s.tack[++s.top[0]]=e;//s0入栈
if(i==1)s.tack[--s.top[1]]=e;//s1入栈
}
void pop(int i,elemtype &e)//出栈操作,i代表栈的编号,e为出栈元素
{
if(i!=0||i!=1)exit(0);//栈号不对
if(i==0)
{
if(s.top[0]==-1)//栈s0空
{
printf("EMPTY!");
return;
}
else e=s.stack[s.top[0]--];//s0出栈
}
if(i==1)
{
if(s.top[1]==N)//栈s1空
{
printf("EMPTY!");
return;
}
else e=s.stack[s.top[1]++];//s1出栈
}
}

⑺ 数据结构中的顺序栈的进栈和出栈问题

#include <stdio.h>

#define StackSize 100
typedef char DataType;
typedef struct
{ DataType data[StackSize];
int top;
}SeqStack;

//下面是算法
void InitStack(SeqStack *S)
{
S->top=-1;
}

int StackEmpty(SeqStack *S)
{
return S->top==-1;
}
int StackFull(SeqStack *S)
{
return S->top==StackSize-1;
}
void Push(SeqStack *S,DataType x)
{ if (StackFull(S))
{ printf("Stack overflow");
exit(0); }
S->data[++S->top]=x; }

DataType Pop(SeqStack *S)
{ if (StackEmpty(S))
{ printf("Stack underflow");
exit(0);
}
return S->data[S->top--];
}

int main(void)
{
SeqStack ss;
int i;
InitStack(&ss);
for(i=0;i<26 ;i++ )
{
Push(&ss,'A'+i);
}
while(i--)
printf("%c\t",Pop(&ss));
return 0;
}

阅读全文

与编写算法实现顺序栈的出栈操作相关的资料

热点内容
服务器mgmt旁边的接口是什么 浏览:844
单片机发光二极管原理图 浏览:50
在北京当程序员6年 浏览:128
编译器gcc如何用 浏览:412
androidbringup 浏览:977
算法设计与分析英文版 浏览:910
java程序员加班吗 浏览:142
编译检查的是什么错误 浏览:405
加密兔f码生成器免费 浏览:292
思科路由器命令明文加密 浏览:171
方舟生存进化服务器如何改名字 浏览:892
央行数字货币app怎么注册 浏览:431
51单片机显示时间 浏览:770
我的世界网易版怎么压缩地图 浏览:682
qq小程序云服务器和 浏览:740
方舟服务器怎么玩才好玩 浏览:561
单片机的部件 浏览:623
编译原理遍的过程 浏览:274
python读取json字符串 浏览:72
ubuntu1404安装php 浏览:636