#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
typedef struct LIST_NODE
{
int da
struct LIST_NODE* next;
}list_node,*plist_node;
/************************************************************************/
/* 随机生成node的数据。按序插入 */
/************************************************************************/
plist_node creat_list_sort()
{
plist_node head = (plist_node)malloc(sizeof(list_node));
head->next = NULL;
plist_node ntemp;
int cycle = 1,ndata = 0;
plist_node curp = head;
plist_node prep;
while (cycle<20)
{
// printf("please input the da
// scanf("%d",&ndata);
// if (ndata==-1)
// {
// break;
// }
ndata = rand()%100;
ntemp = (plist_node)malloc(sizeof(list_node));
ntemp->da
if (head->next==NULL)
{
head->next = ntemp;
curp = ntemp;
curp->next = NULL;
}
else
{
prep = head;
curp = head->next;
while (curp&&curp->da
{
prep=curp;
curp=curp->next;
}
prep->next = ntemp;
ntemp->next = curp;
}
cycle++;
}
return head;
}
/************************************************************************/
/* 随机生成node的数据。不按序插入 */
/************************************************************************/
plist_node create_list()
{
plist_node head = (plist_node)malloc(sizeof(list_node));
head->next = NULL;
int cycle = 0,ndata = 0;
plist_node curp = head;
while (cycle<10)
{
//printf("please input the da
//scanf("%d",&ndata);
ndata = rand()%100;
if (ndata == -1)
{
break;
}
else
{
plist_node ntemp = (plist_node)malloc(sizeof(list_node));
ntemp->da
curp->next = ntemp;
curp = ntemp;
}
cycle++;
}
curp->next = NULL;
return head;
}
/************************************************************************/
/* 向链路顺序插入数据节点 */
/************************************************************************/
plist_node list_insert(plist_node head,plist_node node)
{
plist_node prev = head;
plist_node curp = head->next;
if (curp==NULL)
{
head->next = node;
node->next = NULL;
return head;
}
while (curp)
{
if (curp->da
{
prev=curp;
curp = curp->next;
}
else
break;
}
if (curp == NULL)
{
prev->next = node;
node->next = NULL;
}
else
{
prev->next = node;
node->next = curp;
}
return head;
}
/************************************************************************/
/* 链排序 */
/************************************************************************/
plist_node list_sort(plist_node head)
{
plist_node prear = head->next;
plist_node curp,nextp;
if (!prear->next)
{
printf("the member of the list must be more than 1");
return head;
}
curp = prear->next;
prear->next = NULL;
while (curp)
{
nextp = curp->next;
list_insert(head,curp);
curp= nextp;
}
//list_insert(head,curp);
return head;
}
/************************************************************************/
/* 链反转 */
/************************************************************************/
plist_node list_reverse(plist_node head)
{
plist_node prev = head->next;
plist_node curp = prev->next;
prev->next = NULL;
plist_node pnext = curp->next;
while (pnext)
{
plist_node nextl = pnext->next;
pnext->next = curp;
curp->next = prev;
prev = curp;
curp = pnext;
pnext = nextl;
}
head->next = curp;
return head;
}
void list_printf(plist_node head)
{
plist_node curp = head->next;
while (curp)
{
printf("%d ",curp->da
curp = curp->next;
}
printf("\n");
}
void main()
{
plist_node head;
head = create_list();
list_printf(head);
head = list_sort(head);
list_printf(head);
head = list_reverse(head);
list_printf(head);
head = creat_list_sort();
list_printf(head);
}