C++ Help
Create a node class/struct.
Create a queue class/struct.
Members:
Node - a node that tracks the front of the queue.
Node - a node that tracks the end of the queue.
Count - indicates how many items are on the queue.
Methods:
En-queue
- Accepts a number and adds to the end of the queue.
De-queue
- Returns a number from the front of the queue.
- If the queueis empty, emit an error indicating the queueis
empty.
IsEmpty
- Returns a boolean indicating if the queue is empty.
#include <iostream>
using namespace std;
class Node //class that represent the node
{
public:
int data;
//data part of node
Node* next; //this is link part pointing to next node
};
class queue
{
private:
Node* front;//node indicating front of queue
Node* end;
//node indicating end of queue
int count; //count indicating no.of elements in queue
public:
queue()
//constructor of queue class intializing front,end to null and
count to zero as the queue is empty intially
{
front = NULL;
end = NULL;
count = 0;
}
void Enqueue(int element)//this method is used to add elements to
queue
{
Node* tmp = new
Node();
// Creating a new node
to add the element in it
tmp->data =
element;
tmp->next = NULL;
if ( isEmpty() ) //checking if
the queue is empty
{
front
= end = tmp;//add the first elemnt and since it is the one and only
element it is both front and end
}
else {
end->next = tmp;//add this element at end
end =
tmp;
//make this as end
}
count++;//increment count as
the element is added
}
int Dequeue()//this method removes the element from front of the
queue
{
if ( isEmpty() )//checks if queue is
empty
{
cout<<"Queue is Empty";
return -1;
}
int num = front->data;
Node* tmp = front;
front = front->next;// Move the front pointer to next node
count--;//decrement count
as a number is deleted from queue
return num;
}
bool isEmpty()//function to check wether the queue is
empty
{
if (count==0)//if count is zero it
returns true indicating queue is empty else returns false
return true;
else
return false;
}
};
int main()
{
queue q;//create an object to queue class
int ch;
int d;
do
{
cout<<"\nEnter your choice\n1:Add an element to queue\n2:delete an element from queue\n3:exit\n";
cin>>ch;
switch(ch)//basing on given chice switch to the cases
{
case 1:
//if choice is 1 ask for the number and call Enqueue
function
cout<<"enter the number you want to add to queue:";
int n;
cin>>n;
q.Enqueue(n);
cout<<"element added to queue\n";
break;
case 2:
//if choice is 2 call deque function and store the return value in
d
d=q.Dequeue();
if(d==-1)
cout<<"queue is empty";
else
cout<<"the element removed from queue is:",d;
break;
case 3:
break;
default :
cout<<"invalid choice, please choose correct number\n";
}
}while(ch!=3);
//this loop ends when ch==3
return 0;
}
Sample
Output:
Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
1
enter the number you want to add to queue:20
element added to queue
Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
1
enter the number you want to add to queue:45
element added to queue
Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
1
enter the number you want to add to queue:89
element added to queue
Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
2
the element removed from queue is:20
Enter your choice
1:Add an element to queue
2:delete an element from queue
3:exit
3
C++ Help Create a node class/struct. Create a queue class/struct. Members: Node - a node that...
Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...
Create a CircularArrayQueue<E> implementation of the Queue<E> interface defined in class. Hints: You will need two index variables to keep track of the start and the end of the queue. Consider the queue to be empty when the front/back index pointers are equal (which means that the 'back' pointer will always be pointing at the next place to add an item). Be careful of index increments and decrements; make sure you perform the operations necessary to make the pointers loop...
use
intellij idea
main java
Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: Queue Node<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return...
CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...
Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...
Step 3 Develop the following class: Class Name: ImprovedQueue<T> Access Modifier: public Implements: QueueInterface<T> Instance variables Name: front Access modifier: private Data type: QueueNode<T> Constructors: Name: ImprovedQueue Access modifier: public Parameters: none (default constructor) Task: sets the value of front to null Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Task: returns true if the front is equal to null; otherwise return false Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Task: makes...
//This is the implementation file queue.cpp. //This is the implementation of the template class Queue. //The interface for the template class Queue is in the header file queue.h. #include <iostream> #include <cstdlib> #include <cstddef> #include "queue.h" using std::cout; namespace QueueSavitch { //Uses cstddef: template<class T> Queue<T>::Queue( ) : front(NULL), back(NULL) { //Intentionally empty. } //Uses cstddef: template<class T> bool Queue<T>::isEmpty( ) const { return (back == NULL);//front == NULL would also work } //Uses cstddef: template<class T> void Queue<T>::add(T item)...
Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...
Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...
Write a method for the Queue class in the queue.java program (Listing 4.4) that displays the contents of the queue. Note that this does not mean simply displaying the contents of the underlying array. You should show the queue contents from the first item inserted to the last, without indicating to the viewer whether the sequence is broken by wrapping around the end of the array. Be careful that one item and no items display properly, no matter where front...