# 编程：有序的链表

#include<iostream>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
Node(int n)
{
data=n;
next=NULL;
}
void setNext(Node *n);
int getData();
Node *getNext();
};
class List
{
private:
public:
List()
{
}
void insertlist(Node *s);
void outputlist();
~List();
};
//**********************begin***************************

//*****************end********************
int main()
{
List A;
int n;
cin>>n;
while(n>0)
{
A.insertlist(new Node(n));
cin>>n;
}
A.outputlist();
return 0;
}

``6 2 4 1 8 9 11 67 12 -1``

``1 2 4 6 8 9 11 12 67``

``````#include<stdio.h>
int main()
{
int i=0,n=0;
int a[50],t,j;
while(scanf("%d",&a[i])!=EOF)
{
if(a[i]<0)break;
i++;
}
n=i;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
if(i==n-1)break;
printf(" ");
}

return 0;
}``````

``````
#include<iostream>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
Node(int n)
{
data=n;
next=NULL;
}
void setNext(Node *n);
int getData();
Node *getNext();
};
class List
{
private:
public:
List()
{
}
void insertlist(Node *s);
void outputlist();
~List();
};void Node::setNext(Node *n)
{
next=n;
}
int Node::getData()
{
return data;
}
Node *Node::getNext()
{
return next;
}

void List::insertlist(Node *s)  //插入一个值为a结点
{
Node *p, *q;
else if(p->getData() > s->getData())  //若s是第一个结点
{
s->setNext(p);
}
else
{
while(p->getData() < s->getData() && p->getNext()!=NULL)   //查找结点a
{
q=p;
p=p->getNext();
}
if(p->getData() >= s->getData())     //若有比结点a大的点，插入到q后
{
q->setNext(s);
s->setNext(p);
}
else                    //结点a作为最后的点；
{
p->setNext(s);
}
}
}

void List::outputlist()
{
while(p!=NULL)
{
cout<<p->getData()<<" ";
p=p->getNext();
}
cout<<endl;
return;
}

List::~List()
{
while(p!=NULL)
{
q=p;
p=p->getNext();
delete q;
}
}
//*****************end********************
int main()
{
List A;
int n;
cin>>n;
while(n>0)
{
A.insertlist(new Node(n));
cin>>n;
}
A.outputlist();
return 0;
}``````