Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and code your own FIFO Queue using single liked list to hold a list of in

ID: 3821010 • Letter: D

Question

Design and code your own FIFO Queue using single liked list to hold a list of integers. Use the C++ “struct” structure to create your queue items.

Your program should include the main program to test and demonstrate one function for each Queue operations including:

InitQueue – Initialize FIFO Queue

IsEmptyQueue – Checks for empty Queue

FrontQueue – Returns from item

BackQueue – Returns back item

PushQueue – Adds an item to end of Queue

PopQueue – Delete an item from of Queue

ClearQueue – Delete all items on the Queue

PrintQueue - Display all elements on the Queue

Explanation / Answer

PROGRAM CODE:

#include <iostream>
using namespace std;

struct Queue
{
   int item;
   Queue* next;
}*queue;
int size;

void InitQueue()
{
   queue = NULL;
   size = 0;
}

bool IsEmptyQueue()
{
   return size==0;
}

int FrontQueue()
{
   if(queue != NULL)
       return queue->item;
}

int BackQueue()
{
   Queue *temp = queue;
   while(temp->next!= NULL)
       temp = temp->next;
   return temp->item;
}

void PushQueue(int value)
{
   Queue *node = new Queue;
   node->item = value;
   node->next = NULL;
   if(queue == NULL)
   {
       queue = node;  
   }
   else
   {
       Queue *temp = queue;
       while(temp->next != NULL)
           temp = temp->next;
       temp->next = node;
   }
   size++;
}

void PopQueue()
{

   if(queue == NULL)
       return;
   else if(queue->next == NULL)
   {
       queue = NULL;
       size--;
       return;
   }
   else
   {
       Queue *temp = queue;
       while(temp->next->next != NULL)
           temp = temp->next;
       temp->next = NULL;
       size--;
   }
}

void ClearQueue()
{
   queue = NULL;
   size = 0;
}

void PrintQueue()
{
   Queue *temp = queue;
   while(temp != NULL)
   {
       cout<<temp->item<<" ";
       temp = temp->next;
   }
   cout<<endl;
}
int main() {
   InitQueue();
   cout<<"Is Queue Empty? "<<IsEmptyQueue()<<endl;
   PushQueue(2);
   PushQueue(4);
   PushQueue(6);
   PrintQueue();
   PopQueue();
   PushQueue(8);
   PrintQueue();
   cout<<"Front Item: "<<FrontQueue()<<endl;
   cout<<"Back Item: "<<BackQueue()<<endl;
   ClearQueue();
   cout<<"Queue cleared"<<endl;
   cout<<"Is Queue Empty? "<<IsEmptyQueue()<<endl;
   return 0;
}

OUTPUT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote