“пузырь сортируйте связанный список в c” Ответ

пузырь сортируйте связанный список в c

// C program to implement Bubble Sort on singly linked list
#include <stdio.h>
#include <stdlib.h>
  
/* structure for a node */
struct Node
{
    int data;
    struct Node *next;
};
  
/* Function to insert a node at the beginning of a linked list */
void insertAtTheBegin(struct Node **start_ref, int data);
  
/* Function to bubble sort the given linked list */
void bubbleSort(struct Node *start);
  
/* Function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b);
  
/* Function to print nodes in a given linked list */
void printList(struct Node *start);
  
int main()
{
    int arr[] = {12, 56, 2, 11, 1, 90};
    int list_size, i;
  
    /* start with empty linked list */
    struct Node *start = NULL;
  
    /* Create linked list from the array arr[].
      Created linked list will be 1->11->2->56->12 */
    for (i = 0; i< 6; i++)
        insertAtTheBegin(&start, arr[i]);
  
    /* print list before sorting */
    printf("\n Linked list before sorting ");
    printList(start);
  
    /* sort the linked list */
    bubbleSort(start);
  
    /* print list after sorting */
    printf("\n Linked list after sorting ");
    printList(start);
  
    getchar();
    return 0;
}
  
  
/* Function to insert a node at the beginning of a linked list */
void insertAtTheBegin(struct Node **start_ref, int data)
{
    struct Node *ptr1 = (struct Node*)malloc(sizeof(struct Node));
    ptr1->data = data;
    ptr1->next = *start_ref;
    *start_ref = ptr1;
}
  
/* Function to print nodes in a given linked list */
void printList(struct Node *start)
{
    struct Node *temp = start;
    printf("\n");
    while (temp!=NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
}
  
/* Bubble sort the given linked list */
void bubbleSort(struct Node *start)
{
    int swapped, i;
    struct Node *ptr1;
    struct Node *lptr = NULL;
  
    /* Checking for empty list */
    if (start == NULL)
        return;
  
    do
    {
        swapped = 0;
        ptr1 = start;
  
        while (ptr1->next != lptr)
        {
            if (ptr1->data > ptr1->next->data)
            { 
                swap(ptr1, ptr1->next);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapped);
}
  
/* function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b)
{
    int temp = a->data;
    a->data = b->data;
    b->data = temp;
}
Fair Finch

Сортировка связанного списка с использованием Bubble Sort

void linked_list::sort ()
{
  int i,j=0;
  int counter=0;
  node *curr=head;
  node *trail=head;
  node *temp=NULL;

  while (curr !=NULL)
  {
    curr=curr->next;    //couting the number of items I have in my list. 
    counter++;          //this works fine.
  }

  curr=head->next;          // reseting the curr value for the 2nd position.

  for (i=0; i<counter; i++)
  {
    while (curr != NULL)
    {
      if (trail->data > curr->data)
      {
        temp=curr->next;      //bubble sort for the pointers.
        curr->next=trail;
        trail->next=temp;

        temp=curr;         //reseting trail and curr. curr gets back to be infront.
        curr=trail;     
        trail=temp;

        if (j==0)   //i'm using j to determine the start of the loop so i won't loose the head pointer.
        {
          head=trail;
        }

      }
      j++;
      trail=curr;
      curr=curr->next;   //traversing thru the list. nested loop.
    }

    trail=head;
    curr=trail->next;
    curr->next=trail->next->next;  //traversing thru the list. outer loop.
    j=0;
  }
}
Fair Ferret

Ответы похожие на “пузырь сортируйте связанный список в c”

Вопросы похожие на “пузырь сортируйте связанный список в c”

Смотреть популярные ответы по языку