#include"hlinklist.h"
void sort(linklist head)
{
linklist t,t2,min,z;
t=head->next;
printf("OK");
min=t;
while(t!=NULL)
{
while(t->next!=NULL)
{
if((min->info)>(t->next->info))
{
t2=t;
min=t;
}
t=t->next;
}
t2->next=t2->next->next;
min->next=head->next;
head->next=min;
}
}
int main()
{linklist head;
head=creatbyqueue();
print(head);
sort(head);
print(head);
delist(head);
return 0;
}
估计是sort函数出了问题,还请大神指点一二。。
估计是t2->next=t2->next->next这出了问题但不知如何解决。。
以下为头文件代码以备不时之需:
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct link_node{
datatype info;
struct link_node *next;
}node;
typedef node *linklist;
linklist creatbystack(){
linklist head,s;
datatype x;
s=(linklist)malloc(sizeof(node));
head=s;
head->next=NULL;
printf("请输入若干整数序列:\n");
scanf("%d",&x);
while(x!=0)
{
s=(linklist)malloc(sizeof(node));
s->info=x;
s->next=head->next;
head->next=s;
scanf("%d",&x);
}
return head;
}
linklist creatbyqueue()
{
linklist head,s,r;
datatype x;
s=(linklist)malloc(sizeof(node));
head=r=s;
printf("请输入若干整数序列:\n");
scanf("%d",&x);
while(x!=0)
{
s=(linklist)malloc(sizeof(node));
s->info=x;
r->next=s;
r=s;
scanf("%d",&x);
}
r->next=NULL;
return head;
}
void print(linklist head)
{
linklist p;
int i=0;
p=head->next;
printf("List is:\n");
while(p)
{printf("%5d",p->info);
p=p->next;
i++;
if(i%10==0) printf("\n");
}
printf("\n");
}
void delist(linklist head)
{
linklist p=head;
while(p)
{
head=p->next;
free(p);
p=head;
}
}
http://blog.csdn.net/guoyong10721073/article/details/9009103
http://www.cnblogs.com/liangchao/archive/2012/09/24/2700687.html
只是看到了代码,是什么问题了?
void sort(linklist *head),这个函数的实参是一个指针,故形参改为linklist *head,你再去试试
胡乱写了一下
//排序
void sort(linklist *head)
{
linklist pNode,pTemp,pLast,pBuf,pNext;
pNode=(*head)->next->next;
pLast = (*head)->next;
pNext = (*head)->next;
pBuf = (*head);
bool bMark = true;
printf("OK");
while(pNode!=NULL)
{
pTemp = (*head)->next;
pBuf = (*head);
bMark = true;
while(pTemp != NULL)
{
//取出
if (bMark)
{
pLast = (*head);
while(pLast->next != NULL)//查找取出节点
{
if (pLast->next == pNode)
{
break;
}
pLast = pLast->next;
}
pLast->next = pNode->next;
pNode->next = NULL;
pNext = pLast->next;
bMark = false;
}
if((pTemp->info)>(pNode->info))
{
pBuf->next = pNode;
pNode->next = pTemp;
break;
}
pBuf = pTemp;
pTemp=pTemp->next;
if (NULL == pTemp)
{
pBuf->next = pNode;
break;
}
}
pNode = pNext;//跳到下一个
}
}