0% found this document useful (0 votes)
20 views8 pages

DSA Lab 7 D1 - v4

The document outlines a lab assignment for DSA Lab 7 at IIT Dharwad, focusing on stack operations and related algorithms. It includes tasks such as solving the Readers' Writers' problem, evaluating postfix expressions, converting infix to prefix expressions, implementing the Tower of Hanoi, and designing a call center queue system. Each task provides specific coding requirements and expected outputs for students to follow.

Uploaded by

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

DSA Lab 7 D1 - v4

The document outlines a lab assignment for DSA Lab 7 at IIT Dharwad, focusing on stack operations and related algorithms. It includes tasks such as solving the Readers' Writers' problem, evaluating postfix expressions, converting infix to prefix expressions, implementing the Tower of Hanoi, and designing a call center queue system. Each task provides specific coding requirements and expected outputs for students to follow.

Uploaded by

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

DSA Lab – 7

Stack

Batch – D1

(February, 2024)

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING


INDIAN INSTITUTE OF TECHNOLOGY DHARWAD
Feb, 2024
General Instructions
Before you start programming:

Open the terminal. Using suitable commands -

1. Create a directory named “roll number” in the current directory.

2. Create another directory named “LAB number” within the roll number directory.

3. Within the “LAB_number” directory place your files. Follow the naming convention as
“RollNumber_PracticeQuestionNumber.c” (for example 23200100_p1.c) for practice
questions. Follow the naming convention as “RollNumber_TestQuestionNumber.c” (for
example 23200100 test1.c) for test questions.

After you complete programming and before leaving the lab:

1. Ensure your assigned TA has marked your attendance.

2. Ensure your assigned TA has checked your practice question and test question solutions
and you are evaluated for the same.

3. Ensure to upload your folder on Moodle after evaluation.


1. Readers’ Writers’ problem [5 points]
Two parallel processes are running where one program (writer) is writing data
into the buffer and the other program (reader) is reading data from the buffer.
The data is erased from the buffer as soon as the reader reads it and the
reader is not allowed to read the data when the buffer is empty.

A read operation is denoted by r and write operation is denoted by w

Given a sequence of Reads and Writes, output whether the sequence of


operations is valid or invalid using Stack.

There is no bound on the length of the sequence. So use a linked list to


implement the Stack. At the end of the sequence everything that is written by
the writer should be read by the reader (so the buffer should be empty).

Int Check (char *expression)


{
//Your code
// Return 1 if the expression is valid and 0 if it is invalid
}

void main()
{ //Input the sequence of operations o
//Use the Check() function to verify that the sequence is valid or not
}

Expected Output:

Enter the sequence : wwrwrr


The sequence is valid

Enter the expression : wrwrrw


The sequence is invalid
(Explanation : Because the 3rd read happens when the buffer is
empty)

Enter the expression : wrw


The sequence is invalid
(Explanation : Buffer is not empty at the end)
2. Evaluate Postfix expression [5 points]
Given a postfix arithematic expression, evaluate the expression using Stack.
The maximum length of the postfix expression is 100 and the expression
contains single digit numbers and the arithematic operators / * + -

float Evaluate(char s[100])


{
//Your Code Here
// Return the resulting answer
}
void main()
{ //Input the Arithematic expression s

//Call the Evaluate(s) function and display the output.

Expected Output:

Enter the sequence : 12+31-/


The result is 1.5000

3. Infix to Prefix [ 8 points to convert infix to postfix + 2 points to get


the infix expression ]
Given an infix arithematic expression, compute the equivalent prefix
expression.
The maximum length of the infix expression is 100 and the expression
contains single digit numbers and the arithematic operators :
/ * + - and brackets
(Assume standard order of preference for these arithematic operators)

Strategy: (Also explained for an example below)


Given an infix expression, first reverse the expression
Compute the postfix expression for the reversed expression
Finally reverse the postfix expression obtained in the previous step
to get the result
void InfixToPostfix ( char infix[100], char postfix[100])
{
//Code to convert Infix expression to Postfix expression
}

void InfixToPrefix ( char infix[100], char prefix[100])


{
//Reverse the infix expression
//Use InfixToPostfix() to obtain the postfix expression of the reversed infix expression
//Reverse the postfix expression obtained in the previous step to get the prefix
expression and store the result in prefix[]
}

void main()
{ //Scan the input infix expression
//Use the function InfixToPrefix() to convert the input expression to prefix expression
}

Expected Outcome:

Enter the infix expression : (1+2)/(3-1)


The result is /+12-31

Explanation: Given expression is (1+2)/(3-1)

* First reverse it to get (1-3)/(2+1)


* Find the postfix expression for the reversed string 13-21+/
* Reverse the postfix expression to get the prefix expression /+12-31

4. Tower of Hanoi [5 points]

5. There are 3 towers and n disks of different sizes sorted in ascending order of
size from top to bottom on one rod, the smallest at the top. All the disks in one
tower should be moved onto another tower such that the disks are stacked in the
same way as in original tower.
Constraints:
Only one disk can be moved at a time
A disk is moved off the top of one tower onto the another tower
A disk can only be placed on top of a larger disk

Print the stack contents at each step.

You are free to use either array based stack or a linked list based stack; define
the parameters for the moveDisk() function appropriately depending on your
choice.

void moveDisk(define the parameters appropriately)


{ //Recursive function to move n disks from stack S to stack D
using T as a temporary stack }

void main()
{ //Scan the number of disks (n)
//Compute the way to transfer n disks from source to destination
}

Expected Output:

Enter the number of disks: 3


Strategy:
Initial stack contents: S = [ 1 2 3 ] T = [] D = []
Next step : S = [2 3] T = [] D = [1]
Next step : S = [3] T = [2] D = [1]
Next step : S = [3] T = [1 2] D = []
Next step : S = [] T = [1 2] D = [3]
Next step : S = [1] T = [2] D = [3]
Next step : S = [1] T = [] D = [2 3]
Next step : S = [] T = [] D = [1 2 3]

5. Call center Queue [5 points]

Design a call center queue system as follows: When a new customer


calls, (s)he is put the waiting queue. The call center employees
keep attending the customer requests based on the queue. The system
should also allow the customer to check their position in queue at
any point of time.

Use the Queue data structure to implement this and use an array to
implement the queue. Assume that the maximum number of customers to
be served in a day is 100.
int InsertCustomer(int id, int *queue, int front, int rear)
{ // Insert the customer id into the queue
// Return the position of the new customer in the queue
}

int ServeCustomer(int id, int *queue, int front, int rear)


{ // Pick the customer from the queue who is to be served
// Return the id of the customer being served
}

int CheckStatus(int id, int *queue, int front, int rear)


{ //Return the current position of the customer in the queue }

void main()
{ //Design a menu driven system to implement the banking token
system }

Expected output :

1. Add a Customer to the queue


2. Serve a Customer
3. Check Customer queue status
4. Exit
Enter your choice: 1
Enter customer id: 123
Customer inserted. The current position of the customer in the queue
is 0.

1. Add a Customer to the queue


2. Serve a Customer
3. Check Customer queue status
4. Exit
Enter your choice: 1
Enter customer id: 456
Customer inserted. The current position of the customer in the queue
is 1.

1. Add a Customer to the queue


2. Serve a Customer
3. Check Customer queue status
4. Exit
Enter your choice: 2

The customer to be served is 123.

1. Add a Customer to the queue


2. Serve a Customer
3. Check Customer queue status
4. Exit
Enter your choice: 3
Enter customer id: 456
Current position of customer 456 in the queue is 0.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy