Consider the following Queue class that uses linked list to construct a Queue. c
ID: 3841045 • Letter: C
Question
Consider the following Queue class that uses linked list
to construct a Queue.
class Node;
typedef Node* NodePtr;
class Node
{
public:
int number;
NodePtr next;
Node(){number=0; next=NULL;}
Node(int n){number=n; next=NULL;}
};
//----------------------------------------
class Queue
{
private:
NodePtr top;
int currSize;
const int fullSize;
public:
//initalizez the Queue to an empty Queue (fixed size of 5)
Queue()
{
fullSize= 5;
currSize= 0;
top= NULL;
}
//initializes the Queue to an empty Queue and specific maximum size
Queue(int maxSize)
{
fullSize= maxSize;
currSize= 0;
top = NULL;
}
int enqueue(int item); //places an item into the Queue. Returns -1 if operation is not successful
int dequeue(int& item); //removes an item from the Queue. Returns -1 if operation is not successsful
bool empty(); //checks if the Queue is empty
bool full(); //checks if the Queue is full
};
Explanation / Answer
I have completed the methods for you:):)check it i f u need more than that plz comment it
--------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
typedef Node* NodePtr;
class Node
{
public:
int number;
NodePtr next;
Node()
{
number=0;
next=NULL;
}
Node(int n,Node d)
{
number=n;
next=d;
}
public void setNumber(int n)
{
number=n;
}
public void setNext()
{
next=d;
}
public void getNumber()
{
return number;
}
}
public void getNext()
{
return next;
}
//----------------------------------------
class Queue
{
private:
NodePtr top;
Nodeptr bottom;
int currSize;
const int fullSize;
public:
//initalizez the Queue to an empty Queue (fixed size of 5)
Queue()
{
fullSize= 5;
currSize= 0;
top= NULL;
}
//initializes the Queue to an empty Queue and specific maximum size
Queue(int maxSize)
{
fullSize= maxSize;
currSize= 0;
top = NULL;
}
int enqueue(int item)//places an item into the Queue. Returns -1 if operation is not successful
{
Node nptr=new Node(number,null);
if(bootom==null)
{
top=nptr;
bottom=nptr;
}
else
{
bottom.setNext(nptr);
bottom=bottom.getNext();
}
fullSize++;
}
int dequeue(int& item)//removes an item from the Queue. Returns -1 if operation is not successsful
{
if(Empty())
throw new NoSuchElementException("Underflow Exception");
Node ptr = top;
top = ptr.getNext();
if (top == null)
{
bottom = null;
size-- ;
return ptr.getNumber();
}
else
{
return -1;
}
}
bool empty()//checks if the Queue is empty
{
return top=null;
}
bool full()//checks if the Queue is full
{
if (Empty() )
throw new NoSuchElementException("Underflow Exception");
return top.getNumber();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.