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

C++ C++ Design your own dynamic queue (possibly with a linked list) to implement

ID: 655650 • Letter: C

Question

C++

C++

Design your own dynamic queue (possibly with a linked list) to implement this problem:

The manager of a very popular night club, The Gas Company, has asked you to write a program to help him manage the throngs of people who want to enter the club on a busy night. Often there is a queue that wraps around the building! He never knows how many people will be waiting. Because of fire hazards, he cannot allow everybody into the club at once. So he keeps a list and allows the person at the head of the list to enter when there is room. Because he doesn't earn as much money as he thinks is appropriate for the work, he has been known to take a bribe or two for speedier access.

He wants you to create a way to hold the names of people who are waiting to enter the club. The people standing outside are allowed to enter the club - as space permits - on a first-come-first-seved basis. When there is room, the manager will allow one person in at a time. If someone shows up and offers the manager $50, then that person is inserted into the front of the line. (They still have to wait, but they are the new head of the list.)

Your program should have a menu with the following options:

1. Add name to list

2. Allow one person to enter the club

3. Display the list

4. Jump the list

Your program should use a dynamic queue to implement this. All you have to do is keep the person's name. Validate only for not empty. And if it's longer than your max string, then just truncate it. Make sure that you have proper constructors and destructors. Make your input and output professional. Break your code down into functions as necessary. No global variables.

Explanation / Answer

#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node                        //node created
{
    int info;
    struct node *next;
    struct node *prev;
}*start;
class double_llist                               //linked list created
{
    public:
   void create_list(int value);
   void add_begin(int value);
   void allow_person(int value, int position);
   void display_dlist();
   double_llist()
   {
        start = NULL;
   }
};
int main()                                        //main function
{
    int choice,position;
    string name[20];
    double_llist dl;
    while (1)
    {
   cout<<endl<<"----------------------------"<<endl;
   cout<<endl<<"Operations on Doubly linked list"<<endl;
   cout<<endl<<"----------------------------"<<endl;
   cout<<"1.create Name"<<endl;
   cout<<"2.Add at begining"<<endl;
   cout<<"3.Allow person"<<endl;
   cout<<"4.Display"<<endl;
   cout<<"5.Quit"<<endl;
   cout<<"Enter your choice : ";
   cin>>choice;
   switch ( choice )                     //switch case statement
   {
   case 1:
        cout<<"Enter the name: ";
        cin>>name;
        dl.create_list(name);
        cout<<endl;
        break;
   case 2:
        cout<<"Enter the name: ";
        cin>>name;
        dl.add_begin(name);
        cout<<endl;
        break;
   case 3:
        cout<<"Enter the name: ";
        cin>>name;
        cout<<"Insert Element after postion: ";
        cin>>position;
        dl.allow_person(name, position);
        cout<<endl;
        break;
       case 4:
        dl.display_dlist();
        cout<<endl;
        break;
       case 5:
        exit(1);
   default:
        cout<<"Wrong choice"<<endl;
   }
    }
    return 0;
}

// Create Double Link List

void double_llist::create_list(int value)
{
    struct node *s, *temp;
    temp = new(struct node);
    temp->info = value;
    temp->next = NULL;
    if (start == NULL)
    {
   temp->prev = NULL;
   start = temp;
    }
    else
    {
   s = start;
   while (s->next != NULL)
        s = s->next;
   s->next = temp;
   temp->prev = s;
    }
}
// Insertion at the beginning
void double_llist::add_begin(int value)
{
    if (start == NULL)
    {
   cout<<"First Create the list."<<endl;
   return;
    }
    struct node *temp;
    temp = new(struct node);
    temp->prev = NULL;
    temp->info = value;
    temp->next = start;
    start->prev = temp;
    start = temp;
    cout<<"Element Inserted"<<endl;
}
//Allowed person list
void double_llist::allow_person(int value, int pos)
{
    if (start == NULL)
    {
   cout<<"First Create the list."<<endl;
   return 0;
    }
    struct node *tmp, *q;
    int i;
    q = start;
    for (i = 0;i < pos - 1;i++)
    {
   q = q->next;
   if (q == NULL)
   {
        cout<<"There are less than ";
        cout<<pos<<" elements."<<endl;
        return;
   }
    }
    tmp = new(struct node);
    tmp->info = value;
    if (q->next == NULL)
    {
   q->next = tmp;
   tmp->next = NULL;
   tmp->prev = q;
    }
    else
    {
   tmp->next = q->next;
   tmp->next->prev = tmp;
   q->next = tmp;
   tmp->prev = q;
    }
    cout<<"Element Inserted"<<endl;
}
//Display function list
void double_llist::display_dlist()
{
    struct node *q;
    if (start == NULL)
    {
   cout<<"List empty,nothing to display"<<endl;
   return;
    }
    q = start;
    cout<<"The Doubly Link List is :"<<endl;
    while (q != NULL)
    {
   cout<<q->info<<" <-> ";
   q = q->next;
    }
    cout<<"NULL"<<endl;
}