Implement a queue using a linked list 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 QueueImpLinklist {public static void main(String[] args) {// create queue with sizeQueueLink s = new QueueLink(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 QueueLink {int front = -1;int rear = -1;// initialize the default capacityprivate int DEFAULT_CAPACITY = 10;// linked listprivate ListNode list = null;// constructor with capacitypublic QueueLink(int maxSize) {this.DEFAULT_CAPACITY = maxSize;}// constructor with default capacitypublic QueueLink() {}// add value in queueboolean enQueue(int val) {// if not full, then add else Overflowif (!isFull()) {// if list is emptyif (front == -1) {front++;list = new ListNode(val);} else {front++;list.next = new ListNode(val);}return true;} else {System.out.print("Overflow!! ");return false;}}// get front and front remove element from queueint deQueue() {if (!this.isEmpty()) {rear++;return searchGivenNode(list, rear);} else {// resetlist = null;front = -1;rear = -1;System.out.print("Underflow!! ");return Integer.MIN_VALUE;}}// return nth element from link listprivate static int searchGivenNode(ListNode head, int value) {int nth = 0;while (head != null) {if (nth == value) {return head.val;}nth++;head = head.next;}return -1;}// only get front element from queueint front() {if (!this.isEmpty()) {if (rear == -1) {return list.val;}return searchGivenNode(list, rear);} else {// resetlist = null;front = -1;rear = -1;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;}}class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}
C++
#include <bits/stdc++.h>using namespace std;class ListNode {public:int val;ListNode *next;ListNode() {}ListNode(int val) {this->val = val;this->next=NULL;}ListNode(int val, ListNode *next) {this->val = val;this->next = next;}};class QueueLink {int front1 = -1;int rear1 = -1;// initialize the default capacityint DEFAULT_CAPACITY = 10;// linked listListNode* list = NULL;public:// constructor with capacityQueueLink(int maxSize) {this->DEFAULT_CAPACITY = maxSize;}// constructor with default capacityQueueLink() {}// add value in queuevoid enQueue(int val) {// if not full, then add else Overflowif (!isFull()) {// if list is emptyif (front1 == -1) {front1++;list = new ListNode(val);} else {front1++;list->next = new ListNode(val);}} else {cout<<"Overflow!! ";}}// get front and front remove element from queuevoid deQueue() {if (!this->isEmpty()) {rear1++;searchGivenNode(list, rear1);} else {// resetlist = NULL;front1 = -1;rear1 = -1;cout<<"Underflow!! ";// return INT_MIN;}}// return nth element from link listint searchGivenNode(ListNode *head, int value) {int nth = 0;while (head != NULL) {if (nth == value) {return head->val;}nth++;head = head->next;}return -1;}// only get front element from queueint front() {if (!this->isEmpty()) {if (rear1 == -1) {return list->val;}return searchGivenNode(list, rear1);} else {// resetlist = NULL;front1 = -1;rear1 = -1;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 sizeQueueLink 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