0% found this document useful (0 votes)
21 views15 pages

#Include Using Namespace Struct Int

Uploaded by

seinkein9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

#Include Using Namespace Struct Int

Uploaded by

seinkein9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

21BCE3118

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

COMPLETED LEVEL – HIGH

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;
}
}

// sum of all elements in queue


int sum(node *front)
{
int sum = 0;
while (front != NULL)
{
sum += front->data;
front = front->next;
}
return sum;
}
int main()
{
node *front = NULL;
node *rear = NULL;
int choice, data;
while (1)
{
cout << "1. Enqueue" << endl;
cout << "2. Dequeue" << endl;
cout << "3. Display" << endl;
cout << "4. Queue for VIPs" << endl;
cout << "5. Total no of ppl" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the data: ";
cin >> data;
enqueue(&front, &rear, data);
break;
case 2:
dequeue(&front, &rear);
break;
case 3:
display(front);
break;
case 4:
cout << "Enter the data: ";
cin >> data;
priority_queue(&front, &rear, data);
break;
case 5:
cout << "Total no of adults and childrens : " << sum(front) <<
endl;
break;
case 6:
exit(0);
default:
cout << "Invalid choice" << endl;
}
}
return 0;
}

Output for low level and high level


Output for Mid level
Q2 When burning a DVD it is essential that the laser beam burning pit s onto the surface is
constantly fed with data, otherwise the DVD fails. Most leading DVD burn applications make use of a
circular buffer to stream data from the hard disk onto the DVD. The first part, the ‘writing process’
fills up a circular buffer with data, then the ‘burning process’ begins to read from the buffer as the
laser beam burns pit s onto the surface of the DVD. If the buffer starts to become empty, the
application should continue filling up the emptied space in the buffer with new data from the disk.
Implement this scenario using Circular Queue.
#include <stdio.h>
#define n 5
struct burn
{
int que[n];
int front;
int rear;
} q;

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;
}

void enqueue(int data)


{
if (q.front == -1)
q.front = q.rear = 0;
else
q.rear = (q.rear + 1) % n;
q.que[q.rear] = data;
}

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]);
}

void in_garage(int id, int t)


{
if (full())
printf("\nGarage is full!");
else
{
top++;
road[top] = id;
q.garage[q.rear] = id;
q.time[q.rear] = t;
q.rear = (q.rear + 1) % size;
}
}

void out_garage(int id, int t)


{
int i;
if (empty())
printf("\nGarage is empty!");
else
{
for (i = q.front; i != q.rear; i = (i + 1) % size)
{
if (q.garage[i] == id)
{
printf("\nTruck %d has been in garage for %d minutes", id, t -
q.time[i]);
q.front = (q.front + 1) % size;
break;
}
}
if (i == q.rear)
printf("\nTruck %d is not in garage!", id);
else
{
for (i = 0; i <= top; i++)
{
if (road[i] == id)
{
road[i] = road[top];
top--;
break;
}
}
}
}
}
int main()
{
int choice, id, t=0;
while (1)
{
printf("\n1. Enter truck");
printf("\n2. Exit truck");
printf("\n3. Display road");
printf("\n4. Exit");
printf("\n\nEnter your choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter truck id:");
scanf("%d", &id);
printf("\nEnter time:");
scanf("%d", &t);
in_garage(id, t);
break;
case 2:
printf("\nEnter truck id:");
scanf("%d", &id);
printf("\nEnter time:");
scanf("%d", &t);
out_garage(id, t);
break;
case 3:
roadf();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!");
}
}
}

You might also like