Home > Back-end >  The basic operations of a stack sequence where there is wrong? To solve the
The basic operations of a stack sequence where there is wrong? To solve the

Time:10-04



# include & lt; Iostream. H>
Typedef struct {
Int * base;
Int * top;
Int stacksize;
}SqStack;
Void InitStack (SqStack & amp; S);
Void push (SqStack & amp; S, int e);
Void pop (SqStack & amp; S, int & amp; E);
Void print (SqStack & amp; S);
Void StackLength (SqStack & amp; S);
Void InitStack (SqStack & amp; S)
{
S.t op=S.b ase=0;
}
Void push (SqStack & amp; S, int e)
{
If (S.t op - S.b ase>=S.s tacksize)
cout<" Stack full "& lt; * S.t op=e;
S.t op++;
}
Void pop (SqStack & amp; S, int & amp; E)
{
If (S.t op==S.b ase)
cout<" The stack is empty "& lt; S.t op -;
E=* S.t op;
}
Void print (SqStack & amp; S)
{int * p=S.b ase;
While (p!=S.t op)
{cout<" * (P) "& lt; <" ";
P++;
}
cout}
Int StackLength SqStack (S)
{
If (S.t op==S.b ase)
Return 0;
The else
Return (S.t op - S.b ase);
/* int * p;
P=S.b ase.
int i=0;
While (p!=S.t op) {p++; I++; }
return i; */
}
Void main ()
{
SqStack S;
//initialize
cout<" Initialize the stack \ n ";
InitStack (S);
cout<" The bottom of the stack and the stack: "& lt; cout<" Five elements into the stack: ";
for(int i=0; i<=5; I++)
{int x;
cout<" Please to enter into the stack elements: "& lt; Cin> x;
Push (S, x);
}
Print (S);
int y;
Pop (S, y);
cout<" The stack elements is: "& lt; Print (S);

}

CodePudding user response:

You have to play a pointer, don't allocate space for someone? How could that be wrong...

CodePudding user response:

The allocated space first, otherwise is AV errors at any time, in addition, S.s tacksize initialization
  • Related