Implement a queue using an array in java and c++.
Example 1:
enQueue(1): true: isEmpty(): false: front() : 1: Size() : 1
front(): 1: isEmpty(): false: deQueue() : 1: Size() : 0
Underflow!! Underflow!! front(): -2147483648: isEmpty(): true: deQueue() : -2147483648: Size() : 0
enQueue(1): true: isEmpty(): false: front() : 1: Size() : 1
enQueue(2): true: isEmpty(): false: front() : 1: Size() : 2
enQueue(3): true: isEmpty(): false: front() : 1: Size() : 3
Overflow!! enQueue(4): false: isEmpty(): false: front() : 1: Size() : 3
Approach:
Java
public class QueueImp {public static void main(String[] args) {// create queue with sizeQueueArray s = new QueueArray(3);// push 1 elementSystem.out.println("enQueue(1): " + s.enQueue(1)+ ": isEmpty(): " + s.isEmpty() + ": front() : " + s.front()+ ": Size() : " + s.size());// pop elementSystem.out.println("front(): " + s.front()+ ": isEmpty(): " + s.isEmpty() + ": deQueue() : " + s.deQueue()+ ": Size() : " + s.size());// pop elementSystem.out.println("front(): " + s.front()+ ": isEmpty(): " + s.isEmpty() + ": deQueue() : " + s.deQueue()+ ": Size() : " + s.size());// push another elementSystem.out.println("enQueue(1): " + s.enQueue(1)+ ": isEmpty(): " + s.isEmpty() + ": front() : " + s.front()+ ": Size() : " + s.size());// push another elementSystem.out.println("enQueue(2): " + s.enQueue(2)+ ": isEmpty(): " + s.isEmpty() + ": front() : " + s.front()+ ": Size() : " + s.size());// push another elementSystem.out.println("enQueue(3): " + s.enQueue(3)+ ": isEmpty(): " + s.isEmpty() + ": front() : " + s.front()+ ": Size() : " + s.size());// push another elementSystem.out.println("enQueue(4): " + s.enQueue(4)+ ": isEmpty(): " + s.isEmpty() + ": front() : " + s.front()+ ": Size() : " + s.size());}}class QueueArray {int front = -1;int rear = -1;// initialize the default capacityprivate int DEFAULT_CAPACITY = 10;// arrayprivate int[] array;// constructor with capacitypublic QueueArray(int maxSize) {this.DEFAULT_CAPACITY = maxSize;this.array = new int[DEFAULT_CAPACITY];}// constructor with default capacitypublic QueueArray() {this.array = new int[DEFAULT_CAPACITY];}// add value in queueboolean enQueue(int number) {// if not full, then add else Overflowif (!isFull()) {front++;array[front] = number;return true;} else {System.out.print("Overflow!! ");return false;}}// get front and front remove element from queueint deQueue() {if (!this.isEmpty()) {return array[++rear];} else {// resetfront = -1;rear = -1;System.out.print("Underflow!! ");return Integer.MIN_VALUE;}}// only get front element from queueint front() {if (!this.isEmpty()) {if (rear == -1) {return array[0];}return array[rear];} else {System.out.print("Underflow!! ");return Integer.MIN_VALUE;}}public int size() {return front - rear;}boolean isEmpty() {return front == -1 || front == rear;}boolean isFull() {return front == DEFAULT_CAPACITY - 1;}}
C++
#include <bits/stdc++.h>using namespace std;class Queue {int front1 = -1;int rear1 = -1;// initialize the default capacityint DEFAULT_CAPACITY = 10;// arrayint array[10];public:// constructor with capacityQueue(int maxSize) {this->DEFAULT_CAPACITY = maxSize;array[DEFAULT_CAPACITY];}// constructor with default capacityQueue() {array[DEFAULT_CAPACITY];}// add value in queuevoid enQueue(int number) {// if not full, then add else Overflowif (!isFull()) {front1++;array[front1] = number;} else {cout<<"Overflow!! ";}}// get front and front remove element from queuevoid deQueue() {if (!this->isEmpty()) {{++rear1;}} else {// resetfront1 = -1;rear1 = -1;cout<<"Underflow!! ";}}// only get front element from queueint front() {if (!this->isEmpty()) {if (rear1 == -1) {return array[0];}return array[rear1];} else {cout<<"Underflow!! ";return INT_MIN;}}int size() {return front1 - rear1;}bool isEmpty() {return front1 == -1 || front1== rear1;}bool isFull() {return front1 == DEFAULT_CAPACITY - 1;}};int main(){// create queue with sizeQueue s(3);// push 1 elementcout<<"enQueue(1): ";s.enQueue(1);cout<<": isEmpty(): "<<s.isEmpty();cout<<": front() : "<<s.front();cout<< ": Size() : "<<s.size()<<"\n";// pop elementcout<<"front(): "<<s.front();cout<<": isEmpty(): "<<s.isEmpty();cout<<": deQueue() : ";s.deQueue();cout<<": Size() : "<<s.size()<<"\n";// pop elementcout<<"front(): "<<s.front();cout<<": isEmpty(): "<<s.isEmpty();cout<<": deQueue() : ";s.deQueue();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<"enQueue(1): ";s.enQueue(1);cout<<": isEmpty(): "<<s.isEmpty();cout<<": front() : "<<s.front();cout<<": Size() : "<<s.size()<<"\n";// push another elementcout<<"enQueue(2): ";s.enQueue(2);cout<<": isEmpty(): "<<s.isEmpty();cout<<": front() : "<<s.front();cout<<": Size() : " + s.size()<<"\n";// push another elementcout<<"enQueue(3): ";s.enQueue(3);cout<<": isEmpty(): "<<s.isEmpty();cout<<": front() : " <<s.front();cout<<": Size() : " + s.size()<<"\n";// push another elementcout<<"enQueue(4): ";s.enQueue(4);cout<<": isEmpty(): "<<s.isEmpty();cout<<": front() : "<<s.front();cout<<": Size() : "<<s.size()<<"\n";return 0;}
No comments:
Post a Comment