Skip to content

Commit 22167f7

Browse files
author
Navjinder Virdee
committed
Implemented Doubly LinkedList.
1 parent 774e1fe commit 22167f7

File tree

4 files changed

+415
-0
lines changed

4 files changed

+415
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//Program to Implement DoubleLinkedList.
2+
3+
public class DoublyLinkedList{
4+
static class Node{
5+
int key; //data value
6+
Node next; //points to next element in the list.
7+
Node prev; //points to previous element in the list.
8+
9+
public Node(int data){
10+
this.key=data;
11+
this.next=null;
12+
this.prev=null;
13+
}
14+
}
15+
16+
static Node head; //head of the list i.e first element
17+
static Node tail; //tail of the list i.e last element
18+
static int size; //number of elements in the list.
19+
20+
public DoublyLinkedList(){
21+
head=null;
22+
tail=null;
23+
size=0;
24+
}
25+
26+
//function to add elements at the front of the list.
27+
public static void pushFront(int number){
28+
System.out.println("Inserting data at front : " + number);
29+
Node node = new Node(number);
30+
if(head==null){
31+
head=node;
32+
tail=node;
33+
size++;
34+
return;
35+
}
36+
37+
node.next=head;
38+
head.prev=node;
39+
head=node;
40+
size++;
41+
}
42+
43+
//function to add elements at the back of the list.
44+
public static void pushBack(int number){
45+
System.out.println("Inserting data at back : " + number);
46+
Node node =new Node(number);
47+
if(head==null){
48+
head=node;
49+
tail=node;
50+
size++;
51+
return;
52+
}
53+
54+
Node temp=head;
55+
while(temp.next!=null){
56+
temp=temp.next;
57+
}
58+
temp.next=node;
59+
node.prev=temp;
60+
tail=node;
61+
size++;
62+
}
63+
64+
//function to remove element from the front of list.
65+
public static void popFront(){
66+
System.out.println("PopFront Operation.");
67+
if(head==null){
68+
System.out.println("Empty List.");
69+
return;
70+
}
71+
if(head.next==null){
72+
head=null;
73+
tail=null;
74+
size--;
75+
return;
76+
}
77+
head=head.next;
78+
head.prev=null;
79+
size--;
80+
}
81+
82+
//function to remove elements from the back of the list.
83+
public static void popBack(){
84+
System.out.println("PopBack Operation.");
85+
if(head==null){
86+
System.out.println("Empty List.");
87+
return;
88+
}
89+
90+
if(head.next==null){
91+
head=null;
92+
tail=null;
93+
size--;
94+
return;
95+
}
96+
97+
Node temp=head;
98+
while(temp.next.next!=null){
99+
temp=temp.next;
100+
}
101+
temp.next.prev=null;
102+
temp.next=null;
103+
tail=temp;
104+
size--;
105+
}
106+
107+
//function to display the list.
108+
public static void displayF(){
109+
System.out.print("List : ");
110+
if(head==null){
111+
System.out.println("Empty List.");
112+
return;
113+
}
114+
Node temp=head;
115+
while(temp!=null){
116+
System.out.print(temp.key + " ");
117+
temp=temp.next;
118+
}
119+
System.out.println();
120+
}
121+
122+
public static void displayB(){
123+
System.out.print("Reverse List : ");
124+
if(head==null && tail==null){
125+
System.out.println("Empty List.");
126+
return;
127+
}
128+
129+
Node temp=tail;
130+
while(temp!=null){
131+
System.out.print(temp.key + " ");
132+
temp=temp.prev;
133+
}
134+
System.out.println();
135+
}
136+
137+
public static void main(String [] args){
138+
DoublyLinkedList list = new DoublyLinkedList();
139+
list.pushFront(10);
140+
list.pushBack(12);
141+
list.pushFront(8);
142+
list.pushBack(14);
143+
list.displayF();
144+
list.displayB();
145+
146+
list.popFront();
147+
list.displayF();
148+
list.displayB();
149+
list.popBack();
150+
list.displayF();
151+
list.displayB();
152+
153+
list.popFront();
154+
list.displayB();
155+
list.popFront();
156+
list.displayF();
157+
}
158+
}
159+
160+
161+
162+
163+
164+
165+
166+
167+
168+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//Program to implement Doubly Linked List.
2+
3+
public class DoublyLinkedList{
4+
Node head; //head of the list. i.e first element.
5+
Node tail; //tail of the list. i.e last element.
6+
int size; //number of elements in the list.
7+
8+
public DoublyLinkedList(){
9+
head=null;
10+
tail=null;
11+
size=0;
12+
}
13+
14+
//function to add elements at the front of the list.
15+
public void pushFront(int data){
16+
System.out.println("Inserting data at front : " + data);
17+
Node node = new Node(data);
18+
if(head==null && tail==null){
19+
head=node;
20+
tail=node;
21+
size++;
22+
return;
23+
}
24+
25+
node.next=head;
26+
head.prev=node;
27+
head=node;
28+
size++;
29+
}
30+
31+
//function to add elements at the back of the list.
32+
public void pushBack(int data){
33+
System.out.println("Inserting data at back : " + data);
34+
Node node = new Node(data);
35+
if(tail==null && head==null){
36+
head=node;
37+
tail=node;
38+
size++;
39+
return;
40+
}
41+
42+
tail.next=node;
43+
node.prev=tail;
44+
tail=node;
45+
size++;
46+
}
47+
48+
//function to remove elements from the front of the list.
49+
public void popFront() throws Exception{
50+
System.out.println("PopFront Operation.");
51+
if(head==null && tail==null){
52+
throw new Exception("List is empty!");
53+
}
54+
if(head==tail){
55+
head=null;
56+
tail=null;
57+
size--;
58+
return;
59+
}
60+
61+
head.next.prev=null;
62+
head=head.next;
63+
size--;
64+
}
65+
66+
//function to remove elements from the back of the list.
67+
public void popBack() throws Exception{
68+
System.out.println("PopBack Operation.");
69+
if(head==null && tail==null){
70+
throw new Exception("List is empty!");
71+
}
72+
if(head==tail){
73+
head=null;
74+
tail=null;
75+
size--;
76+
return;
77+
}
78+
tail.prev.next=null;
79+
tail=tail.prev;
80+
size--;
81+
}
82+
83+
//function to get the first element of the list.
84+
public int topFront() throws Exception{
85+
if(head==null){
86+
throw new Exception("List is empty!");
87+
}
88+
return head.key;
89+
}
90+
91+
//function to get last element of the list.
92+
public int topBack() throws Exception{
93+
if(tail==null){
94+
throw new Exception("List is empty!");
95+
}
96+
return tail.key;
97+
}
98+
99+
//function to find specific element in the list.
100+
public boolean Find(int data){
101+
Node temp=head;
102+
while(temp!=null){
103+
if(temp.key==data){
104+
return true;
105+
}
106+
temp=temp.next;
107+
}
108+
return false;
109+
}
110+
111+
//function to get size of the list.
112+
public int size(){
113+
return size;
114+
}
115+
116+
//function to display the list.
117+
public void forwardDisplay() throws Exception{
118+
System.out.print("List : ");
119+
Node temp = head;
120+
if(temp==null){
121+
throw new Exception("List is empty!");
122+
}
123+
while(temp!=null){
124+
System.out.print(temp.key + " ");
125+
temp=temp.next;
126+
}
127+
System.out.println();
128+
}
129+
130+
//function to display the list in reverse direction.
131+
public void reverseDisplay() throws Exception{
132+
System.out.print("Reverse List : ");
133+
Node temp = tail;
134+
if(temp==null){
135+
throw new Exception("List is empty!");
136+
}
137+
while(temp!=null){
138+
System.out.print(temp.key + " ");
139+
temp=temp.prev;
140+
}
141+
System.out.println();
142+
}
143+
144+
//function to remove specific element from the list.
145+
public void remove(int data) throws Exception{
146+
System.out.println("Remove data : " + data);
147+
Node temp = head;
148+
Node prevNode = null;
149+
if(temp==null){
150+
throw new Exception("List is empty!");
151+
}
152+
size--;
153+
154+
if(temp==tail){
155+
if(temp.key==data){
156+
head=null;
157+
tail=null;
158+
return;
159+
}
160+
else{
161+
throw new Exception("Data not found");
162+
}
163+
}
164+
if(head.key==data){
165+
head=head.next;
166+
head.prev=null;
167+
return;
168+
}
169+
while(temp!=null && temp.key!=data){
170+
prevNode=temp;
171+
temp=temp.next;
172+
}
173+
prevNode.next=prevNode.next.next;
174+
prevNode.next.prev=prevNode;
175+
temp=null;
176+
}
177+
}
178+
179+
180+
181+
182+
183+
184+
185+

0 commit comments

Comments
 (0)