導航:首頁 > 源碼編譯 > 編寫演算法實現順序棧的出棧操作

編寫演算法實現順序棧的出棧操作

發布時間: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;
}

閱讀全文

與編寫演算法實現順序棧的出棧操作相關的資料

熱點內容
c編譯器使用說明 瀏覽:703
鄭州前端程序員私活有風險嗎 瀏覽:10
小型螺桿機壓縮機 瀏覽:516
成人解壓最好的方法 瀏覽:48
最小製冷壓縮機 瀏覽:488
xampp支持python 瀏覽:367
深圳周立功單片機 瀏覽:56
圓上點與點之間角度演算法 瀏覽:863
怎麼知道微信關聯了哪些app 瀏覽:700
android事件驅動 瀏覽:886
簽約大屏系統源碼 瀏覽:806
安卓系統怎麼轉入平板 瀏覽:425
安卓手機相機怎麼提取文字 瀏覽:219
如何查看伺服器映射的外網地址 瀏覽:985
圖片刺綉演算法 瀏覽:675
阿里雲伺服器沒有實例 瀏覽:605
綿陽有沒有什麼app 瀏覽:848
怎麼用游俠映射伺服器 瀏覽:921
為什麼無意下載的app無法刪除 瀏覽:308
word2007打開pdf 瀏覽:118