将链表中的元素按照负数、0、正数的方式排列
代码
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
node *next;
}Node;
void Creat(Node *L)
{
node *s,*tc=L;
printf("请输入元素个数n:");
int n,i;
scanf("%d", &n);
while(n--){
s=(Node*)malloc(sizeof(Node));
scanf("%d",&s->data );
tc->next =s;
tc=s;
}
tc->next =NULL;
}
int DisList(Node *L)
{
if(L->next == NULL) return 0;
node *p=L->next ;
while(p != NULL){
printf("%d ", p->data );
p=p->next ;
}
printf("\n");
return 1;
}
int traverse(Node *L)
{
Node *p =L->next;
int cnt=0;
while(p != NULL){
p=p->next ;
cnt++;
}
return cnt;
}
int sort(Node *L)
{
Node *pre=L->next ,*p=pre->next ;
while(pre != NULL){
p=pre->next ;
while(p != NULL){
if(pre->data >=0 && p->data <0){
int temp=pre->data ;
pre->data =p->data ;
p->data=temp;
}else if(pre->data >0 && p->data == 0){
int temp=pre->data ;
pre->data =p->data ;
p->data=temp;
}else{
p=p->next ;
}
}
pre=pre->next ;
}
}
int main()
{
Node *L=(Node *)malloc(sizeof(Node));
Creat(L);
sort(L);
DisList(L);
return 0;
}