- Implementing a Queue in Java using Arrays and Linked Lists 24 Dec 2013. Following on from my previous post on implementing a stack in Java, I now wish to discuss the as important queue data-structure. Similar in kind to the restrictions placed upon the stack implementation, the queue only allows mutation via two methods.
- The C Program is written for implementation of STACK using Array. C Program to Implement Structure with Pointers. Implementation of Queue using Array in C.
- Write A Program To Implement Queue
- Write A Program To Implement Queue Adt Using Array In Java
- Write A Java Program To Implement Circular Queue Adt Using An Array
The problem is opposite of this post. We are given a Queue data structure that supports standard operations like enqueue() and dequeue(). Ba pass full movie free download mp4 for mobile phones. We need to implement a Stack data structure using only instances of Queue and queue operations allowed on the instances.
C Program To Implement Queue using Array. Learn How To Implement of Queue using Array in C Programming. This Program For Queue in Data Structures is based on Static Arrays. The Queue C Program can be either executed through Arrays or Linked Lists. There following Java code shows how to implement a queue without using any extra data structures in Java. We can implement a queue by using an array. LeetCode – Implement Queue using Stacks (Java) Design a Data Structure with Insert, Delete and GetMostFrequent of O(1) Implement a Stack Using an Array in Java.
A stack can be implemented using two queues. Let stack to be implemented be ‘s’ and queues used to implement be ‘q1’ and ‘q2’. Stack ‘s’ can be implemented in two ways:
https://newrealestate920.weebly.com/full-stamina-cheat-san-andreas-pc.html. Method 1 (By making push operation costly)
This method makes sure that newly entered element is always at the front of ‘q1’, so that pop operation just dequeues from ‘q1’. ‘q2’ is used to put every new element at front of ‘q1’.
This method makes sure that newly entered element is always at the front of ‘q1’, so that pop operation just dequeues from ‘q1’. ‘q2’ is used to put every new element at front of ‘q1’.
- push(s, x) operation’s step are described below:
- Enqueue x to q2
- One by one dequeue everything from q1 and enqueue to q2.
- Swap the names of q1 and q2
- pop(s) operation’s function are described below:
- Dequeue an item from q1 and return it.
Below is the implementation of the above approach:
C++
two queue */ // Two inbuilt queues // elements Stack() curr_size = 0; { q2.push(x); // Push all the remaining while (!q1.empty()) { q1.pop(); queue< int > q = q1; q2 = q; { // if no elements are there in q1 return ; curr_size--; { return -1; } int size() return curr_size; }; // Driver code { s.push(1); s.push(3); cout << 'current size: ' << s.size() cout << s.top() << endl; cout << s.top() << endl; cout << s.top() << endl; cout << 'current size: ' << s.size() return 0; // This code is contributed by Chhavi |
Java
two queue */ // Two inbuilt queues static Queue<Integer> q1 = new LinkedList<Integer>(); static Queue<Integer> q2 = new LinkedList<Integer>(); // To maintain current number of static int curr_size; Stack() curr_size = 0 ; { q2.add(x); // Push all the remaining while (!q1.isEmpty()) { q1.remove(); Queue<Integer> q = q1; q2 = q; { // if no elements are there in q1 return ; curr_size--; { return - 1 ; } static int size() return curr_size; } // driver code { s.push( 1 ); s.push( 3 ); System.out.println( 'current size: ' + s.size()); s.pop(); s.pop(); } // This code is contributed by Prerna |
Python3
# two queue self .q1 = Queue() # of elements self .curr_size + = 1 # Push x first in empty q2 # elements in q1 to q2. self .q2.put( self .q1.queue[ 0 ]) self .q = self .q1 self .q2 = self .q def pop( self ): # if no elements are there in q1 return self .curr_size - = 1 def top( self ): return - 1 return self .curr_size # Driver Code s = Stack() s.push( 2 ) print (s.top()) print (s.top()) print (s.top()) print ( 'current size: ' , s.size()) # This code is contributed by PranchalK |
C#
Write A Program To Implement Queue
two queue */ using System.Collections; class GfG { public class Stack { public Queue q1 = new Queue(); // elements { } public void push( int x) curr_size++; // Push x first in empty q2 // elements in q1 to q2. q2.Enqueue(q1.Peek()); } // swap the names of two queues q1 = q2; } public void pop() if (q1.Count 0) q1.Dequeue(); } public int top() if (q1.Count 0) return ( int )q1.Peek(); { } public static void Main(String[] args) Stack s = new Stack(); s.push(2); Console.WriteLine( 'current size: ' + s.size()); s.pop(); s.pop(); Console.WriteLine( 'current size: ' + s.size()); } // This code is contributed by Arnab Kundu |
Output :
Method 2 (By making pop operation costly)
In push operation, the new element is always enqueued to q1. In pop() operation, if q2 is empty then all the elements except the last, are moved to q2. Finally the last element is dequeued from q1 and returned. https://adinde.hatenablog.com/entry/2020/11/22/143538.
In push operation, the new element is always enqueued to q1. In pop() operation, if q2 is empty then all the elements except the last, are moved to q2. Finally the last element is dequeued from q1 and returned. https://adinde.hatenablog.com/entry/2020/11/22/143538.
- push(s, x) operation:
- Enqueue x to q1 (assuming size of q1 is unlimited).
- pop(s) operation:
- One by one dequeue everything except the last element from q1 and enqueue to q2.
- Dequeue the last item of q1, the dequeued item is result, store it.
- Swap the names of q1 and q2
- Return the item stored in step 2.
C++
using two queue */ using namespace std; class Stack { int curr_size; public : { } void pop() if (q1.empty()) // push others in q2. q2.push(q1.front()); } // Pop the only left element q1.pop(); queue< int > q = q1; q2 = q; { curr_size++; { return -1; while (q1.size() != 1) { q1.pop(); int temp = q1.front(); // to empty the auxiliary queue after q1.pop(); // push last element to q2 queue< int > q = q1; q2 = q; } int size() return curr_size; }; // Driver code { s.push(1); s.push(3); << endl; s.pop(); s.pop(); cout << 'current size: ' << s.size() return 0; // This code is contributed by Chhavi |
Java
using two queue */ Queue<Integer> q1 = new LinkedList<>(), q2 = new LinkedList<>(); { } void remove() if (q1.isEmpty()) // push others in q2. q2.add(q1.peek()); } // Pop the only left element q1.remove(); Queue<Integer> q = q1; q2 = q; { curr_size++; { return - 1 ; while (q1.size() != 1 ) { q1.remove(); int temp = q1.peek(); // to empty the auxiliary queue after q1.remove(); // push last element to q2 Queue<Integer> q = q1; q2 = q; } int size() return curr_size; public static void main(String[] args) Stack s = new Stack(); s.add( 2 ); s.add( 4 ); System.out.println( 'current size: ' + s.size()); s.remove(); s.remove(); System.out.println( 'current size: ' + s.size()); } // This code is contributed by Princi Singh |
C#
using System.Collections; { { public Queue q2 = new Queue(); public void Push( int x) => q1.Enqueue(x); //move all elements from q1 to q2 except the rear of q1. //swap q1 and q2 public int Pop() if (q1.Count 0) while (q1.Count > 1) q2.Enqueue(q1.Dequeue()); int res = ( int )q1.Dequeue(); q1 = q2; return res; { return -1; { } q2.Enqueue(res); q1 = q2; return res; }; { s.Push(1); Console.WriteLine( 'Size of Stack: ' + s.Size() + 'tTop : ' + s.Top()); Console.WriteLine( 'Size of Stack: ' + s.Size() + 'tTop : ' + s.Top()); Console.WriteLine( 'Size of Stack: ' + s.Size() + 'tTop : ' + s.Top()); s.Pop(); Console.WriteLine( 'Size of Stack: ' + s.Size() + 'tTop : ' + s.Top()); Console.WriteLine( 'Size of Stack: ' + s.Size() + 'tTop : ' + s.Top()); s.Push(5); Console.WriteLine( 'Size of Stack: ' + s.Size() + 'tTop : ' + s.Top()); } //Size of Stack: 2 Top : 7 //Size of Stack: 2 Top : 7 //Size of Stack: 2 Top : 5 |
Output :
References:
Implement Stack using Two Queues
Implement Stack using Two Queues
This article is compiled by Sumit Jain and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
Improved By : prerna saini, andrew1234, PranchalKatiyar, spn12, princi singh, more
Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends.In previous post we had discussed introduction of deque. Now in this post we see how we implement deque Using circular array.
Operations on Deque:
Mainly the following four basic operations are performed on queue:
insetFront(): Adds an item at the front of Deque.
insertRear(): Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteRear(): Deletes an item from rear of Deque.
Operations on Deque:
Mainly the following four basic operations are performed on queue:
insetFront(): Adds an item at the front of Deque.
insertRear(): Adds an item at the rear of Deque.
deleteFront(): Deletes an item from front of Deque.
deleteRear(): Deletes an item from rear of Deque.
In addition to above operations, following operations are also supported
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
getFront(): Gets the front item from queue.
getRear(): Gets the last item from queue.
isEmpty(): Checks whether Deque is empty or not.
isFull(): Checks whether Deque is full or not.
Circular array implementation deque
For implementing deque, we need to keep track of two indices, front and rear. We enqueue(push) an item at the rear or the front end of qedue and dequeue(pop) an item from both rear and front end.
Working
1. Create an empty array ‘arr’ of size ‘n’
initialize front = -1 , rear = 0
Inserting First element in deque, at either front or rear will lead to the same result.
After insert Front Points = 0 and Rear points = 0
Insert Elements at Rear end
For implementing deque, we need to keep track of two indices, front and rear. We enqueue(push) an item at the rear or the front end of qedue and dequeue(pop) an item from both rear and front end.
Working
1. Create an empty array ‘arr’ of size ‘n’
initialize front = -1 , rear = 0
Inserting First element in deque, at either front or rear will lead to the same result.
After insert Front Points = 0 and Rear points = 0
Insert Elements at Rear end
Insert Elements at Front end
Delete Element From Rear end Condition zero serial key generator.
Delete Element From Front end
Disk drill pro download for mac. Below is the implementation of above idea.
C++
// array using namespace std; // Maximum size of array or Dequeue class Deque int arr[MAX]; int rear; public : { rear = 0; } // Operations on Deque: void insertrear( int key); void deleterear(); bool isEmpty(); int getRear(); bool Deque::isFull() return ((front 0 && rear size-1)|| } // Checks whether Deque is empty or not. { } // Inserts an element at front { if (isFull()) cout << 'Overflown' << endl; } // If queue is initially empty { rear = 0; else if (front 0) front = front-1; // insert current element into Deque } // function to inset element at rear end void Deque ::insertrear( int key) if (isFull()) cout << ' Overflown ' << endl; } // If queue is initially empty { rear = 0; else if (rear size-1) else arr[rear] = key ; void Deque ::deletefront() // check whether Deque is empty or not { return ; if (front rear) front = -1; } // back to initial position front = 0; else // increment front by '1' to remove current front = front+1; void Deque::deleterear() if (isEmpty()) cout << ' Underflown' << endl ; } // Deque has only one element { rear = -1; else if (rear 0) else } // Returns front element of Deque { if (isEmpty()) cout << ' Underflown' << endl; } } // function return rear element of Deque { if (isEmpty() || rear < 0) cout << ' Underflown' << endl; } } // Driver program to test above function { cout << 'Insert element at rear end : 5 n' ; dq.insertrear(10); cout << 'get rear element ' << ' ' cout << 'After delete rear element new rear' dq.insertfront(15); cout << 'get front element ' << ' ' << 'front become ' << dq.getFront() << endl; } |
Java
// Java implementation of De-queue using circular class Deque static final int MAX = 100 ; int front; int size; public Deque( int size) arr = new int [MAX]; rear = 0 ; } /*// Operations on Deque: void insertrear(int key); void deleterear(); bool isEmpty(); int getRear();*/ // Checks whether Deque is full or not. { front rear+ 1 ); boolean isEmpty () return (front - 1 ); void insertfront( int key) // check whether Deque if full or not { return ; if (front - 1 ) front = 0 ; } // front is at first position of queue front = size - 1 ; else // decrement front end by '1' arr[front] = key ; // of Deque. { { return ; if (front - 1 ) front = 0 ; } // rear is at last position of queue rear = 0 ; // increment rear end by '1' rear = rear+ 1 ; // insert current element into Deque } // Deletes element at front end of Deque { if (isEmpty()) System.out.println( 'Queue Underflown' ); } // Deque has only one element { rear = - 1 ; else if (front size - 1 ) // front value from Deque } // Delete element at rear end of Deque { { return ; if (front rear) front = - 1 ; } rear = size- 1 ; rear = rear- 1 ; int getFront() // check whether Deque is empty or not { return - 1 ; return arr[front]; int getRear() // check whether Deque is empty or not { return - 1 ; return arr[rear]; public static void main(String[] args) System.out.println( 'Insert element at rear end : 5 ' ); System.out.println( 'insert element at rear end : 10 ' ); System.out.println( 'get rear element : ' + dq.getRear()); dq.deleterear(); System.out.println( 'After delete rear element new rear become : ' + System.out.println( 'inserting element at front end' ); System.out.println( 'get front element: ' +dq.getFront()); dq.deletefront(); System.out.println( 'After delete front element new front become : ' + } |
Output:
Time Complexity: Time complexity of all operations like insertfront(), insertlast(), deletefront(), deletelast()is O(1).
Download picture viewer for mac. In mext post we will discuss deque implementation using Doubly linked list.
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.