简体   繁体   中英

Sorting the numbers in a list / C

I need to sort the numbers that are entered in a list, but I am doing something wrong and it's sorting all of them except the first one.Any ideas how to fix this?

Here's my code:

    #include <stdio.h>
    #include <stdlib.h>

    struct node
    {
        int data;
        struct node *next;
    };
    struct node* List;

    void Add (struct node* p, int d)
   {
    struct node* q;
    q=malloc(sizeof(struct node));
    if (q==NULL)
        printf("Not enaugh memory!");
    else
    {
        q->data=d;
        if(List==NULL)
        {
            q->next=NULL;
            List=q;
        }
        else
        {
            struct node *ptr=List;
            while((ptr->next!=NULL)&&(ptr->next->data>d))
            {
                ptr=ptr->next;
            }
            q->next=ptr->next;
            ptr->next=q;

        }
    }
}


    int main()
    {
        int n,i,a;
        printf("How much numbers are you going to enter? ");
        scanf("%d",&n);
        for (i=1; i<=n; i++)
        {
            printf("\nEnter a number: ");
            scanf("%d",&a);
            Add(List,a);
        }
        printf("\nThe sorted numbers are: ");
        struct node *ptr=List;
        while(ptr!=NULL)
        {
            printf("%d\t",ptr->data);
            ptr=ptr->next;
        }
        printf("\n\n");
        system("PAUSE");
        return 0;
    }

Thanks for the help in advance :-)

in add() function,

if(List==p)

this statement is true for all elements you insert to list since the call to add is,

Add(List,a);

so p=List . therefore the sorting code written in else part is not executed.
Also add statements to check for empty initial list.
You can use code similar to this,

void Add (int d)
{
    struct node* q;
    q=malloc(sizeof(struct node));
    if (q==NULL)
        printf("Not enaugh memory!");
    else
    {
        q->data=d;
        if(List==NULL)
        {
            q->next=NULL;
            List=q;
        }
        else
        {
            struct node *ptr=List;
            while((ptr->next!=NULL)&&(ptr->next->data>d))
            {
                ptr=ptr->next;
            }
            q->next=ptr->next;
            ptr->next=q;

        }
    }
}

Since list is a global variable you dont need to pass it to Add() function. change the function call to

 Add(a);

You always call Add with List as the first parameter, so it's alway true inside Add that (List==p) . Consequently each new item is just inserted at the front of the list and there is no sorting at all.

EDIT 1

A good practice would be sending a list to the Add routine as a parameter. Or, if you want to keep it external, just don't give it to Add at all and test if(List == NULL)

void Add( int d)
{
    // ... alloc 'q' and fill q->d here, then:
    if(List == NULL)
    {
        q->next = NULL;
        List = q;
    }
    else
    {
        struct node *b;  // put q after b
        for( b = List; b->next != NULL; b = b->next)
            if( b->next->data >= d)
                break;
        q->next = b->next;
        b->next = q;
    }
}

EDIT 2

Example of transferring the list to a function by parameter

void Add( struct node **n, int d)
{
    // ... alloc 'q' and fill q->d here, then:

    while( *n != NULL && (*n)->data < d)
        n = & (*n)->next;

    q->next = *n;
    *n = q;
}

int main()
{
    // ...
    Add( &List, a);
    // ...
}
void Add (struct node* p, int d){
    struct node* q;
    q=malloc(sizeof(struct node));
    if (q==NULL)
        printf("Not enaugh memory!");
    else{
        q->data=d;
        if(List==NULL || List->data < d){//modify this line
            q->next= List;//and change NULL to List
            List=q;
        } else {
            struct node *ptr=List;
            while((ptr->next!=NULL)&&(ptr->next->data>d)){
                ptr=ptr->next;
            }
            q->next=ptr->next;
            ptr->next=q;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM