简体   繁体   English

我应该怎么做才能使链表中的最后一个节点参与冒泡排序?

[英]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. 我猜这是因为指针移到NULL并拒绝了最后一个节点数据。 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 输入:2 3 1 5 4

Output: 1 2 3 5 4 输出: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. 由于for循环中的条件ptr->next!=NULLsort_list()的内部循环在排序过程中未考虑最后一个节点。 You should check it ptr with NULL : 您应该使用NULL检查它ptr

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 . main函数需要具有int not void的返回值。
  • There are calls to traverse that pass head as an argument, which will give an error. traverse调用,该traversehead作为参数传递,这将产生错误。 Either update the traverse function to except a list argument, or fix those calls. traverse函数更新为list参数以外的任何一种,或者修复这些调用。
  • The sort_list function's inner for loop's condition should end when ptr == NULL ie ptr == NULLsort_list函数的内部for循环条件应结束

     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. 该函数可能更具防御性,如果head恰好是NULL ,则当评估new->next != NULL时,第一个for循环将导致分段错误。 You could have an if statement at the beginning of the function to guard against this. 您可以在函数的开头使用if语句来防止这种情况。

  • The last thing is since malloc is being used, make sure that memory is freed. 最后一件事是因为正在使用malloc ,请确保释放了内存。 You will need to iterate through the list and free each node. 您将需要遍历列表并释放每个节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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