Basic operations on Doubly linked list

Basic operations on Doubly linked list


package Linked_List;

class Node2
{
	int data;
	Node2 prev = null;
	Node2 next = null;
	
	Node2()
	{}
	Node2(int a)
	{
		this.data = a;
	}
}

public class DoublyLinkedList
{
	Node2 head = null;
	
	public void push(int a)
	{
		Node2 node = new Node2(a);
		
		if(head == null)
			head = node;
		else
		{
			Node2 temp = this.head;
			for(temp=this.head; temp.next!=null; temp=temp.next)
			{}
			temp.next = node;
			node.prev = temp;
		}
	}
	
	public void print()
	{
		Node2 temp = this.head;
		for(temp=this.head; temp!=null; temp=temp.next)
			System.out.print(temp.data + " <--> ");
		System.out.println("null");
	}
}

Comments

Popular posts from this blog

4.Sort an array of 0's, 1's & 2's.

1. Reverse

3. Height of Binary tree