简体   繁体   中英

What should I do so that the last node in the linked list participates in bubble sort?

I have written a linked list program and I ought to program a bubble sorting function, but the last node seems to not participate in the bubble sort. I guess that's because the pointer moves to NULL and rejects the last node data. Can you please suggest me a way to sort the linked list in any other way, that would also help.

My code is:

#include<stdio.h>
#include<stdlib.h>
typedef struct node_type{
   int data;
   struct node_type *next;
}node;

typedef node *list;
list head;

void traverse()
{
   list temp;
   temp=head;
   printf("\nThe Data is: \n");
   while(temp!=NULL)
   {
      printf("%d\n",temp->data);
      temp=temp->next;
   }
   printf("\n\n");
}

void sort_list()
{
   list new,ptr;
   int temp;
   for(new=head;new->next!=NULL;new=new->next)
   {
       for(ptr=new->next;ptr->next!=NULL;ptr=ptr->next)
       {
           if(new->data > ptr->data)
              {
                  temp=new->data;
                  new->data=ptr->data;
                  ptr->data=temp;
              }  
        }
   }
   traverse();
   }

void main()
      {
         list temp,new;
         head=NULL;
         int n;
         char ch;
         temp=(list) malloc(sizeof(node));
         printf("\nGive data: ");
         scanf("%d",&temp->data);
         head=temp;
         printf("\n");
         printf("Enter more data(y/n): ");
         scanf("\n%c",&ch);
         while(ch=='y')
            {
               new=(list) malloc(sizeof(node));
               printf("Give data: ");
               scanf("%d",&new->data);
               temp->next=new;
               temp=new;
               printf("\nEnter more data(y/n): ");
               scanf("\n%c",&ch);
            }
       temp->next=NULL;
       traverse(head);
       printf("\n\nDo you want to sort the Link-List(y/n): ");
       scanf("\n%c",&ch);
       if(ch=='y')
          { 
             sort_list();
          }
    }

Input: 2 3 1 5 4

Output: 1 2 3 5 4

The inner loop of sort_list() is not considering the last node during sorting because of the condition ptr->next!=NULL in for loop. You should check it ptr with NULL :

for(ptr = new->next; ptr != NULL; ptr = ptr->next)
                     ^^^^

There are several things that needs to be fixed.

  • The main function needs to have a return value of int not void .
  • There are calls to traverse that pass head as an argument, which will give an error. Either update the traverse function to except a list argument, or fix those calls.
  • The sort_list function's inner for loop's condition should end when ptr == NULL ie

     for(ptr=new->next; ptr != NULL; ptr=ptr->next) 

The function could be more defensive, if head happens to be NULL , the first for loop would cause a segmentation fault when new->next != NULL becomes evaluated. You could have an if statement at the beginning of the function to guard against this.

  • The last thing is since malloc is being used, make sure that memory is freed. You will need to iterate through the list and free each node.

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