Home > Back-end >  Data structure will be ordered by size of data in the stack, but in the end, a data cannot be sorted
Data structure will be ordered by size of data in the stack, but in the end, a data cannot be sorted

Time:11-15

Turn to
Sophomore learning take the stack data structure, the code is a stack of data according to the smallest, and then stack the last data can't sort of
#include
#include
using namespace std;
Struct LinkstNode
{
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<" 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<" 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);
cout<" Remove m the first element of the: "& lt; }
If (temp1 & lt; Gettop (m))
{
Pop (m, temp2);
cout<" Because "& lt; To Locate (s, temp2);
cout<" Will "& lt; Locate (m, temp1);
cout<" 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);
}

  • Related