#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
class mylist : public Node
{
public:
Node* head;
Node* rear;
public:
mylist()
{
head=0;
rear=0;
}
void add(){}
bool isEmpty()
{
if(head==0)
return true;
else
return false;
}
void show()
{
Node* current=0;
cout<<"List: ";
for(current=head;current!=0;current=current->next)
{
cout<<current->data<<" ";
}
cout<<endl;
}
};
class InsertSort : public mylist
{
private:
public:
void add(int n)
{
Node* current = new Node;
current->data = n;
current->next = 0;
if(isEmpty())
{
head = current;
rear = current;
}
else
{
Node* tptr1 = 0;
Node* tptr2 = 0;
for(tptr2=head;tptr2!=0;tptr2=tptr2->next)
{
if(n<(tptr2->data))break;
tptr1 = tptr2;
}
if(n<head->data)
{
current->next = head;
head = current;
}
else
{
if(tptr2==0)/*is rear*/
{
rear = current;
}
current->next = tptr2;
tptr1->next = current;
}
}
}
};
int main()
{
InsertSort InSort;
int n;
cout<<"*key 1 is add, key -1 is end.*"<<endl;
while(cin>>n)
{
if(n==1)
{
int m;
cin>>m;
InSort.add(m);
}
else if(n==-1)
{
InSort.show();
break;
}
else
cout<<"*Wrong input.*"<<endl;
}
return 0;
}
留言列表