Describe
Enter an integer of 10 0~100 into the array data, and use the insertion sort method to sort the input in ascending order and output
Input
10 Integers of 0~100
Output
10 number of inputs from small to large output
Sample input
7
3
6
12
2
98
15
60
20
16
Sample output
2 3 6 7 12 15 16 20 60 98
Idea: Use the chain list, edge comparison side insertion, the final output.
Code:
#include
using namespace Std;
struct node
{
int data;
Node *next;
};
Class Linklist
{
Private
Node *first;
Public
Linklist ();
~linklist ();
void Paixu (int x);
void Printlist ();
};
Linklist::linklist ()
{
First = new node;
First->next = NULL;
}
Linklist::~linklist ()
{
Node *q=null;
while (First!=null)
{
Q = First;
First = first->next;
Delete q;
}
}
void linklist::P aixu (int x)
{
Node *p,*q,*r;
R=new node;
R->data= x;
p = First;
Q = first->next;
if (q!=null)
{
while (Q!=null&&q->data<</span>r->data)
{
q=q->next;
p=p->next;
}//Edge Traverse Edge to compare new data
if (q!=null)
{
r->next=q;
p->next=r;
}//finding new data locations and not inserting new data at the end of the list
else if (q==null)
{
p->next=r;
r->next=null;
}//find new data location insert new data at the end of the list
}
else if (q==null)
{
p->next=r;
r->next=null;
}
}
void linklist::P rintlist ()
{
Node *p;
p=first->next;
while (P!=null)
{
cout<<p->data<<endl;
p=p->next;
}//output all sorted data
}
int main ()
{
Linklist l;
int i,x;
for (i=0;i<</span>10;i++)
{
cin>>x;
L.paixu (x);
}
L.printlist ();
return 0;
}
D1165: Inserting sort