==========================
功能:输入一点(X,Y),按照X值
(由小到大)排序
返回:指向链表表头的指针
==========================
#include "stdio.h"
#include "malloc.h"
typedef struct Node
{
int Xdata;
int Ydata;
struct Node *pNext;
}NODE, *PNODE;
//创建链表
PNODE CreatList();
//链表排序
PNODE sort(PNODE pHead);
int main()
{
PNODE pHead = NULL;
PNODE ptemp = NULL;
//开始创建链表
pHead = CreatList();
//按照Xdata大小进行排序
sort(pHead);
//链表输出显示
PNODE p = pHead->pNext;
while(NULL != p)
{
printf("%d,%d\n",p->Xdata,p->Ydata);
p = p->pNext;
}
//链表内存释放
p = pHead;
while (p != NULL)
{
ptemp = p;
p = p->pNext;
delete(ptemp);
}
pHead = NULL;
return 1;
}
PNODE CreatList()
{
int data1;
int data2;
//分配不存放有效数据的头结点
PNODE pHead = (PNODE)malloc(sizeof(NODE));
PNODE pTail = pHead;
pTail->pNext = NULL;
while (scanf("%d,%d\n",&data1,&data2) != EOF)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->Xdata = data1;
pNew->Ydata = data2;
pTail->pNext = pNew;
pNew->pNext=NULL;
pTail = pNew;
}
return pHead;
}
PNODE sort(PNODE pHead)
{
int Xtemp,Ytemp;
PNODE p1 = NULL,p2 = NULL;
p1 = pHead->pNext;
while(p1)
{
p2 = p1->pNext;
while(p2)
{
if (p1->Xdata > p2->Xdata)
{
Xtemp = p1->Xdata;
Ytemp = p1->Ydata;
p1->Xdata = p2->Xdata;
p1->Ydata = p2->Ydata;
p2->Xdata = Xtemp;
p2->Ydata = Ytemp;
}
p2 = p2->pNext;
}
p1 = p1->pNext;
}
return pHead;
}