Home > Back-end >  In the stack and remove has always been a value, change for a long time or don't know how to do
In the stack and remove has always been a value, change for a long time or don't know how to do

Time:11-08

#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;
}

Bool Locate (LinkstNode * & amp; S, int e, int I)
{
int j=0;
LinkstNode * p=s * r;
If (i<=0)
return false;
While (j{
j++;
P=p - & gt; Next;
}
If (p==NULL)
return false;
The else
{
R=(LinkstNode *) malloc (sizeof (LinkstNode));
R - & gt; data=https://bbs.csdn.net/topics/e;
R - & gt; Next=p - & gt; Next;
P - & gt; Next=s;
return true;
}
}

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<" 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;
}
Bool 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<" Remove s the elements in the stack, "& lt; Push (m, temp1);
While (s - & gt; Next!=NULL)
{
Pop (s, temp1);
cout<" Remove s the elements in the stack, "& lt; If (temp1 & gt; Gettop (m))
{
cout<" Because "& lt; " Locate (m, temp1, 1);
cout<" Remove m the first element of the: "& lt; }
If (temp1 & lt; Gettop (m))
{
Pop (m, temp2);
cout<" Because "& lt; Locate (s, temp2, 1);
cout<" Will "& lt; Locate (m, temp1, 1);
cout<" Will "& lt; Locate (m, temp2, 1);
cout<" Will "& 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);
}













  • Related