Print linked list from middle into right-left order

Displaying middle right-left sequence

Here given code implementation process.

//C Program
//Print linked list from middle into right-left order
#include<stdio.h>
#include <stdlib.h> //for malloc function

//create structure
struct Node
{
  int data;
  struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
//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;
    }
  }
}
//Display element of Node
void display(struct Node*temp)
{

  if(temp==NULL)
  {
    printf("Empty linked list");
  }
  while(temp!=NULL)
  {
    printf("%3d",temp->data);
    temp=temp->next;
  }
}
//Print linked list element in right-left order at middle position
void show(struct Node*head,struct Node*slow,struct Node**middle)
{

  if(head==NULL) return;

  if(head->next != NULL && head->next->next != NULL)
  {
    //Until the not get middle elements
    show(head->next->next,slow->next,middle);
  }
  else if(head->next!=NULL && head->next->next==NULL)
  {
    //When even number of elements
    *middle=(slow)->next;
  }
  
  

  if(*middle !=NULL)
  {
    printf("%3d",(*middle)->data );
    *middle=(*middle)->next;
  }
  else
  {
     *middle=slow->next;
  }
  printf("%3d",(slow)->data );
}
int main()
{
  //create node pointer
  struct Node*head=NULL;
  
  //insert element of linked list

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

  printf("\n Nodes :");
  //display all node
  display(head);
  printf("\n O / P :");

  struct Node*middle=NULL;
  
  show(head,head,&middle);

  //case 2
  struct Node*root=NULL;
  insert(&root,1);
  insert(&root,2);
  insert(&root,3);
  insert(&root,4);
  insert(&root,5);
  insert(&root,6);
  insert(&root,7);


  middle=NULL;

  printf("\n Nodes :");
  display(root);

  printf("\n O / P :");
  show(root,root,&middle);
  return 0;

}

Output

 Nodes :  1  2  3  4  5  6  7  8
 O / P :  5  4  6  3  7  2  8  1
 Nodes :  1  2  3  4  5  6  7
 O / P :  4  5  3  6  2  7  1
//C++ Program 
//Print linked list from middle into right-left 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 show( Node*, 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;
  }
}

//Print linked list element in right-left order at middle position
void LinkedList:: show( Node*head, Node*slow, Node**middle)
{

  if(head==NULL) return;

  if(head->next != NULL && head->next->next != NULL)
  {
    //Until the not get middle elements
    show(head->next->next,slow->next,middle);
  }
  else if(head->next!=NULL && head->next->next==NULL)
  {
    //When even number of elements
    *middle=(slow)->next;
  }
  


  if(*middle !=NULL)
  {
    cout<<(*middle)->data<< "  ";
    *middle=(*middle)->next;
  }
  else
  {
    *middle=slow->next;
  }

  cout<<slow->data << "  ";
}
int main()
{

  //create object
  LinkedList obj;
  //insert element of linked list

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

  //display all node
  obj.display();
  cout<<(" O / P :  ");

  Node*middle=NULL;
  
  obj.show(obj.head,obj.head,&middle);

  //case 2
  LinkedList root;
  root.insert(1);
  root.insert(2);
  root.insert(3);
  root.insert(4);
  root.insert(5);
  root.insert(6);
  root.insert(7);


  middle=NULL;

  root.display();

  cout<<(" O / P :  ");
  root.show(root.head,root.head,&middle);
  return 0;
}

Output

Linked List : 1  2  3  4  5  6  7  8  
 O / P :  5  4  6  3  7  2  8  1  
Linked List : 1  2  3  4  5  6  7  
 O / P :  4  5  3  6  2  7  1 
//Java Program
//Print linked list from middle into right-left order
public class LinkedList
{

  static class Node
  {
    int data;
    Node next;
  }
  public Node head,middle;
  //Class constructors
  LinkedList()
  {
    head=null;
    middle=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"); 
    }
  }
  
  //Print linked list element in right-left order at middle position
  public void show( Node head, Node slow)
  {

    if(head==null) return;

    if(head.next != null && head.next.next != null)
    {
      //Until the not get middle elements
      show(head.next.next,slow.next);
    }
    else if(head.next!=null && head.next.next==null)
    {
      //When even number of elements
      middle = slow.next;
    }
    
    

    if( middle != null)
    {
      System.out.print("  "+middle.data);
      middle=( middle).next;
    }
    else
    {
      middle=slow.next;
    }
    System.out.print("  "+slow.data);
  }
  
  public void  right_left()
  {

    middle=null;

    show(head,head);
  }



  public static void main(String[] args) 
  {

    LinkedList obj=new LinkedList();
    obj.insert(1);
    obj.insert(2);
    obj.insert(3);
    obj.insert(4);
    obj.insert(5);
    obj.insert(6);
    obj.insert(7);
    obj.insert(8);

    System.out.println("\n Nodes :");
    //display all node
    obj.display();
    System.out.println(" O / P :");
    obj.right_left();

    //case 2
    LinkedList root=new LinkedList();
    root.insert(1);
    root.insert(2);
    root.insert(3);
    root.insert(4);
    root.insert(5);
    root.insert(6);
    root.insert(7);


    System.out.println("\n Nodes :");
    root.display();

    System.out.println(" O / P :");
    root.right_left();

  }
}

Output

  1  2  3  4  5  6  7  8
 O / P :
  5  4  6  3  7  2  8  1
 Nodes :
  1  2  3  4  5  6  7
 O / P :
  4  5  3  6  2  7  1
#Python Program
#Print linked list from middle into right-left 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
    self.middle=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
  

  def show(self,head,slow):
    if(head==None) :
      return

    if(head.next != None and head.next.next != None):
      #Until the not get middle elements
      self.show(head.next.next,slow.next)
    
    elif(head.next != None and head.next.next == None):
      #When even number of elements
      self.middle = slow.next
    
    
    

    if(self.middle != None):
      print(self.middle.data,end="  ")
      self.middle = self.middle.next
    
    else:
      self.middle=slow.next

    print(slow.data,end="  ")
  
  
  def rightLeft(self):

    self.middle=None

    self.show(self.head,self.head)
  
      
    

def main():
    #Create Object of class Linked
    obj=LinkedList()
    obj.insert(1)
    obj.insert(2)
    obj.insert(3)
    obj.insert(4)
    obj.insert(5)
    obj.insert(6)
    obj.insert(7)
    obj.insert(8)

    print("\nNodes :")
    #display all node
    obj.display()
    print("\nO / P :")
    obj.rightLeft()

    #case 2
    root=LinkedList()
    root.insert(1)
    root.insert(2)
    root.insert(3)
    root.insert(4)
    root.insert(5)
    root.insert(6)
    root.insert(7)


    print("\nNodes :")
    root.display()

    print("\nO / P :")
    root.rightLeft()
if __name__=="__main__":
    main()

Output

Nodes :
1  2  3  4  5  6  7  8  
O / P :
5  4  6  3  7  2  8  1  
Nodes :
1  2  3  4  5  6  7  
O / P :
4  5  3  6  2  7  1 
//C# Program
//Print linked list from middle into right-left order

using System;
public class Node
{
  public int data;
  public Node next;
}

public class LinkedList
{


  public Node head,middle;
  //Class constructors
  LinkedList()
  {
    head=null;
    middle=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"); 
    }
  }

  //Print linked list element in right-left order at middle position
  public void show( Node head, Node slow)
  {

    if(head==null) return;

    if(head.next != null && head.next.next != null)
    {
      //Until the not get middle elements
      show(head.next.next,slow.next);
    }
    else if(head.next!=null && head.next.next==null)
    {
      //When even number of elements
      middle = slow.next;
    }



    if( middle != null)
    {
      Console.Write("  "+middle.data);
      middle=( middle).next;
    }
    else
    {
      middle=slow.next;
    }
    Console.Write("  "+slow.data);
  }

  public void  rightLeft()
  {

    middle=null;

    show(head,head);
  }



  public static void Main(String[] args) 
  {

    LinkedList obj=new LinkedList();
    obj.insert(1);
    obj.insert(2);
    obj.insert(3);
    obj.insert(4);
    obj.insert(5);
    obj.insert(6);
    obj.insert(7);
    obj.insert(8);

    Console.WriteLine("\n Nodes :");
    //display all node
    obj.display();
    Console.WriteLine(" O / P :");
    obj.rightLeft();

    //case 2
    LinkedList root=new LinkedList();
    root.insert(1);
    root.insert(2);
    root.insert(3);
    root.insert(4);
    root.insert(5);
    root.insert(6);
    root.insert(7);


    Console.WriteLine("\n Nodes :");
    root.display();

    Console.WriteLine(" O / P :");
    root.rightLeft();

  }
}

Output

 Nodes :
  1  2  3  4  5  6  7  8
 O / P :
  5  4  6  3  7  2  8  1
 Nodes :
  1  2  3  4  5  6  7
 O / P :
  4  5  3  6  2  7  1
<?php
//Php program 
//Print linked list from middle into right-left order
class Node
{
  public $data;
  public $next;
  function __construct($data)
  {
    $this->data = $data;
    $this->next = NULL;
  }
}
class LinkedList
{

  public $head;
  public $middle;
  function __construct()
  {
    $head = NULL;
    $middle = 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
        }
      }   
    }

    //Print linked list element in right-left order at middle position
    function show($head,$slow)
    {

      if($head==NULL) return;

      if($head->next != NULL && $head->next->next != NULL)
      {
        //Until the not get middle elements
        $this->show($head->next->next,$slow->next);
      }
      else if($head->next!=NULL && $head->next->next==NULL)
      {
        //When even number of elements
        $this->middle=$slow->next;
      }

     

      if($this->middle !=NULL)
      {
        echo "  ".$this->middle->data;
        $this->middle=$this->middle->next;
      }
      else
      {
        $this->middle= $slow->next;
      }
      echo "  ".$slow->data;
    }
    function  right_left()
    {

      $this->middle=NULL;

      $this->show($this->head,$this->head);
    }

  }


  function main()
  {
    //Make a object of LinkedList class
    $obj= new LinkedList();

    /*
    *Insert following nodes in linked list
    */
    $obj->insert(1);
    $obj->insert(2);
    $obj->insert(3);
    $obj->insert(4);
    $obj->insert(5);
    $obj->insert(6);
    $obj->insert(7);
    $obj->insert(8);

    //display all node
    $obj->display();
    echo ("\n O / P :  ");
    $obj->right_left();




    //Case 2
    $root=new LinkedList();
    $root->insert(1);
    $root->insert(2);
    $root->insert(3);
    $root->insert(4);
    $root->insert(5);
    $root->insert(6);
    $root->insert(7);

    $root->display();

    echo ("\n O / P :  ");
    $root->right_left();

  }
  main();
  ?>

Output

Linked List :  1  2  3  4  5  6  7  8
 O / P :    5  4  6  3  7  2  8  1
Linked List :  1  2  3  4  5  6  7
 O / P :    4  5  3  6  2  7  1


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