C++ For my class named NodeQueue show how to write the following functions. clas
ID: 3823427 • Letter: C
Question
C++
For my class named NodeQueue show how to write the following functions.
class NodeQueue{
public:
NodeQueue(); // instantiate a new queue object with no elements(nodes)
int size() const; // Return size of the current queue
bool empty() const; // return true if queue is empty
bool full() const; //return true if queue is full
private:
Node *m_front; //pointer to first element of the queue
Node *m_back //pointer to last element of the queue
}
These are only some of the functions for what I am working on but any help would be greatly appreciated.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
struct QueueNode
{
int data;
struct QueueNode *next;
};
QueueNode* newNode(int x)
{
QueueNode* q=new QueueNode;
q->data=x;
q->next=NULL;
return q;
}
class NodeQueue
{
private:
QueueNode *front,*rear;
public:
NodeQueue()
{
front=rear=NULL;
}
bool isEmpty()
{
return(!front&&!rear);
}
int size()
{
QueueNode* temp=front;
int c=0;
while(temp!=NULL)
{
temp=temp->next;
c++;
}
return c;
}
void enqueue(int x)
{
if(isEmpty())
{
front=rear=newNode(x);
return;
}
//In likedlist implementation there is no overflow conditions as its dynamically grows
QueueNode* temp=newNode(x);
rear->next=temp;
rear=temp;
}
QueueNode* dequeue()
{
if(isEmpty())
{
cout<<"Underflow ";
return NULL;
}
QueueNode* temp=front;
front=front->next;
if(!front)
rear=NULL;
return temp;
}
};
int main()
{
NodeQueue q;
q.enqueue(10);
q.enqueue(20);
q.dequeue();
q.dequeue();
q.dequeue();
QueueNode* temp=q.dequeue();
if(temp)
cout<<temp->data<<endl;
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
QueueNode* tempe=q.dequeue();
if(tempe)
cout<<tempe->data<<endl;
int s=q.size();
cout << "Queue size :";
cout<<s<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.