1//PART A: Complete the code below (do not use any standard libraries). 3 //PART
ID: 3592750 • Letter: 1
Question
1//PART A: Complete the code below (do not use any standard libraries). 3 //PART B: For each public method, provide a big-oh bound on the worst case 4 //run-time for that method in terms of the number of items 'n' contained in 5 //the data structure at the time of the method call. Justify your bound in each case 7class queueLL 9 private: //put what you need here.. 2 public: queueLL () //your code here queueLL () /your code here //add item to back of queue enqueue (int x) //your code here //remove and return first item from queue dequeue () //your code hereExplanation / Answer
struct queueLLNode{
int key;
queueLLNode *next;
};
class queueLL{
private:
queueLLNode *front;
queueLLNode *rear;
public:
//Constructor for queueLL class
queueLL(){
front=NULL;
rear=NULL;
}
//Destructor
~queueLL(){
// traverse the queue and deallocate memory for all queue elements
while(front != NULL)
{
queueLLNode * temporary = front;
front = front->next;
delete temporary;
}
// finally make rear also null
rear = NULL;
}
//add item to the back of the queue
void enqueue( int x ){
//get space/memory for a new queueLLNode
queueLLNode *temporary =new queueLLNode;
//if value returned is false
if(temporary==NULL){
// in this case memory overflow
cout<<" Memory Overflow"<<endl;
return;
}
temporary ->key = x;
temporary ->next = NULL;
//if inserted node is a first node or intially the queue is empty
if(front==NULL){
front = temporary;
rear = temporary;
}
else{
rear ->next = temporary;
rear = temporary;
}
}
queueLLNode * dequeue(){
//if the queue is empty return a message
if (front == NULL){
cout<<"Queue is Empty"<<endl;
return NULL;
}
queueLLNode *temporary =new queueLLNode;
temporary ->key = front -> key;
temporary ->next = NULL;
cout << temporary->key << endl;
//if only single element is there in the queue
if(front==rear){
front = NULL;
rear = NULL;
}
else{
front = front->next;
}
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.