单链表的基本操作
typedef struct node{
int data;
struct node*pnext;
}*PNODE,NODE;
PNODE create_list();
void show_list(PNODE phead);
int length_list(PNODE phead);
bool is_empty(PNODE phead);
void sort_list(PNODE phead);
int main()
{
PNODE phead=NULL;
phead=create_list();
show_list(phead);
printf("\n");
sort_list(phead);
show_list(phead);
}
PNODE create_list()
{
PNODE phead=(PNODE)malloc(sizeof(NODE));
PNODE ptail=phead;
ptail->pnext=NULL;
printf("请输入链表长度:");
int len; int val;
scanf("%d",&len);
for(int i=0;i<len;i++)
{
PNODE p=(PNODE)malloc(sizeof(NODE));
printf("请输入值");
scanf("%d",&val);
p->data=val;
ptail->pnext=p;
p->pnext=NULL;
ptail=p;
}
return phead;
}
void show_list(PNODE phead)
{
PNODE p=phead->pnext;
while(p!=NULL)
{
printf("%d",p->data);
p=p->pnext ;
}
}
int length_list(PNODE phead)
{
PNODE p=phead->pnext;
int cnt=0;
while(p!=NULL)
{
cnt++;
p=p->pnext;
}
return cnt;
}
bool is_empty(PNODE phead)
{
if(phead->pnext==NULL)
{
return true;
}
else return false;
}
void sort_list(PNODE phead)
{
PNODE p,q;int i,j,t;
int len=length_list(phead);
for(i=0,p=phead->pnext;i<len-1;i++,p=p->pnext)
{
for(j=i+1,q=p->pnext;j<len;j++,q=q->pnext)
{
if(p->data>q->data)