#Include Using Namespace Struct Int
#Include Using Namespace Struct Int
ARYAN CHUGH.
DSA ASSESMENT 2
Q1 In a theme park, the Roller-Coaster ride is started only when a good number of riders line up in
the counter (say 20 members). When the ride proceeds with these 20 members, a new set of riders
will line up in the counter. This keeps continuing. Implement the above scenario of lining up and
processing using arrays with Queue ADT
In the following code I have used linked list to enqueue and dequeue and also in high level I have
added priority queue so that VIPs get the priority and I have used switch for each function.
Space complexity of algo - I have used pointers as arguments in every function to save space.
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
node *prev;
};
// enqueue function using linked list
void enqueue(node **front, node **rear, int data)
{
node *newnode = new node;
newnode->data = data;
newnode->next = NULL;
if (*front == NULL)
{
*front = newnode;
*rear = newnode;
}
else
{
(*rear)->next = newnode;
*rear = newnode;
}
}
// dequeue function using linked list
void dequeue(node **front, node **rear)
{
if (*front == NULL)
{
cout << "Queue is empty" << endl;
}
else
{
node *temp = *front;
*front = (*front)->next;
delete temp;
}
}
// display function using linked list
void display(node *front)
{
if (front == NULL)
{
cout << "Queue is empty" << endl;
}
else
{
while (front != NULL)
{
cout << front->data << endl;
front = front->next;
}
cout << endl;
}
}
//priority queue using linked list
void priority_queue(node **front, node **rear, int data)
{
node *newnode = new node;
newnode->data = data;
newnode->next = NULL;
if (*front == NULL)
{
*front = newnode;
*rear = newnode;
}
else
{
node *temp = *front;
while (temp->data < data)
{
temp = temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
}
}
int pd;
int isfull()
{
if (q.front == (q.rear + 1) % n)
return 1;
else
return 0;
}
int isempty()
{
if (q.front == -1)
return 1;
else
return 0;
}
int dequeue()
{
int item;
item = q.que[q.front];
if (q.front == q.rear)
q.front = q.rear = -1;
else
q.front = (q.front + 1) % n;
pd = item;
return item;
}
void display()
{
if (isempty())
{
printf("\nCircular buffer empty!!!");
return;
}
int i;
i = q.front;
while (i != q.rear)
{
printf("\n%d", q.que[i]);
i = (i + 1) % n;
}
printf("\n%d", q.que[i]);
}
void disp()
{
printf("\nFront index:%d", q.front);
printf("\nRear index:%d", q.rear);
}
void newdisp()
{
printf("\nLast burnt data: %d", pd);
}
int main()
{
int ch, data;
q.front = -1;
q.rear = 0;
printf("\n\tMAIN MENU\n1. Write data onto the circular buffer");
printf("\n2. Burn data onto the DVD");
printf("\n3. Display data currently on the circular buffer(Data that has
not been burnt yet)");
printf("\n4. Display last burnt data");
printf("\n5. Display front and rear indices");
printf("\n6. Exit");
do
{
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
if (isfull())
printf("\nCircular Buffer is Full! Please Burn the Data in the
DVD");
else
{
printf("\nEnter data:\n");
scanf("%d", &data);
enqueue(data);
}
break;
case 2:
if (isempty())
printf("\nCircular Buffer is Empty! Please Enter New Data to
Read");
else
printf("\nThe Data Burned on the DVD is %d", dequeue());
break;
case 3:
display();
break;
case 4:
newdisp();
break;
case 5:
disp();
break;
case 6:
printf("\nExiting process!");
break;
default:
printf("\nInvalid choice! Please try again!");
}
} while (ch != 6);
return 0;
}
Q3 a) There is a garage where the access road can accommodate any number of trucks at one time.
The garage is built in such a way that only the last truck entered can be moved out. Each of the
trucks is identified by a positive integer (a truck_id). Implement dynamically to handle truck moves,
allowing for the following commands: i) On_road (truck_id); ii) Enter_garage (truck_ id); iii)
Exit_garage (truck_id); iv) Show_trucks (garage or road);
#include <stdio.h>
#include <cstdlib>
#define size 20
int road[size];
int i, j, top = -1;
struct queue
{
int garage[size], time[size];
int front, rear;
} q;
void roadf()
{
int i;
printf("\nRoad:");
for (i = 0; i <= top; i++)
printf("\t%d", road[i]);
}
int full()
{
if (top == size - 1)
return 1;
else
return 0;
}
int empty()
{
if (top == -1)
return 1;
else
return 0;
}
void show()
{
int i;
printf("\nGarage:");
for (i = q.front; i <= q.rear; i++)
printf("\t%d", q.garage[i]);
}