typedef struct node
{
int data;
struct node * pnext;
}node, *pnode;
pnode create_list(void);
void traverse_list(pnode phead);
bool is_empty(pnode phead);
int lenth_list(pnode);
//bool insert_list(pnode , int ,int);
//bool delete_list(pnode, int ,int*);
void sort_list(pnode );
int main (void)
{
pnode phead = NULL;
phead = create_list();
traverse_list(phead);
int len = lenth_list(phead);
printf("链表长度:%d",len);
sort_list(phead);
traverse_list(phead);
return 0;
}
pnode create_list(void)
{
int len;
int i;
int val;
pnode phead = (pnode) malloc(sizeof(node));
if(NULL == phead)
{
printf("false\n");
exit(-1);
}
pnode ptail = phead;
ptail -> pnext = NULL;
printf("please input the number of list:");
scanf("%d",&len);
for( i = 0; i"please input the val of the %d:",i+1);
scanf("%d",&val);
pnode pnew = (pnode) malloc(sizeof(node));
if(NULL == pnew)
{
printf("false\n");
exit(-1);
}
pnew->data = val;
ptail->pnext = pnew;
pnew->pnext = NULL;
ptail = pnew;
}
return phead;
}
void traverse_list(pnode phead)
{
pnode p = phead->pnext;
while(NULL != p)
{
printf("%d\n",p->data);
p = p->pnext;
}
printf("\n");
return;
}
int lenth_list(pnode phead)
{
pnode p = phead->pnext;
int len = 0;
while (NULL != p)
{
++len;
p->pnext;
}
return len;
}
void sort_list(pnode phead)
{
int i, j, k;
int len = lenth_list(phead);
pnode p,q;
for (i=0,p=phead->pnext; i < len-1; ++i , p = p->pnext)
{
for(j=i+1,q=p->pnext; j pnext)
{
if (p->data > q->data)
{
k = p->data;
p->data = q->data;
q->data = k;
}
}
}
return ;
}