Check if given key is exist in doubly linked list

Find node in doubly linked list

Here given code implementation process.

//C Program
//Find node in doubly linked list
#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;
    }
  }

}
void find_node(int key)
{
  if(head==NULL)
  {
    printf("\nEmpty Linked List");
    return;
  }
  int status = 0;
  struct Node * front=head,*rear=tail;
  while(front!=NULL && rear != NULL)
  {
    if(front->data == key || rear->data == key)
    {
      status = 1;
      break;
    }
    
    if(front==rear || front->next == rear)
    {

      //means gievn key not exist
      break;
    }
    //visit to new node
    front = front -> next;
    rear = rear ->prev;
  }
  if(status==1)
  {
    printf("\n Node %d is exist\n",key );
  }
  else
  {
    printf("\n Node %d are not exist\n",key );
  }
  
 
}
int main() {

  //Insert element of linked list
  insert(8);
  insert(2);
  insert(23);
  insert(-4);
  insert(7);
  insert(3);
  insert(9);
  //display all node

  display();
  find_node(-4);
  find_node(11);
  find_node(3);
  return 0;
}

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
/*
 C++ Program
 Find node in 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 = 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;
    }
  }
  void find_node(int key) {
    if (this->head == NULL) {
      cout << "\nEmpty Linked List";
      return;
    }
    int status = 0;
    Node *front = this->head, *rear = this->tail;
    while (front != NULL && rear != NULL) {
      if (front->data == key || rear->data == key) {
        status = 1;
        break;
      }
      if (front == rear || front->next == rear) {
        break;
      }
      front = front->next;
      rear = rear->prev;
    }
    if (status == 1) {
      cout << "\n Node " << key << " is exist\n";
    } else {
      cout << "\n Node " << key << " are not exist\n";
    }
  }
};
int main() {
  LinkedList obj ;
  obj.insert(8);
  obj.insert(2);
  obj.insert(23);
  obj.insert(-4);
  obj.insert(7);
  obj.insert(3);
  obj.insert(9);
  obj.display();
  obj.find_node(-4);
  obj.find_node(11);
  obj.find_node(3);
  return 0;
}

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
/*
  Java Program
  Find node in 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 = 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 void find_node(int key) {

    if (head == null) {

      System.out.print("\nEmpty Linked List");
      return;
    }
    int status = 0;
    Node front = head, rear = tail;

    while (front != null && rear != null) {

      if (front.data == key || rear.data == key) {
        status = 1;
        break;
      }

      if (front == rear || front.next == rear) {
        break;
      }
      front = front.next;
      rear = rear.prev;
    }

    if (status == 1) {

      System.out.print("\n Node " + key + " is exist\n");
    } else {

      System.out.print("\n Node " + key + " are not exist\n");
    }
  }



  public static void main(String[] args) {
    LinkedList obj = new LinkedList();
    //Insert element of linked list
    obj.insert(8);
    obj.insert(2);
    obj.insert(23);
    obj.insert(-4);
    obj.insert(7);
    obj.insert(3);
    obj.insert(9);
    //display all node

    obj.display();
    obj.find_node(-4);
    obj.find_node(11);
    obj.find_node(3);
  }


}

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
/*
  C# Program
  Find node in 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 = 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 void find_node(int key) {

		if (head == null) {

			Console.Write("\nEmpty Linked List");
			return;
		}
		int status = 0;
		Node front = head, rear = tail;

		while (front != null && rear != null) {

			if (front.data == key || rear.data == key) {
				status = 1;
				break;
			}

			if (front == rear || front.next == rear) {
				break;
			}
			front = front.next;
			rear = rear.prev;
		}

		if (status == 1) {

			Console.Write("\n Node " + key + " is exist\n");
		} else {

			Console.Write("\n Node " + key + " are not exist\n");
		}
	}



	public static void Main(String[] args) {
		LinkedList obj = new LinkedList();
		//Insert element of linked list
		obj.insert(8);
		obj.insert(2);
		obj.insert(23);
		obj.insert(-4);
		obj.insert(7);
		obj.insert(3);
		obj.insert(9);
		//display all node

		obj.display();
		obj.find_node(-4);
		obj.find_node(11);
		obj.find_node(3);
	}


}

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
# Python 3 Program
# Find node in doubly linked list

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 find_node(self, key) :
    if (self.head == None) :
      print("\nEmpty Linked List")
      return
    
    status = 0
    front = self.head
    rear = self.tail
    while (front != None and rear != None) :
      if (front.data == key or rear.data == key) :
        status = 1
        break
      
      if (front == rear or front.next == rear) :
        break
      
      front = front.next
      rear = rear.prev
    
    if (status == 1) :
      print("\n Node ", key ," is exist")
    else :
      print("\n Node ", key ," are not exist")
    
  

def main() :
  obj = LinkedList()
  obj.insert(8)
  obj.insert(2)
  obj.insert(23)
  obj.insert(-4)
  obj.insert(7)
  obj.insert(3)
  obj.insert(9)
  obj.display()
  obj.find_node(-4)
  obj.find_node(11)
  obj.find_node(3)

if __name__ == "__main__":
  main()

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
# Ruby Program
# Find node in doubly linked list

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 find_node(key) 
		if (@head == nil) 
			print("\nEmpty Linked List")
			return
		end
		status = 0
		front = @head
		rear = @tail
		while (front != nil and rear != nil) 
			if (front.data == key or rear.data == key) 
				status = 1
				break
			end
			if (front == rear or front.next == rear) 
				break
			end
			front = front.next
			rear = rear.prev
		end
		if (status == 1) 
			print("\n Node ", key ," is exist\n")
		else 
			print("\n Node ", key ," are not exist\n")
		end
	end
end
def main() 
	obj = LinkedList.new()
	obj.insert(8)
	obj.insert(2)
	obj.insert(23)
	obj.insert(-4)
	obj.insert(7)
	obj.insert(3)
	obj.insert(9)
	obj.display()
	obj.find_node(-4)
	obj.find_node(11)
	obj.find_node(3)
end


main()

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
<?php
/*
 Php Program
 Find node in doubly linked list
*/

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 find_node($key) {
    if ($this->head == null) {
      echo("\nEmpty Linked List");
      return;
    }
    $status = 0;
    $front = $this->head;
    $rear = $this->tail;
    while ($front != null && $rear != null) {
      if ($front->data == $key || $rear->data == $key) {
        $status = 1;
        break;
      }
      if ($front == $rear || $front->next == $rear) {
        break;
      }
      $front = $front->next;
      $rear = $rear->prev;
    }
    if ($status == 1) {
      echo("\n Node ". $key ." is exist\n");
    } else {
      echo("\n Node ". $key ." are not exist\n");
    }
  }
}

function main() {
  $obj = new LinkedList();
  $obj->insert(8);
  $obj->insert(2);
  $obj->insert(23);
  $obj->insert(-4);
  $obj->insert(7);
  $obj->insert(3);
  $obj->insert(9);
  $obj->display();
  $obj->find_node(-4);
  $obj->find_node(11);
  $obj->find_node(3);
}
main();

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
/*
 Node Js Program
 Find node in doubly linked list
*/

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;
		}
	}
	find_node(key) {
		if (this.head == null) {
			process.stdout.write("\nEmpty Linked List");
			return;
		}
		var status = 0;
		var front = this.head;
		var rear = this.tail;
		while (front != null && rear != null) {
			if (front.data == key || rear.data == key) {
				status = 1;
				break;
			}
			if (front == rear || front.next == rear) {
				break;
			}
			front = front.next;
			rear = rear.prev;
		}
		if (status == 1) {
			process.stdout.write("\n Node " + key + " is exist\n");
		} else {
			process.stdout.write("\n Node " + key + " are not exist\n");
		}
	}
}

function main() {
	var obj = new LinkedList();
	obj.insert(8);
	obj.insert(2);
	obj.insert(23);
	obj.insert(-4);
	obj.insert(7);
	obj.insert(3);
	obj.insert(9);
	obj.display();
	obj.find_node(-4);
	obj.find_node(11);
	obj.find_node(3);
}

main();

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist
/*
 Swift 4 Program
 Find node in doubly linked list
*/

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");
      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 find_node(_ key: Int) {
    if (self.head == nil) {
      print("\nEmpty Linked List");
      return;
    }
    var status: Int = 0;
    var front: Node? = self.head;
    var rear: Node? = self.tail;
    while (front != nil && rear != nil) {
      if (front!.data == key || rear!.data == key) {
        status = 1;
        break;
      }
      if (front === rear || front!.next === rear) {
        break;
      }
      front = front!.next;
      rear = rear!.prev;
    }
    if (status == 1) {
      print("\n Node ", key ," is exist");
    } else {
      print("\n Node ", key ," are not exist");
    }
  }
}
func main() {
  let obj: LinkedList = LinkedList();
  obj.insert(8);
  obj.insert(2);
  obj.insert(23);
  obj.insert(-4);
  obj.insert(7);
  obj.insert(3);
  obj.insert(9);
  obj.display();
  obj.find_node(-4);
  obj.find_node(11);
  obj.find_node(3);
}
main();

Output

 Head to Tail Nodes : 
  8  2 23 -4  7  3  9
 Tail to Head Nodes : 
  9  3  7 -4 23  2  8
 Node -4 is exist

 Node 11 are not exist

 Node 3 is exist


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