Home > Back-end >  Take the last element of stack can't take it out
Take the last element of stack can't take it out

Time:11-09

# include
# include
using namespace std;

Struct LinkstNode
{
The int data;
LinkstNode * next;
};

Void InitStack (LinkstNode * & amp; S)
{
S=(LinkstNode *) malloc (sizeof (LinkstNode));
S - & gt; Next=NULL;
}

Void Push (LinkstNode * & amp; S, int a [], int n)
{
LinkstNode * r;
S=(LinkstNode *) malloc (sizeof (LinkstNode));
S - & gt; Next=NULL;
for(int i=0; i{
R=(LinkstNode *) malloc (sizeof (LinkstNode));
R - & gt; data=https://bbs.csdn.net/topics/a [I];
R - & gt; Next=s - & gt; Next;
S - & gt; Next=r;
}
}
Void Push (LinkstNode * & amp; S, int e)
{
LinkstNode * r;
S=(LinkstNode *) malloc (sizeof (LinkstNode));
S - & gt; Next=NULL;
R=(LinkstNode *) malloc (sizeof (LinkstNode));
R - & gt; data=https://bbs.csdn.net/topics/e;
R - & gt; Next=s - & gt; Next;
S - & gt; Next=r;
}

Void the Locate (LinkstNode * & amp; S, int e)
{
LinkstNode * l;
S=(LinkstNode *) malloc (sizeof (LinkstNode));
S - & gt; Next=NULL;
L=(LinkstNode *) malloc (sizeof (LinkstNode));
L - & gt; data=https://bbs.csdn.net/topics/e;
L - & gt; Next=s - & gt; Next;
S - & gt; Next=l;
}


Void DispStack (LinkstNode * s, int n)
{
int a[n];
for(int i=0; i{
A [I]=s - & gt; Next - & gt; The data;
S=s - & gt; Next;
}
Cout<& lt;" A stack of order "& lt; for(int i=0; i{
Cout}
Cout}

Bool pop (LinkstNode * s, int & amp; E)
{
LinkstNode * p;
If (s - & gt; Next==NULL)
return false;
P=s - & gt; Next;
E=p - & gt; The data;
S - & gt; Next=p - & gt; Next;
free(p);
return true;
}
Int gettop (LinkstNode * s)
{
LinkstNode * p;
Int e;
If (s - & gt; Next==NULL)
return false;
E=s - & gt; Next - & gt; The data;
Return e;
}


Void paixu (LinkstNode * s)
{
LinkstNode * m;
InitStack (m);
Int temp1;
Int temp2;
Pop (s, temp1);
Cout<& lt;" Remove s the elements in the stack, "& lt; Push (m, temp1);
While (s - & gt; Next!=NULL)
{
Pop (s, temp1);
Cout<& lt;" Remove s the elements in the stack, "& lt; If (temp1 & gt; Gettop (m))
{
Cout<& lt;" Because "& lt; Locate (m, temp1);
Cout<& lt;" Remove m the first element of the: "& lt; }
If (temp1 & lt; Gettop (m))
{
Pop (m, temp2);
Cout<& lt;" Because "& lt; To Locate (s, temp2);
Cout<& lt;" Will "& lt; Locate (m, temp1);
Cout<& lt;" Then "& lt; }
}
}

The main ()
{
,4,5,1 int a [4]={2};
Int n=4;
LinkstNode * s;
InitStack (s);
Push (s, a, n);
DispStack (s, n);
Paixu (s);
}














CodePudding user response:

Locate function does not put the array into the stack.
  • Related