//此题适用计数排序
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int num;
struct node * next;
}Node, *List;
List ListInit(List Head, int n)
{
List p;
int i;
for(i = 0; i < n; i ++)
{
p = (List)malloc(sizeof(Node));
p->next = Head;
Head = p;
}
return Head;
}
List ListReleas(List Head)
{
List p = Head;
while(Head)
{
p = Head;
Head=p->next;
free(p);
}
return Head;
}
int main()
{
List Head = NULL, p = NULL;
int count[3] = , n, inum;
printf("输入节点数:");
scanf("%d", &n);
Head = ListInit(Head, n);
printf("输入每个节点值(共%d个,只接受-1,0,1):\n", n);
p = Head;
while( p != NULL )
{
scanf("%d", &p->num);
if(p->num < 2 && p->num > -2)
p = p->next;
}
//以下为排序(计数排序)
p = Head;
while( p != NULL )
{
count[p->num + 1] ++;
p = p->next;
}
p = Head;
inum = -1;
while( p != NULL )
{
if(count[inum + 1])
{
p->num = inum;
p = p->next;
count[inum + 1]--;
}
else
inum ++;
}
//以下为输出
p = Head;
while( p != NULL )
{
printf("%d ", p->num);
p = p->next;
}
printf("\n");
ListReleas(Head);
return 0;
}
追问int count[3] = , n, inum;
printf("输入每个节点值(共%d个,只接受-1,0,1):\n", n);
是什么意思啊?不大懂 问什么只接受-1,0,1呢?