Queue implementation using linked list in Java
Queue is another famous data structure which allows operating from two ends(front and rear) through head and tail. It works in First In First Out(FIFO) fashion.
Queue data structure is like two ended room which allows entry(enqueue) from one end called as rear and exit (dequeue) from another end called a front...
So to remember , queue always supports insertion from rear-end / tail and deletion called as dequeue happens only from fron end / head.
Let's see how can we implement simple Queue data structure in Java using a single linked list.
We need follow the Cell class we have used in the Stack example in the previous post.
Queue main class :
package com.krish.queue; import com.krish.datastructures.common.Cell; public class QueueMain { Cell head; Cell tail; public QueueMain() { head = null; tail = null; } public void enqueue(Object obj) { Cell newCell = new Cell(obj, null); if (head == null && tail == null) head = newCell; else tail.next = newCell; tail = newCell; System.out.println("Enqueud element:"+obj); printQueue(); } public Cell front() { return head; } public Cell rear() { return tail; } public void dequeue() { if (head == null && tail == null) { System.out.println("Q is empty"); } else { System.out.println("Dequeued element:"+head.getVal()); if (head.next == null) { tail = null; } head = head.next; } printQueue(); } public int getsize() { int size = 0; if(!(head ==null && tail == null)){ size = 1; for(Cell n = head; n.next != null; n = n.next) size = size+1; return size; } return size; } public void printQueue() { if (head == null && tail == null) { System.out.println("Q is empty"); } else { System.out.println("Q Elememts:"); Cell current = head; System.out.println("Head"); while (current != null) { System.out.println("->" + current.getVal()); current = current.next; } System.out.println("<--tail br=""> System.out.println("Size of the Q:"+getsize()); } } public static void main(String[] args) { QueueMain queue = new QueueMain(); queue.enqueue(23); queue.enqueue(43); queue.enqueue(143); queue.enqueue(321); queue.dequeue(); queue.dequeue(); } } --tail>
Output :
Enqueud element:23
Q Elememts:
Head
->23
<--tail br="">Size of the Q:1
Enqueud element:43
Q Elememts:
Head
->23
->43
<--tail br="">Size of the Q:2
Enqueud element:143
Q Elememts:
Head
->23
->43
->143
<--tail br="">Size of the Q:3
Enqueud element:321
Q Elememts:
Head
->23
->43
->143
->321
<--tail br="">Size of the Q:4
Dequeued element:23
Q Elememts:
Head
->43
->143
->321
<--tail br="">Size of the Q:3
Dequeued element:43
Q Elememts:
Head
->143
->321
<--tail br="">Size of the Q:2--tail>--tail>--tail>--tail>--tail>--tail>
References : [1][2][3][4]
Q Elememts:
Head
->23
<--tail br="">Size of the Q:1
Enqueud element:43
Q Elememts:
Head
->23
->43
<--tail br="">Size of the Q:2
Enqueud element:143
Q Elememts:
Head
->23
->43
->143
<--tail br="">Size of the Q:3
Enqueud element:321
Q Elememts:
Head
->23
->43
->143
->321
<--tail br="">Size of the Q:4
Dequeued element:23
Q Elememts:
Head
->43
->143
->321
<--tail br="">Size of the Q:3
Dequeued element:43
Q Elememts:
Head
->143
->321
<--tail br="">Size of the Q:2--tail>--tail>--tail>--tail>--tail>--tail>
References : [1][2][3][4]
0 comments to "Queue implementation using linked list in Java"
Popular Posts
-
The best solution to know about these init levels is to understand the " man init " command output on Unix. There are basically 8...
-
How to Unlock BSNL 3G data card to use it with Airtel and Vodafone Model no : LW272 ? How to unlock BSNL 3G data card( Model no : LW272 )us...
-
How to transfer bike registration from one State to other (Karnataka to Andhra)?? Most of us having two wheelers purchased and registered in...
-
ఓం శ్రీ స్వామియే శరణం ఆయ్యప్ప!! Related posts : Trip to Sabarimala - Part 1 Trip to Sabarimala - Part 2 Ayappa Deeksha required things...
-
Following are some of interesting blogs I found till now ...please comment to add your blog here. Blogs in English : http://nitawriter.word...
Popular posts
- Airtel and vodafone GPRS settings for pocket PC phones
- Andhra 2 America
- Ayyappa Deeksha required things
- Blogs I watch !
- Captions for your bike
- DB2 FAQs
- Deepavali Vs The Goddes of sleep
- ETV - Dhee D2 D3
- Evolution of smoking in India Women
- How to make credit card payments?
- init 0, init 1, init 2 ..
- Java-J2EE interview preparation
- mCheck Application jar or jad download
- My SQL FAQs
- My Travelogues
- Old is blod - New is italic
- Online pay methids for credit cards
- Oracle FAQs
- Pilgrimages
- Smoking in Indian Women
- Technology Vs Humans
- Twitter feeds for all Telugu stars on single page.
- Unix best practices
- Unix FAQs
Post a Comment
Whoever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs, u can at least use Name/URL option which doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")