Insert node at end of circular doubly linked list

Insertion at end of circular doubly linked list

Here given code implementation process.

//C Program
//Insert node at end of circular doubly linked list
#include <stdio.h>

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

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

struct Node *head = NULL, *tail = NULL;
//Insert Node at end of linked list
void add_node(int value) {

  //Create a dynamic node
  struct Node *node = (struct Node *) malloc(sizeof(struct Node));
  if (node == NULL) {
    printf("Memory overflow\n");
    return;

  } else {
    //set data value
    node->next = node;
    node->prev = node;
    node->data = value;

    if (head == NULL) {
      head = node;
      tail = node;
    } else {
      node->next = head;
      node->prev = tail;
      head->prev = node;
      tail->next = node;
      tail = node;
    }
    
  }
}
//display element from head to tail
void head_to_tail() {

  if (head == NULL) {
    printf("Empty linked list");
  } else {
    struct Node *temp = head;
    printf("\nNode Form Front to Rear :\n");
    //Traverse doubly linked list from front to rear
    while (temp != NULL) {
      //print node value
      printf("%d  ", temp->data);

      temp = temp->next;

      if (temp == head) {
        break;
      }


    }
    
  }
}
//display element from tail to head
void tail_to_head() {

  if (tail == NULL) {
    printf("Empty linked list");
  } else {
    struct Node *temp = tail;
    printf("\nNode Form Rear to Front :\n");
    //Traverse doubly linked list from rear to front
    while (temp != NULL) {
      //print node value
      printf("%d  ", temp->data);

      temp = temp->prev;
      if (temp == tail) {
        break;
      }
    }
    
  }
}


int main() {


  //insert element of linked list
  add_node(1);
  add_node(2);
  add_node(3);
  add_node(4);
  add_node(5);
  add_node(6);


  //Display all node
  head_to_tail();
  tail_to_head();

  return 0;
}

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
/*
 C++ Program
 Insert node at end of circular doubly linked list
*/

#include<iostream>

using namespace std;
class Node {
  public:
    int data;
  Node *next;
  Node *prev;
  Node(int value) {
    this->data = value;
    this->next = this;
    this->prev = this;
  }
};
class LinkedList {
  public:
    Node *head;
  Node *tail;
  LinkedList() {
    this->head = NULL;
    this->tail = NULL;
  }
  void add_node(int value) {
    Node *node = new Node(value);
    if (node == NULL) {
      cout << "Memory overflow\n";
      return;
    } else {
      if (this->head == NULL) {
        this->head = node;
      } else {
        node->next = this->head;
        node->prev = this->tail;
        this->head->prev = node;
        this->tail->next = node;
      }
      this->tail = node;
    }
  }
  void head_to_tail() {
    if (this->head == NULL) {
      cout << "Empty linked list";
    } else {
      Node *temp = this->head;
      cout << "\nNode Form Front to Rear :\n";
      while (temp != NULL) {
        cout << temp->data << "  ";
        temp = temp->next;
        if (temp == this->head) {
          break;
        }
      }
    }
  }
  void tail_to_head() {
    if (this->tail == NULL) {
      cout << "Empty linked list";
    } else {
      Node *temp = this->tail;
      cout << "\nNode Form Rear to Front :\n";
      while (temp != NULL) {
        cout << temp->data << "  ";
        temp = temp->prev;
        if (temp == this->tail) {
          break;
        }
      }
    }
  }
};
int main() {
  LinkedList obj;
  obj.add_node(1);
  obj.add_node(2);
  obj.add_node(3);
  obj.add_node(4);
  obj.add_node(5);
  obj.add_node(6);
  obj.head_to_tail();
  obj.tail_to_head();
  return 0;
}

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
/*
  Java Program
  Insert node at end of circular doubly linked list
*/
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 = this;
    this.prev = this;
  }
}
class LinkedList {

  public Node head;
  public Node tail;

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

  if (node == null) {

    System.out.print("Memory overflow\n");
    return;
  } else {

    if (head == null) {
      head = node;
    } else {
      node.next = head;
      node.prev = tail;
      head.prev = node;
      tail.next = node;
    }
    tail = node;
  }
}
void head_to_tail() {

  if (head == null) {

    System.out.print("Empty linked list");
  } else {
    Node temp = head;

    System.out.print("\nNode Form Front to Rear :\n");

    while (temp != null) {

      System.out.print(temp.data+"  ");
      temp = temp.next;

      if (temp == head) {
        break;
      }
    }
  }
}
void tail_to_head() {

  if (tail == null) {

    System.out.print("Empty linked list");
  } else {
    Node temp = tail;

    System.out.print("\nNode Form Rear to Front :\n");

    while (temp != null) {

      System.out.print(temp.data+"  ");
      temp = temp.prev;

      if (temp == tail) {
        break;
      }
    }
  }
}



  public static void main(String[] args) {

    LinkedList obj = new LinkedList();
    obj.add_node(1);
    obj.add_node(2);
    obj.add_node(3);
    obj.add_node(4);
    obj.add_node(5);
    obj.add_node(6);
    obj.head_to_tail();
    obj.tail_to_head();

  }
}

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
/*
  C# Program
  Insert node at end of circular doubly linked list
*/
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 = this;
		this.prev = this;
	}
}
class LinkedList {

	public Node head;
	public Node tail;

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

		if (node == null) {

			Console.Write("Memory overflow\n");
			return;
		} else {

			if (head == null) {
				head = node;
			} else {
				node.next = head;
				node.prev = tail;
				head.prev = node;
				tail.next = node;
			}
			tail = node;
		}
	}
	void head_to_tail() {

		if (head == null) {

			Console.Write("Empty linked list");
		} else {
			Node temp = head;

			Console.Write("\nNode Form Front to Rear :\n");

			while (temp != null) {

				Console.Write(temp.data+"  ");
				temp = temp.next;

				if (temp == head) {
					break;
				}
			}
		}
	}
	void tail_to_head() {

		if (tail == null) {

			Console.Write("Empty linked list");
		} else {
			Node temp = tail;

			Console.Write("\nNode Form Rear to Front :\n");

			while (temp != null) {

				Console.Write(temp.data+"  ");
				temp = temp.prev;

				if (temp == tail) {
					break;
				}
			}
		}
	}



	public static void Main(String[] args) {

		LinkedList obj = new LinkedList();
		obj.add_node(1);
		obj.add_node(2);
		obj.add_node(3);
		obj.add_node(4);
		obj.add_node(5);
		obj.add_node(6);
		obj.head_to_tail();
		obj.tail_to_head();

	}
}

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
# Python 3 Program
# Insert node at end of circular doubly linked list
class Node :

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

class LinkedList :

  def __init__(self) :
    self.head = None
    self.tail = None
  
  def add_node(self, value) :
    node = Node(value)
    if (node == None) :
      print("Memory overflow\n")
      return
    else :
      if (self.head == None) :
        self.head = node
      else :
        node.next = self.head
        node.prev = self.tail
        self.head.prev = node
        self.tail.next = node
      
      self.tail = node
    
  
  def head_to_tail(self) :
    if (self.head == None) :
      print("Empty linked list")
    else :
      temp = self.head
      print("\nNode Form Front to Rear :")
      while (temp != None) :
        print(temp.data ,end="  ")
        temp = temp.next
        if (temp == self.head) :
          break
        
      
    
  
  def tail_to_head(self) :
    if (self.tail == None) :
      print("Empty linked list")
    else :
      temp = self.tail
      print("\nNode Form Rear to Front :")
      while (temp != None) :
        print(temp.data ,end="  ")
        temp = temp.prev
        if (temp == self.tail) :
          break
        
      
    
  

def main() :
  obj = LinkedList()
  obj.add_node(1)
  obj.add_node(2)
  obj.add_node(3)
  obj.add_node(4)
  obj.add_node(5)
  obj.add_node(6)
  obj.head_to_tail()
  obj.tail_to_head()

if __name__ == "__main__":
  main()

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
# Ruby Program
# Insert node at end of circular doubly linked list

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

class LinkedList 
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		@head = nil
		@tail = nil
	end
	def add_node(value) 
		node = Node.new(value)
		if (node == nil) 
			print("Memory overflow\n")
			return
		else 
			if (@head == nil) 
				@head = node
			else 
				node.next = @head
				node.prev = @tail
				@head.prev = node
				@tail.next = node
			end
			@tail = node
		end
	end
	def head_to_tail() 
		if (@head == nil) 
			print("Empty linked list")
		else 
			temp = @head
			print("\nNode Form Front to Rear  :\n")
			while (temp != nil) 
				print(temp.data ,"  ")
				temp = temp.next
				if (temp == @head) 
					break
				end
			end
		end
	end
	def tail_to_head() 
		if (@tail == nil) 
			print("Empty linked list")
		else 
			temp = @tail
			print("\nNode Form Rear to Front  :\n")
			while (temp != nil) 
				print(temp.data ,"  ")
				temp = temp.prev
				if (temp == @tail) 
					break
				end
			end
		end
	end
end
def main() 
	obj = LinkedList.new()
	obj.add_node(1)
	obj.add_node(2)
	obj.add_node(3)
	obj.add_node(4)
	obj.add_node(5)
	obj.add_node(6)
	obj.head_to_tail()
	obj.tail_to_head()
end


main()

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
<?php

/*
 Php Program
 Insert node at end of circular doubly linked list
*/

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

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

  function __construct() {
    $this->head = null;
    $this->tail = null;
  }

  function add_node($value) {
    $node = new Node($value);
    if ($node == null) {
      echo("Memory overflow\n");
      return;
    } else {
      if ($this->head == null) {
        $this->head = $node;
      } else {
        $node->next = $this->head;
        $node->prev = $this->tail;
        $this->head->prev = $node;
        $this->tail->next = $node;
      }
      $this->tail = $node;
    }
  }

  function head_to_tail() {
    if ($this->head == null) {
      echo("Empty linked list");
    } else {
      $temp = $this->head;
      echo("\nNode Form Front to Rear :\n");
      while ($temp != null) {
        echo($temp->data ."  ");
        $temp = $temp->next;
        if ($temp == $this->head) {
          break;
        }
      }
    }
  }

  function tail_to_head() {
    if ($this->tail == null) {
      echo("Empty linked list");
    } else {
      $temp = $this->tail;
      echo("\nNode Form Rear to Front :\n");
      while ($temp != null) {
        echo($temp->data ."  ");
        $temp = $temp->prev;
        if ($temp == $this->tail) {
          break;
        }
      }
    }
  }
}

function main() {
  $obj = new LinkedList();
  $obj->add_node(1);
  $obj->add_node(2);
  $obj->add_node(3);
  $obj->add_node(4);
  $obj->add_node(5);
  $obj->add_node(6);
  $obj->head_to_tail();
  $obj->tail_to_head();
}
main();

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
/*
 Node Js Program
 Insert node at end of circular doubly linked list
*/

class Node {

	constructor(value) {
		this.data = value;
		this.next = this;
		this.prev = this;
	}
}
class LinkedList {
	
	constructor() {
		this.head = null;
		this.tail = null;
	}
	add_node(value) {
		var node = new Node(value);
		if (node == null) {
			process.stdout.write("Memory overflow\n");
			return;
		} else {
			if (this.head == null) {
				this.head = node;
			} else {
				node.next = this.head;
				node.prev = this.tail;
				this.head.prev = node;
				this.tail.next = node;
			}
			this.tail = node;
		}
	}
	head_to_tail() {
		if (this.head == null) {
			process.stdout.write("Empty linked list");
		} else {
			var temp = this.head;
			process.stdout.write("\nNode Form Front to Rear :\n");
			while (temp != null) {
				process.stdout.write(temp.data + "  ");
				temp = temp.next;
				if (temp == this.head) {
					break;
				}
			}
		}
	}
	tail_to_head() {
		if (this.tail == null) {
			process.stdout.write("Empty linked list");
		} else {
			var temp = this.tail;
			process.stdout.write("\nNode Form Rear to Front :\n");
			while (temp != null) {
				process.stdout.write(temp.data + "  ");
				temp = temp.prev;
				if (temp == this.tail) {
					break;
				}
			}
		}
	}
}

function main() {
	var obj = new LinkedList();
	obj.add_node(1);
	obj.add_node(2);
	obj.add_node(3);
	obj.add_node(4);
	obj.add_node(5);
	obj.add_node(6);
	obj.head_to_tail();
	obj.tail_to_head();
}

main();

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  1 
/*
 Swift 4 Program
 Insert node at end of circular doubly linked list
*/

class Node {
  var data: Int;
  var next: Node? ;
  var prev: Node? ;
  init(_ value: Int) {
    self.data = value;
    self.next = self;
    self.prev = self;
  }
}
class LinkedList {
  var head: Node? ;
  var tail: Node? ;
  init() {
    self.head = nil;
    self.tail = nil;
  }
  func add_node(_ value: Int) {
    let node: Node? = Node(value);
    if (node == nil) {
      print("Memory overflow\n");
      return;
    } else {
      if (self.head == nil) {
        self.head = node;
      } else {
        node!.next = self.head;
        node!.prev = self.tail;
        self.head!.prev = node;
        self.tail!.next = node;
      }
      self.tail = node;
    }
  }
  func head_to_tail() {
    if (self.head == nil) {
      print("Empty linked list");
    } else {
      var temp: Node? = self.head;
      print("\nNode Form Front to Rear :");
      while (temp != nil) {
        print(temp!.data ,terminator:"  ");
        temp = temp!.next;
        if (temp === self.head) {
          break;
        }
      }
    }
  }
  func tail_to_head() {
    if (self.tail == nil) {
      print("Empty linked list");
    } else {
      var temp: Node? = self.tail;
      print("\nNode Form Rear to Front :");
      while (temp != nil) {
        print(temp!.data ,terminator:"  ");
        temp = temp!.prev;
        if (temp === self.tail) {
          break;
        }
      }
    }
  }
}
func main() {
  let obj: LinkedList = LinkedList();
  obj.add_node(1);
  obj.add_node(2);
  obj.add_node(3);
  obj.add_node(4);
  obj.add_node(5);
  obj.add_node(6);
  obj.head_to_tail();
  obj.tail_to_head();
}
main();

Output

Node Form Front to Rear :
1  2  3  4  5  6  
Node Form Rear to Front :
6  5  4  3  2  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