Rearrange linked list in increasing order

Rearrange linked list in increasing order

Here given code implementation process.

//C Program
//Rearrange linked list in increasing order (Sort order list)
#include <stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
  int data;
  struct Node*next;
};


//insert Node element
void insert(struct Node**head,int value)
{
  //Create dynamic node
  struct Node*node=(struct Node*)malloc(sizeof(struct Node));
  if(node==NULL)
  {
    printf("Memory overflow\n");

  }else
  {
    node->data=value;
    node->next=NULL;
    if(*head==NULL)
    {
      *head=node;

    }
    else
    {
      struct Node*temp=*head;
      //find last node
      while(temp->next!=NULL)
      {
        temp=temp->next;
      }
      //add node at last possition
      temp->next=node;
    }
  }
}

void arrange(struct Node*head,struct Node*element)
{

  if(head==NULL)
  {
    return;
  }
  else
  {
    struct Node*temp=head;
    //Finding location of inserting nodes
    while(temp->next!=NULL && temp->next->data < element->data)
    {
      temp=temp->next;
    }

    element->next=temp->next;
    temp->next=element;
  }

}
void sortList(struct Node**head, struct Node*temp)
{
  if(*head==NULL || temp==NULL)
  {
    return;
  }
  struct Node*next=temp->next;

  if((*head)->data > temp->data)
  {
    //get first smallest element
    *head=temp;
  }
  temp->next=NULL;
  //Recursively execute until the last node
  sortList(head,next);
  
  if(*head!=temp)
  {
    //Add node to its proper position
    arrange(*head,temp);
  }
}


//Display element of Node
void display(struct Node*temp)
{
  
  if(temp==NULL)
  {
    printf("Empty linked list");
  }

  while(temp!=NULL)
  {
    printf("%d  ",temp->data);
    temp=temp->next;
  }
}
int main()
{
  //create node pointer
  struct Node*head=NULL;


  insert(&head,3);
  insert(&head,5);
  insert(&head,2);
  insert(&head,1);
  insert(&head,6);
  insert(&head,4);
  insert(&head,-3);


  printf("Before Sort\n");
  //display all node
  display(head);

  printf("\nAfter Sort\n");
  sortList(&head,head);
  //display all node
  display(head);
  return 0;
}

Output

Before Sort
3  5  2  1  6  4  -3  
After Sort
-3  1  2  3  4  5  6
//C++ Program 
//Rearrange linked list in increasing order
#include <iostream>
using namespace std;

//create structure
struct Node
{
  int data;
  struct Node*next;
};
class LinkedList
{

public:
  Node*head;//head pointer
  LinkedList();
  void insert(int);
  void display();
  void sortList(Node**,Node*);
  void arrange(Node*,Node*);
  
};
LinkedList::LinkedList()
{
  head=NULL;
}
//insert node at end of linked list
void LinkedList :: insert(int value)
{
    //Create dynamic node
  struct Node*node=new Node;
  if(node==NULL)
  {
    cout<<"Memory overflow\n";
  }else
  {
    node->data=value;
    node->next=NULL;
    if(head==NULL)
    {
      //base condition
      head=node;
    }else
    {
      Node*temp=head;
      while(temp->next!=NULL)
      {
        temp=temp->next;
      }
      //add newly node at last
      temp->next=node;
    }
  }
}
//display all node value in linked list
void LinkedList:: display()
{
  if(head==NULL)
  {
    cout<<"Empty linked list";
  }
  else
  {
    Node*temp=head;
    cout<<"\nLinked List : ";
    while(temp!=NULL)
    {
      //print node value
      cout<<temp->data<<"  ";
      temp=temp->next;
    }
    cout<<endl;
  }
}
void LinkedList:: arrange( Node*head, Node*element)
{

  if(head==NULL)
  {
    return;
  }
  else
  {
    Node*temp=head;
    //Finding location of inserting node
    while(temp->next!=NULL && temp->next->data < element->data)
    {
      temp=temp->next;
    }
    element->next=temp->next;
    temp->next=element;
  }

}
void LinkedList:: sortList( Node**head,  Node*temp)
{
  if(*head==NULL || temp==NULL)
  {
    return;
  }
  Node*next=temp->next;

  if((*head)->data > temp->data)
  {
    //Get first smallest element
    *head=temp;
  }
  temp->next=NULL;
  //Recursively execute until the last node
  sortList(head,next);
  
  if(*head!=temp)
  {
    //Add node to its proper position
    arrange(*head,temp);
  }
}

int main()
{

  //create object
  LinkedList obj;
 

  obj.insert(3);
  obj.insert(5);
  obj.insert(2);
  obj.insert(1);
  obj.insert(6);
  obj.insert(4);
  obj.insert(-3);


  cout<<("Before Sort\n");
  //display all node
  obj.display();

  cout<<("\nAfter Sort\n");
  obj.sortList(&obj.head,obj.head);
  //display all node
  obj.display();
  return 0;
}

Output

Before Sort

Linked List : 3  5  2  1  6  4  -3  

After Sort

Linked List : -3  1  2  3  4  5  6  
//Java Program
//Rearrange linked list in increasing order
public class LinkedList
{

  static class Node
  {
    int data;
    Node next;
  }
  public Node head,back;
  //Class constructors
  LinkedList()
  {
    head=null;
    back=null;
  } 
  //insert element
  public void insert(int value)
  {
    //Create  node
    Node node=new Node();
    node.data=value;
    node.next=null;
    if(head==null)
    {
      head=node;
    }
    else
    {
      Node temp=head;
      //find last node
      while(temp.next!=null)
      {
        temp=temp.next;
      }
      temp.next=node;
    }
    
  }
  //Display all Linked List elements
  public void display()
  {
    if(head!=null)
    {
      Node temp=head;
      while(temp!=null)
      {
        System.out.print("  "+temp.data);
        temp=temp.next;
      }
      System.out.println();
    }else
    {
      System.out.println("Empty Linked list"); 
    }
  }
  
  public void arrange(Node element)
  {

    if(head==null)
    {
      return;
    }
    else
    {
      Node temp=head;
      //Finding location of inserting nodes
      while(temp.next!=null && temp.next.data < element.data)
      {
        temp=temp.next;
      }

      element.next=temp.next;
      temp.next=element;
    }

  }
  public void sortList(Node temp)
  {
    if(head==null || temp==null)
    {
      return;
    }
    Node next=temp.next;

    if(head.data > temp.data)
    {
      //Get first smallest element
      head=temp;
    }
    temp.next=null;
    //Recursively execute until the last node
    sortList(next);
    
    if(head!=temp)
    {
      //Add node to its proper position
      arrange(temp);
    }
  }
  public static void main(String[] args) 
  {

    LinkedList obj=new LinkedList();

    obj.insert(3);
    obj.insert(5);
    obj.insert(2);
    obj.insert(1);
    obj.insert(6);
    obj.insert(4);
    obj.insert(-3);


    System.out.print("Before Sort\n");
    //display all node
    obj.display();

    System.out.print("\nAfter Sort\n");
    obj.sortList(obj.head);
    //display all node
    obj.display();

  }
}

Output

Before Sort
  3  5  2  1  6  4  -3

After Sort
  -3  1  2  3  4  5  6
#Python Program
#Rearrange linked list in increasing order
class Node:
  def __init__(self,data):
    self.data=data
    self.next=None

#Create Class LinkedList    
class LinkedList:
  def __init__(self):
    #Assign default value
    self.head=None

  #insert new node to linked list  
  def insert(self,data):
    node=Node(data)
    node.next=None
    if self.head==None:
      self.head=node
    else:
      temp=self.head
      while temp.next!=None:
          temp=temp.next
      #add node    
      temp.next=node

  def display(self):
    if(self.head==None):
      print("Empty Linked List")
      return

    temp=self.head
    
    while(temp!=None):
      print(temp.data,end="  ")
      temp=temp.next

    print()

  def arrange(self,element):

    if(self.head==None):
      return;
    
    else:
      temp=self.head;
      #Finding location of inserting nodes
      while(temp.next!=None and temp.next.data < element.data):
        temp=temp.next;
      

      element.next=temp.next;
      temp.next=element;
    

  
  def sortList(self,temp):

    if(self.head==None or temp==None):
      return;
    
    nextNode=temp.next;

    if(self.head.data > temp.data):
      #Get first smallest element
      self.head=temp;
    
    temp.next=None;
    #Recursively execute until the last node
    self.sortList(nextNode);
    
    if(self.head!=temp):
      #Add node to its proper position
      self.arrange(temp);
    

def main():
    #Create Object of class Linked
    obj = LinkedList()

    obj.insert(3);
    obj.insert(5);
    obj.insert(2);
    obj.insert(1);
    obj.insert(6);
    obj.insert(4);
    obj.insert(-3);


    print("Before Sort\n");
    #display all node
    obj.display();

    print("\nAfter Sort\n");
    obj.sortList(obj.head);
    #display all node
    obj.display();
if __name__=="__main__":
    main()

Output

Before Sort

3  5  2  1  6  4  -3  

After Sort

-3  1  2  3  4  5  6  
//C# Program
//Rearrange linked list in increasing order
using System;
public class Node
{
  public int data;
  public Node next;
}

public class LinkedList
{


  public Node head,back;
  //Class constructors
  LinkedList()
  {
    head=null;

  } 
  //insert element
  public void insert(int value)
  {
    //Create  node
    Node node=new Node();
    node.data=value;
    node.next=null;
    if(head==null)
    {
      head=node;
    }
    else
    {
      Node temp=head;
      //find last node
      while(temp.next!=null)
      {
        temp=temp.next;
      }
      temp.next=node;
    }

  }
  //Display all Linked List elements
  public void display()
  {
    if(head!=null)
    {
      Node temp=head;
      while(temp!=null)
      {
        Console.Write("  "+temp.data);
        temp=temp.next;
      }
      Console.WriteLine();
    }else
    {
      Console.WriteLine("Empty Linked list"); 
    }
  }

  public void arrange(Node element)
  {

    if(head==null)
    {
      return;
    }
    else
    {
      Node temp=head;
      //Finding location of inserting nodes
      while(temp.next!=null && temp.next.data < element.data)
      {
        temp=temp.next;
      }

      element.next=temp.next;
      temp.next=element;
    }

  }
  public void sortList(Node temp)
  {
    if(head==null || temp==null)
    {
      return;
    }
    Node next=temp.next;

    if(head.data > temp.data)
    {
      //Get first smallest element
      head=temp;
    }
    temp.next=null;
    //Recursively execute until the last node
    sortList(next);

    if(head!=temp)
    {
      //Add node to its proper position
      arrange(temp);
    }
  }
  public static void Main(String[] args) 
  {

    LinkedList obj=new LinkedList();

    obj.insert(3);
    obj.insert(5);
    obj.insert(2);
    obj.insert(1);
    obj.insert(6);
    obj.insert(4);
    obj.insert(-3);


    Console.Write("Before Sort\n");
    //display all node
    obj.display();

    Console.Write("\nAfter Sort\n");
    obj.sortList(obj.head);
    //display all node
    obj.display();

  }
}

Output

Before Sort
  3  5  2  1  6  4  -3

After Sort
  -3  1  2  3  4  5  6
<?php
//Php program 
//Rearrange linked list in circular order
class Node
{
  public $data;
  public $next;
  function __construct($data)
  {
    $this->data = $data;
    $this->next = NULL;
  }
}
class LinkedList
{

  public $head;
 
  function __construct()
  {
    $head = NULL;
  }
    /*
    * Append the Given data value at end of linked list
    * Fun : insert
    * Parm: data value
    *@return None
    */
    function insert($data)
    {
      $newNode=new Node($data); 
      if($this->head==NULL)
      {
        $this->head=$newNode;
      }else
      {
        $temp=$this->head;
            //find last node of linked list
        while($temp->next!=NULL)
        {
          $temp=$temp->next;
        }
            //add new node to last of linked list
        $temp->next=$newNode;
      }
    }
        //Display all inserted node in linked list
    function display()
    {
      if($this->head==NULL)
      {
        echo "Empty Linked List";
      }
      else
      {
        $temp=$this->head;
        echo "\nLinked List :";
        while($temp!=NULL)
        {
          //display node value
          echo "  ".$temp->data;
          $temp=$temp->next; //visit to next node
        }
      }   
    }

    function arrange($element)
    {

      if($this->head==NULL)
      {
        return;
      }
      else
      {
        $temp=$this->head;
       //Finding location of inserting nodes
        while($temp->next!=NULL && $temp->next->data < $element->data)
        {
          $temp=$temp->next;
        }

        $element->next=$temp->next;
        $temp->next=$element;
      }

    }
    function sortList($temp)
    {
      if($this->head==NULL || $temp==NULL)
      {
        return;
      }
      $next=$temp->next;

      if($this->head->data > $temp->data)
      {
        //Get first smallest element
        $this->head=$temp;
      }
      $temp->next=NULL;
      //Recursively execute until the last node
      $this->sortList($next);

      if($this->head!=$temp)
      {
        //Add node to its proper position
        $this->arrange($temp);
      }
    }

  }


  function main()
  {

    //create object
    $obj = new LinkedList();


    $obj->insert(3);
    $obj->insert(5);
    $obj->insert(2);
    $obj->insert(1);
    $obj->insert(6);
    $obj->insert(4);
    $obj->insert(-3);


    echo ("Before Sort");
    //display all node
    $obj->display();

    echo ("\nAfter Sort");
    $obj->sortList($obj->head);
    //display all node
    $obj->display();

  }
  main();
  ?>

Output

Before Sort
Linked List :  3  5  2  1  6  4  -3
After Sort
Linked List :  -3  1  2  3  4  5  6

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved