#include <iostream.h>
typedef struct {
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S);
void push(SqStack &S, int e);
void pop(SqStack &S,int &e);
void print(SqStack &S);
void StackLength(SqStack &S);
void InitStack(SqStack &S)
{
S.top=S.base=0;
}
void push(SqStack &S, int e)
{
if(S.top-S.base>=S.stacksize)
cout<<"堆疊滿"<<endl;
*S.top=e;
S.top++;
}
void pop(SqStack &S,int &e)
{
if(S.top==S.base)
cout<<"堆疊空"<<endl;
S.top--;
e=*S.top;
}
void print(SqStack &S)
{ int *p=S.base;
while(p!=S.top)
{ cout<<"*(P)"<<" ";
p++;
}
cout<<endl;
}
int StackLength(SqStack S)
{
if(S.top==S.base)
return 0;
else
return(S.top-S.base);
/*int *p;
p=S.base;
int i=0;
while(p!=S.top) {p++;i++;}
return i;*/
}
void main()
{
SqStack S;
//初始化
cout<<"初始化堆疊\n";
InitStack(S);
cout<<"堆疊底和堆疊頂:"<<S.base<<","<<S.top<<endl;
cout<<"5個元素入堆疊:";
for(int i=0;i<=5;i++)
{ int x;
cout<<"請輸入要入堆疊的元素:"<<endl;
cin>>x;
push(S, x);
}
print(S);
int y;
pop(S, y);
cout<<"出堆疊元素為:"<<y<<endl;
print(S);
}
uj5u.com熱心網友回復:
你要玩指標,又不給人家分配空間?怎么可能不出錯……uj5u.com熱心網友回復:
先給指標分配空間,不然隨時都是AV錯誤,另外,S.stacksize要賦初值轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/110030.html
標籤:基礎類
