Implement Queue using Linked List - The Coding Shala
Home >> Data Structure >> Implement queue using linked list
Other Posts You May Like
In this post, we will learn how to implement Queue using Linked List and will write a Java Program for the same.
Implement Queue using Linked List
We will implement Queue using Linked List. The basic operation of the queue like push/offer and remove/poll method will implement here.
Java Program:
class QueueNode { int data; QueueNode next; QueueNode(int a) { data = a; next = null; } } class MyQueue { QueueNode front, rear; // This function should add an item at // rear void push(int a) { QueueNode newNode = new QueueNode(a); if(front == null || rear == null) { front = newNode; rear = front; } else { rear.next = newNode; rear = rear.next; } } // This function should remove front // item from queue and should return // the removed item. int pop() { if(front == null) return -1; int res = front.data; front = front.next; return res; } }
- Queue Data Structure
- Circular Queue Data Structure
- Implement Queue using Array
- Implement Stack using Array
- Implement Stack using Linked List
Comments
Post a Comment