Convert a doubly linked list into reversal anti clockwise spiral form

Reverse anti-clockwise spiral form of doubly linked list

Here given code implementation process.

// C Program
// Convert a doubly linked list into reversal anti clockwise spiral form
#include <stdio.h>

#include <stdlib.h> //for malloc function

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


//head and tail pointers
struct Node *head = NULL, *tail = NULL;

//insert Node element of end of linked list
void insert(int value) {

  //Create a dynamic Node
  struct Node *node = (struct Node *) malloc(sizeof(struct Node));
  if (node == NULL) {
    printf("Memory overflow\n");
  } else {
    //Set data value
    node->data = value;
    node->next = NULL;
    node->prev = NULL;
    if (head == NULL) {
      head = node;
      tail = node;

    } else {
      node->prev = tail;
      tail->next = node;

      tail = node;

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

    printf("\n Head to Tail Nodes : \n");
    //Traverse doubly linked list from front to rear
    while (temp != NULL) {
      //print Node value
      printf("%3d", temp->data);
      temp = temp->next;
    }
    printf("\n Tail to Head Nodes : \n");

    temp = tail;

    //Traverse doubly linked list from rear to front
    while (temp != NULL) {
      //print Node value
      printf("%3d", temp->data);
      temp = temp->prev;
    }
  }

}

//Reverse linked list nodes
struct Node* reverse(struct Node*head)
{
  struct Node*temp=head,*next=NULL;
  while(temp!=NULL)
  {
    //make new upcoming node as head
    head=temp;

    temp=temp->next;
    
    //modified current node link
    (head)->prev=temp;
    (head)->next=next;
     next=head;
  }
  return head;
}

void arrange()
{

  if(head==NULL || head->next==NULL )
  {
    //When less than two nodes
    return ;
  }
  struct Node*last=tail,*current=head,*auxiliary=NULL,*temp=NULL,*back=NULL;

  struct Node*new_head=last;

  while(current!=NULL && last!=NULL)
  {
    if(current==last)
    {
      current->next=NULL;
      current->prev=back;

      tail = current;
      break;
    }
    else if(current->next==last)
    {
      current->next=NULL;
      current->prev=last;
      last->next=current;
      last->prev=back;

      tail = current;
      break;
    }

    auxiliary=current->next;
    temp=last->prev;

    current->prev=last;
    last->next=current;

    current->next = temp;
    last->prev = back;
    back=current;

    current=auxiliary;
    last=temp;

  }
  tail = new_head;
  head = reverse(new_head);
  
}
int main() {

  //Insert element of linked list
  insert(1);
  insert(2);
  insert(3);
  insert(4);
  insert(5);
  insert(6);
  insert(7);
  insert(8);
  insert(9);

  printf("\nBefore Reversal spiral ");
  //display all node
  display();

  arrange();
  printf("\nAfter Reversal spiral ");
  display();

  return 0;
}

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
/*
 C++ Program
 Convert a doubly linked list into reversal anti clockwise spiral form
*/

#include<iostream>

using namespace std;
class Node {
  public:
    int data;
  Node *next;
  Node *prev;
  Node(int value) {
    this->data = value;
    this->next = NULL;
    this->prev = NULL;
  }
};
class LinkedList {
  public:
    Node *head;
  Node *tail;
  LinkedList() {
    this->head = NULL;
    this->tail = NULL;
  }
  void insert(int value) {
    Node *node = new Node(value);
    if (node == NULL) {
      cout << "Memory overflow\n";
      return;
    }
    if (this->head == NULL) {
      this->head = node;
      this->tail = node;
    } else {
      node->prev = this->tail;
      this->tail->next = node;
      this->tail = node;
    }
  }
  void display() {
    Node *temp = this->head;
    if (temp == NULL) {
      cout << "\nEmpty linked list\n";
      return;
    }
    cout << "\n Head to Tail Nodes : \n  ";
    while (temp != NULL) {
      cout << temp->data << "  ";
      temp = temp->next;
    }
    temp = this->tail;
    cout << "\n Tail to Head Nodes : \n  ";
    while (temp != NULL) {
      cout << temp->data << "  ";
      temp = temp->prev;
    }
  }
  Node *reverse(Node *front) {
    Node *temp = front, *back = NULL;
    while (temp != NULL) {
      front = temp;
      temp = temp->next;
      front->prev = temp;
      front->next = back;
      back = front;
    }
    return front;
  }
  void arrange() {
    if (this->head == NULL || this->head->next == NULL) {
      return;
    }
    Node *last = this->tail, *current = this->head, *auxiliary = NULL, *temp = NULL, *back = NULL;
    Node *new_head = last;
    while (current != NULL && last != NULL) {
      if (current == last) {
        current->next = NULL;
        current->prev = back;
        this->tail = current;
        break;
      } else
      if (current->next == last) {
        current->next = NULL;
        current->prev = last;
        last->next = current;
        last->prev = back;
        this->tail = current;
        break;
      }
      auxiliary = current->next;
      temp = last->prev;
      current->prev = last;
      last->next = current;
      current->next = temp;
      last->prev = back;
      back = current;
      current = auxiliary;
      last = temp;
    }
    this->tail = new_head;
    this->head = this->reverse(new_head);
  }
};
int main() {
  LinkedList obj ;
  obj.insert(1);
  obj.insert(2);
  obj.insert(3);
  obj.insert(4);
  obj.insert(5);
  obj.insert(6);
  obj.insert(7);
  obj.insert(8);
  //obj.insert(9);
  cout << "\nBefore Reversal spiral ";
  obj.display();
  obj.arrange();
  cout << "\nAfter Reversal spiral ";
  obj.display();
  return 0;
}

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
/*
  Java Program
  Convert a doubly linked list into reversal anti clockwise spiral form
*/
class Node {

  public int data;

  public Node next;

  public Node prev;

  public Node(int value) {
    //Setup initial values of linked list node
    this.data = value;
    this.next = null;
    this.prev = null;
  }
}
class LinkedList {

  public Node head;
  public Node tail;

  public LinkedList() {
    head = null;
    tail = null;
  }
  public void insert(int value) {
    Node node = new Node(value);

    if (node == null) {

      System.out.print("Memory overflow\n");
      return;
    }
    if (head == null) {
      head = node;
      tail = node;
    } else {
      //Add Node at the end of linked list
      node.prev = tail;
      tail.next = node;
      tail = node;
    }
  }



  public void display() {
    Node temp = head;
    if (temp == null) {
      System.out.print("\nEmpty linked list\n");
      return;
    }

    System.out.print("\n Head to Tail Nodes : \n  ");

    while (temp != null) {
      System.out.print(temp.data + "  ");
      temp = temp.next;
    }

    temp = tail;

    System.out.print("\n Tail to Head Nodes : \n  ");

    while (temp != null) {
      System.out.print(temp.data + "  ");
      temp = temp.prev;
    }

  }
  public Node reverse(Node front) {
    Node temp = front, back = null;

    while (temp != null) {
      front = temp;
      temp = temp.next;
      front.prev = temp;
      front.next = back;
      back = front;
    }
    return front;
  }
  public void arrange() {

    if (head == null || head.next == null) {
      return;
    }
    Node last = tail, current = head, auxiliary = null, temp = null, back = null;
    Node new_head = last;

    while (current != null && last != null) {

      if (current == last) {
        current.next = null;
        current.prev = back;
        tail = current;
        break;
      } else
      if (current.next == last) {
        current.next = null;
        current.prev = last;
        last.next = current;
        last.prev = back;
        tail = current;
        break;
      }
      auxiliary = current.next;
      temp = last.prev;
      current.prev = last;
      last.next = current;
      current.next = temp;
      last.prev = back;
      back = current;
      current = auxiliary;
      last = temp;
    }
    tail = new_head;
    head = reverse(new_head);
  }




  public static void main(String[] args) {

    LinkedList obj = new LinkedList();
    //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);
    obj.insert(9);

    System.out.print("\nBefore Reversal spiral ");
    //Display all node
    obj.display();


    obj.arrange();

    System.out.print("\nAfter Reversal spiral ");

    //Display all node
    obj.display();
  }
}

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
/*
  C# Program
  Convert a doubly linked list into reversal anti clockwise spiral form
*/
using System;
public class Node {

	public int data;

	public Node next;

	public Node prev;

	public Node(int value) {
		//Setup initial values of linked list node
		this.data = value;
		this.next = null;
		this.prev = null;
	}
}
class LinkedList {

	public Node head;
	public Node tail;

	public LinkedList() {
		head = null;
		tail = null;
	}
	public void insert(int value) {
		Node node = new Node(value);

		if (node == null) {

			Console.Write("Memory overflow\n");
			return;
		}
		if (head == null) {
			head = node;
			tail = node;
		} else {
			//Add Node at the end of linked list
			node.prev = tail;
			tail.next = node;
			tail = node;
		}
	}



	public void display() {
		Node temp = head;
		if (temp == null) {
			Console.Write("\nEmpty linked list\n");
			return;
		}

		Console.Write("\n Head to Tail Nodes : \n  ");

		while (temp != null) {
			Console.Write(temp.data + "  ");
			temp = temp.next;
		}

		temp = tail;

		Console.Write("\n Tail to Head Nodes : \n  ");

		while (temp != null) {
			Console.Write(temp.data + "  ");
			temp = temp.prev;
		}

	}
	public Node reverse(Node front) {
		Node temp = front, back = null;

		while (temp != null) {
			front = temp;
			temp = temp.next;
			front.prev = temp;
			front.next = back;
			back = front;
		}
		return front;
	}
	public void arrange() {

		if (head == null || head.next == null) {
			return;
		}
		Node last = tail, current = head, auxiliary = null, temp = null, back = null;
		Node new_head = last;

		while (current != null && last != null) {

			if (current == last) {
				current.next = null;
				current.prev = back;
				tail = current;
				break;
			} else
				if (current.next == last) {
					current.next = null;
					current.prev = last;
					last.next = current;
					last.prev = back;
					tail = current;
					break;
				}
			auxiliary = current.next;
			temp = last.prev;
			current.prev = last;
			last.next = current;
			current.next = temp;
			last.prev = back;
			back = current;
			current = auxiliary;
			last = temp;
		}
		tail = new_head;
		head = reverse(new_head);
	}




	public static void Main(String[] args) {

		LinkedList obj = new LinkedList();
		//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);
		obj.insert(9);

		Console.Write("\nBefore Reversal spiral ");
		//Display all node
		obj.display();


		obj.arrange();

		Console.Write("\nAfter Reversal spiral ");

		//Display all node
		obj.display();
	}
}

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
# Python 3 Program
# Convert a doubly linked list into reversal anti clockwise spiral form

class Node :

  def __init__(self, value) :
    self.data = value
    self.next = None
    self.prev = None
  

class LinkedList :

  def __init__(self) :
    self.head = None
    self.tail = None
  
  def insert(self, value) :
    node = Node(value)
    if (node == None) :
      print("Memory overflow\n")
      return
    
    if (self.head == None) :
      self.head = node
      self.tail = node
    else :
      node.prev = self.tail
      self.tail.next = node
      self.tail = node
    
  
  def display(self) :
    temp = self.head
    if (temp == None) :
      print("\nEmpty linked list\n")
      return
    
    print("\n Head to Tail Nodes :  ")
    while (temp != None) :
      print(temp.data ,end="  ")
      temp = temp.next
    
    temp = self.tail
    print("\n Tail to Head Nodes :  ")
    while (temp != None) :
      print(temp.data ,end="  ")
      temp = temp.prev
    
  
  def reverse(self, front) :
    temp = front
    back = None
    while (temp != None) :
      front = temp
      temp = temp.next
      front.prev = temp
      front.next = back
      back = front
    
    return front
  
  def arrange(self) :
    if (self.head == None or self.head.next == None) :
      return
    
    last = self.tail
    current = self.head
    auxiliary = None
    temp = None
    back = None
    new_head = last
    while (current != None and last != None) :
      if (current == last) :
        current.next = None
        current.prev = back
        self.tail = current
        break
      elif (current.next == last) :
        current.next = None
        current.prev = last
        last.next = current
        last.prev = back
        self.tail = current
        break
      
      auxiliary = current.next
      temp = last.prev
      current.prev = last
      last.next = current
      current.next = temp
      last.prev = back
      back = current
      current = auxiliary
      last = temp
    
    self.tail = new_head
    self.head = self.reverse(new_head)
  

def main() :
  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)
  obj.insert(9)
  print("\nBefore Reversal spiral ")
  obj.display()
  obj.arrange()
  print("\nAfter Reversal spiral ")
  obj.display()

if __name__ == "__main__":
  main()

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
# Ruby Program
# Convert a doubly linked list into reversal anti clockwise spiral form

class Node 
	attr_reader :data, :next, :prev
	attr_accessor :data, :next, :prev
	def initialize(value) 
		self.data = value
		self.next = nil
		self.prev = nil
	end
end

class LinkedList 
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		@head = nil
		@tail = nil
	end
	def insert(value) 
		node = Node.new(value)
		if (node == nil) 
			print("Memory overflow\n")
			return
		end
		if (@head == nil) 
			@head = node
			@tail = node
		else 
			node.prev = @tail
			@tail.next = node
			@tail = node
		end
	end
	def display() 
		temp = @head
		if (temp == nil) 
			print("\nEmpty linked list\n")
			return
		end
		print("\n Head to Tail Nodes  :\n  ")
		while (temp != nil) 
			print(temp.data ,"  ")
			temp = temp.next
		end
		temp = @tail
		print("\n Tail to Head Nodes  :\n  ")
		while (temp != nil) 
			print(temp.data ,"  ")
			temp = temp.prev
		end
	end
	def reverse(front) 
		temp = front
		back = nil
		while (temp != nil) 
			front = temp
			temp = temp.next
			front.prev = temp
			front.next = back
			back = front
		end
		return front
	end
	def arrange() 
		if (@head == nil or @head.next == nil) 
			return
		end
		last = @tail
		current = @head
		auxiliary = nil
		temp = nil
		back = nil
		new_head = last
		while (current != nil and last != nil) 
			if (current == last) 
				current.next = nil
				current.prev = back
				@tail = current
				break
			elsif (current.next == last) 
				current.next = nil
				current.prev = last
				last.next = current
				last.prev = back
				@tail = current
				break
			end
			auxiliary = current.next
			temp = last.prev
			current.prev = last
			last.next = current
			current.next = temp
			last.prev = back
			back = current
			current = auxiliary
			last = temp
		end
		@tail = new_head
		@head = self.reverse(new_head)
	end
end
def main() 
	obj = LinkedList.new()
	obj.insert(1)
	obj.insert(2)
	obj.insert(3)
	obj.insert(4)
	obj.insert(5)
	obj.insert(6)
	obj.insert(7)
	obj.insert(8)
	obj.insert(9)
	print("\nBefore Reversal spiral ")
	obj.display()
	obj.arrange()
	print("\nAfter Reversal spiral ")
	obj.display()
end


main()

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
<?php
/*
 Php Program
 Convert a doubly linked list into reversal anti clockwise spiral form
*/

class Node {
  public $data;
  public $next;
  public $prev;

  function __construct($value) {
    $this->data = $value;
    $this->next = null;
    $this->prev = null;
  }
}
class LinkedList {
  public $head;
  public $tail;

  function __construct() {
    $this->head = null;
    $this->tail = null;
  }
  public
  function insert($value) {
    $node = new Node($value);
    if ($node == null) {
      echo("Memory overflow\n");
      return;
    }
    if ($this->head == null) {
      $this->head = $node;
      $this->tail = $node;
    } else {
      $node->prev = $this->tail;
      $this->tail->next = $node;
      $this->tail = $node;
    }
  }
  public
  function display() {
    $temp = $this->head;
    if ($temp == null) {
      echo("\nEmpty linked list\n");
      return;
    }
    echo("\n Head to Tail Nodes : \n  ");
    while ($temp != null) {
      echo($temp->data . "  ");
      $temp = $temp->next;
    }
    $temp = $this->tail;
    echo("\n Tail to Head Nodes : \n  ");
    while ($temp != null) {
      echo($temp->data . "  ");
      $temp = $temp->prev;
    }
  }
  public
  function reverse($front) {
    $temp = $front;
    $back = null;
    while ($temp != null) {
      $front = $temp;
      $temp = $temp->next;
      $front->prev = $temp;
      $front->next = $back;
      $back = $front;
    }
    return $front;
  }
  public
  function arrange() {
    if ($this->head == null || $this->head->next == null) {
      return;
    }
    $last = $this->tail;
    $current = $this->head;
    $auxiliary = null;
    $temp = null;
    $back = null;
    $new_head = $last;
    while ($current != null && $last != null) {
      if ($current == $last) {
        $current->next = null;
        $current->prev = $back;
        $this->tail = $current;
        break;
      } else
      if ($current->next == $last) {
        $current->next = null;
        $current->prev = $last;
        $last->next = $current;
        $last->prev = $back;
        $this->tail = $current;
        break;
      }
      $auxiliary = $current->next;
      $temp = $last->prev;
      $current->prev = $last;
      $last->next = $current;
      $current->next = $temp;
      $last->prev = $back;
      $back = $current;
      $current = $auxiliary;
      $last = $temp;
    }
    $this->tail = $new_head;
    $this->head = $this->reverse($new_head);
  }
}

function main() {
  $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);
  $obj->insert(9);
  echo("\nBefore Reversal spiral ");
  $obj->display();
  $obj->arrange();
  echo("\nAfter Reversal spiral ");
  $obj->display();
}
main();

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
/*
 Node Js Program
 Convert a doubly linked list into reversal anti clockwise spiral form
*/

class Node {

	constructor(value) {
		this.data = value;
		this.next = null;
		this.prev = null;
	}
}
class LinkedList {
	
	constructor() {
		this.head = null;
		this.tail = null;
	}
	insert(value) {
		var node = new Node(value);
		if (node == null) {
			process.stdout.write("Memory overflow\n");
			return;
		}
		if (this.head == null) {
			this.head = node;
			this.tail = node;
		} else {
			node.prev = this.tail;
			this.tail.next = node;
			this.tail = node;
		}
	}
	display() {
		var temp = this.head;
		if (temp == null) {
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		process.stdout.write("\n Head to Tail Nodes : \n  ");
		while (temp != null) {
			process.stdout.write(temp.data + "  ");
			temp = temp.next;
		}
		temp = this.tail;
		process.stdout.write("\n Tail to Head Nodes : \n  ");
		while (temp != null) {
			process.stdout.write(temp.data + "  ");
			temp = temp.prev;
		}
	}
	reverse(front) {
		var temp = front;
		var back = null;
		while (temp != null) {
			front = temp;
			temp = temp.next;
			front.prev = temp;
			front.next = back;
			back = front;
		}
		return front;
	}
	arrange() {
		if (this.head == null || this.head.next == null) {
			return;
		}
		var last = this.tail;
		var current = this.head;
		var auxiliary = null;
		var temp = null;
		var back = null;
		var new_head = last;
		while (current != null && last != null) {
			if (current == last) {
				current.next = null;
				current.prev = back;
				this.tail = current;
				break;
			} else
			if (current.next == last) {
				current.next = null;
				current.prev = last;
				last.next = current;
				last.prev = back;
				this.tail = current;
				break;
			}
			auxiliary = current.next;
			temp = last.prev;
			current.prev = last;
			last.next = current;
			current.next = temp;
			last.prev = back;
			back = current;
			current = auxiliary;
			last = temp;
		}
		this.tail = new_head;
		this.head = this.reverse(new_head);
	}
}

function main() {
	var 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);
	obj.insert(9);
	process.stdout.write("\nBefore Reversal spiral ");
	obj.display();
	obj.arrange();
	process.stdout.write("\nAfter Reversal spiral ");
	obj.display();
}

main();

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5
/*
 Swift 4 Program
 Convert a doubly linked list into reversal anti clockwise spiral form
*/

class Node {
  var data: Int;
  var next: Node? ;
  var prev: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.next = nil;
    self.prev = nil;
  }
}
class LinkedList {
  var head: Node? ;
  var tail: Node? ;
  init() {
    self.head = nil;
    self.tail = nil;
  }
  func insert(_ value: Int) {
    let node: Node? = Node(value);
    if (node == nil) {
      print("Memory overflow\n");
      return;
    }
    if (self.head == nil) {
      self.head = node;
      self.tail = node;
    } else {
      node!.prev = self.tail;
      self.tail!.next = node;
      self.tail = node;
    }
  }
  func display() {
    var temp: Node? = self.head;
    if (temp == nil) {
      print("\nEmpty linked list\n");
      return;
    }
    print("\n Head to Tail Nodes :  ");
    while (temp != nil) {
      print(temp!.data ,terminator:"  ");
      temp = temp!.next;
    }
    temp = self.tail;
    print("\n Tail to Head Nodes :  ");
    while (temp != nil) {
      print(temp!.data ,terminator:"  ");
      temp = temp!.prev;
    }
  }
  func reverse(_ list: Node? ) -> Node? {
    var temp: Node? = list;
    var back: Node? = nil;
    var front: Node? = nil; 
    while (temp != nil) {
      front = temp;
      temp = temp!.next;
      front!.prev = temp;
      front!.next = back;
      back = front;
    }
    return front;
  }
  func arrange() {
    if (self.head == nil || self.head!.next == nil) {
      return;
    }
    var last: Node? = self.tail;
    var current = self.head;
    var auxiliary: Node?  = nil;
    var temp: Node?  = nil;
    var back: Node?  = nil;
    let new_head: Node? = last;
    while (current != nil && last != nil) {
      if (current === last) {
        current!.next = nil;
        current!.prev = back;
        self.tail = current;
        break;
      } else
      if (current!.next === last) {
        current!.next = nil;
        current!.prev = last;
        last!.next = current;
        last!.prev = back;
        self.tail = current;
        break;
      }
      auxiliary = current!.next;
      temp = last!.prev;
      current!.prev = last;
      last!.next = current;
      current!.next = temp;
      last!.prev = back;
      back = current;
      current = auxiliary;
      last = temp;
    }
    self.tail = new_head;
    self.head = self.reverse(new_head);
  }
}
func main() {
  let obj: LinkedList = 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);
  obj.insert(9);
  print("\nBefore Reversal spiral ");
  obj.display();
  obj.arrange();
  print("\nAfter Reversal spiral ");
  obj.display();
}
main();

Output

Before Reversal spiral 
 Head to Tail Nodes : 
  1  2  3  4  5  6  7  8  9
 Tail to Head Nodes : 
  9  8  7  6  5  4  3  2  1
After Reversal spiral 
 Head to Tail Nodes : 
  5  4  6  3  7  2  8  1  9
 Tail to Head Nodes : 
  9  1  8  2  7  3  6  4  5


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