Assignment 02 - FA24

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

COMSATS University, Islamabad

Islamabad Campus
Department of Computer Science

Read before Attempt


Assignment No. 2: STACK AND QUEUE
Course code and Title: CSC211, Data Structure
Instructor: Tanveer Ahmed
Assigned Date: October 7,2024 Due Date: October 25,2024
Total Marks: --
Theory CLO-1: Employ linear data structures to solve computing problems.
Lab CLO -4: CLO-4: Implement data structures and algorithms
Instructions:
1. This is an individual assignment. You will submit your work individually through your logins on MS
Team
2. Try to get the concepts, consolidate your concepts and ideas from these questions
3. You should read recommended books to clarify your concepts as lecture slides are not sufficient.
4. Try to make the solution by yourself and protect your work from other students.
If I found the solution files of some students are same, then I will reward zero
marks to all those students.
5. Deadline for this assignment is October 25,2024. This deadline will not be extended.

Performance Indicators for Every Assignment (Grading rubric)


Each assignment will be assigned a letter grade of either A, B, C, D, or F based on its
submission.
This five-point (discrete) scale is described as follows:

Grade Status Description

A Exemplary Solution presented solves the problem stated correctly and meets all require
(=60-70%) ments of the problem.
B Capable Solution is mostly correct, satisfying most of the above criteria under
(=50-60%) the exemplary category, but contains some minor pitfalls, errors/flaws or limit
ations.

FA2024 Page 1
C Needs Solution demonstrates a viable approach toward solving the problem but
Improvement contains some major pitfalls, errors/flaws or limitations.
(=40-50%)

D Unsatisfactory -Critical elements of the solution are missing or significantly flawed


(=20-40%) and does not demonstrate sufficient understanding of the problem.
F Not attempted Late/no submission

To discourage copying/cheating, 30% marks may be awarded based on the marks obtained either
in the corresponding quiz or a question(s) in the Midterm exam that will be used as multiplying
factor. Hence, the final grades (out of 100) will be calculated as follows:

Submission marks (given by the letter grades shown above) + 30 * multiplying factor

FA2024 Page 2
Question # 1
You are managing a warehouse where three categories of items are stored in separate stacks. Each category has
its own priority:
• Low-priority items (values ≤ 1,000),
• Medium-priority items (values between 1,001 and 5,000),
• High-priority items (values > 5,000).
All items are stored in a single array, and the total storage capacity for all three stacks combined is 200 items.
Each item has the following details:
• Item ID
• Item Name
• Quantity
At any time, the number of items in each stack may vary: one stack could be empty, while others are full, or
they could all be partially filled.
Theory Task:
• Write the Push and Pop operations to manage three stacks in one array, storing the items in the
appropriate stack based on their priority.
• Include item details: Item ID, Name, and Quantity.
Lab Task: Implement three Stacks in a single Array.
Question # 2
Theory task: Write pseudocode(algorithm) that convert mathematical expressions written in infix notation into
prefix notation.
Lab task: In computer science, expressions can be represented in various notations. Implement a program that
can perform the following conversions:
1. Infix to Prefix Conversion: Convert an expression written in infix notation (e.g., A + B) to prefix
notation (e.g., + A B).
2. Prefix to Postfix Conversion: Convert an expression in prefix notation (e.g., + A B) to postfix notation
(e.g., A B +).
3. Infix to Postfix Conversion: Convert an expression written in infix notation (e.g., A + B) to postfix
notation (e.g., A B +).
Your program should present a menu with the following options:
1. Convert Infix to Prefix
2. Convert Prefix to Postfix
3. Convert Infix to Postfix
4. Exit
The program should repeatedly prompt the user for a choice until the user decides to exit.

Question # 3
You are tasked with managing the inventory system of a large warehouse. Each item in the inventory is
associated with a stock quantity, and you need to determine for each item if there is a larger stock quantity that
appears later in the inventory list. If such an item exists, it will be referred to as the Next Greater Element
(NGE). If no larger stock quantity is found, the result will be -1 for that item.
Problem Statement: Given an array of stock quantities representing items in the warehouse, for each item, find
the Next Greater Element (NGE) — the first stock quantity that is larger and appears after the current item in

FA2024 Page 3
the list. If no such element exists for an item, return -1 for that item. Write an efficient algorithm using a stack
to solve this problem.
Input: arr[] = [ 13 , 7, 6 , 12 ]
Output: 13 → -1
7 → 12
6 → 12
12 → -1
Lab Task: Implement the required functionality using Stack
Question # 4
A task scheduler uses a deque (double-ended queue) to manage tasks that need to be executed. Tasks can be
added to the back of the deque, and the scheduler removes tasks from the front of the deque to execute them.
Problem Statement:
• You are required to implement a task scheduler using a deque.
• The scheduler should support the following operations:
1. Add Task: Add a new task to the back of the deque.
2. Execute Task: Remove a task from the front of the deque and execute it.
Requirements:
1. Write a program that simulates the behavior of the task scheduler using a deque.
2. Provide functions for adding and executing tasks from the deque.
3. Explain the time complexity of both operations (adding and removing tasks) in your solution.
Example:
• After adding tasks A, B, and C to the deque, the deque would look like this: ['A', 'B', 'C']
• If the scheduler executes one task, the deque would become: ['B', 'C'] after removing and executing task
A.
Lab Task:
• Implement the required functionality using a deque.
Question # 5
You are required to implement a multi-level undo/redo functionality for an application using a deque (double-
ended queue). Each time a user performs an action, the current state of the application is saved onto the deque.
When the user chooses to undo an action, the most recent state is popped from the deque, and the previous state
is restored. Similarly, when the user redoes an action, the next state is popped from the deque and applied.
Problem Statement:
• Implement undo and redo functionality using a deque to manage the application's states.
• The system should support the following operations:
1. Perform Action: Save the current state onto the deque whenever the user performs an action.
2. Undo: Remove the current state from the deque and restore the previous state.
3. Redo: Reapply a previously undone action by popping the next state from the deque.
Lab Task:
1. Implement a system to handle undo and redo operations using a deque.
Question # 6
You are working as a software engineer for a company that develops a code editor. One of the essential features
of the editor is checking if the braces in the code are balanced. Braces can include parentheses (), curly braces
{}, and square brackets []. A piece of code is considered valid if all types of braces are correctly nested and
balanced.
Problem Statement:
• Given a string representing a piece of code, your task is to determine if all the braces are balanced.
• A string is considered balanced if:
1. Every opening brace has a corresponding closing brace of the same type.

FA2024 Page 4
2. Braces are properly nested (i.e., no closing brace can appear before its matching opening brace).
Theory Task:
• Write an efficient algorithm that checks if a given string has balanced braces.
• Your algorithm should use a stack to ensure that braces are correctly matched and nested.
Example Input: [{()}]
Example Output: Yes
Lab Task: Implement the algorithm using a stack to check for balanced braces.
Question # 7
You are developing a text processing application that includes a feature to check if a given string is a palindrome.
A palindrome is a word, phrase, or sequence that reads the same backward as forward (e.g., "racecar" or "level").
Your task is to implement this functionality using different data structures: a stack implemented with an array,
a singly linked list, and a doubly linked list.
Problem Statement:
• Given a string, your task is to determine if it is a palindrome using the following three data structures:
1. Stack implemented with an array
2. Singly linked list
3. Doubly linked list
Theory Tasks:
1. For each implementation, write an algorithm that:
o Pushes each character of the string onto the stack.
o Pops characters from the stack to check if the string is a palindrome.
2. Provide a detailed explanation of how each data structure is utilized in your implementation.
Lab task: Implement the palindrome checking functionality using a stack with an array, a singly linked list, and
a doubly linked list.

FA2024 Page 5

You might also like