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

Birla Institute of Technology and Science, Pilani Hyd Campus

BITS F232: Foundations of Data Structures and Algorithms


1st Semester 2022-23 Lab No:6 (10th TO 15th Oct 2022)
Stacks using Arrays and Linked Lists
General Instructions
• Based on the instructions given in the lab, you will either use ArrayStack or LinkedStack ADT in
your code to solve the given tasks. DO NOT use STL.
• For each program given, make sure to show the time spent by your code by using “timer.h”
header file as taught in previous lab.
• Indent your code appropriately and use proper variable names, if not already provided in the
question. Also, use comments wherever necessary.

• Make sure to debug your code after every task or after every code block to avoid having to debug
your entire file at once in the last minute.

Program 1: (Prog1.cpp attached)

The Balanced Parentheses problem is a very important problem in Computer Science. We have given you a tougher
problem to solve: “Balanced Brackets” problem. Allowed brackets are only parentheses ‘(‘, ‘)’ and curly-braces ‘{‘,’}’.
Implement the isBalanced() method. Try to follow along the pseudo code given below to come up with a valid
solution in C++. You should get the output as shown below: (Use Array Implementation of Stack here)

bool isBalanced(string expr)


{
// initialize either an ArrayStack or a LinkedStack as per instructions
given in the lab.

for (int i = 0; i < expr.length(); i++)


{
char token = expr[i];

if (!isBracket(token))
continue;

// if the token is an opening bracket, PUSH the token into the stack
// ………………………………………………………………… TASK 1 ………………………………………………………………………

// else if the token is a closing parentheses


{
// if the stack is empty OR the TOP of the stack is not opening
parentheses, we should return false.
// otherwise, POP from the stack.
// …………………………………………………………… TASK 2 …………………………………………………………………
}
// else if the token is a closing curly braces
{
// if the stack is empty OR the TOP of the stack is not opening
parentheses, we should return false.
// otherwise, POP from the stack.
// …………………………………………………………… TASK 3 …………………………………………………………………
}
}
// make sure that the stack is empty
// this means all brackets matched.
// ……………………………………… TASK 4 ………………………………………
}

Output:

Program 2: (Prog2.cpp attached has a Linked implementation of Stack)


Run the program Prog2.cpp as given in the attachment for HTML tags matching example as discussed in the class.
Find out what is the output of the code for the input given at the end. Modify the input to accommodate other
test cases. Write down all the observations in your note book (TASK 5).

INPUT

Write down the runtime complexity of the code you run in this task in your note book. (TASK 6)
Program 3: (Prog3.cpp attached)

Stock Span Problem is yet another interesting problem. An efficient way to solve this problem is by using Stack.
However, solving this problem using Stack is tricky. You need to implement findSpans() method. Try to go through
the pseudo code given below to come up with a valid solution. The method returns an array containing the span of
i’th day for the given stockPrice array. You should get the output as shown below: (You are free to use either Array
or Linked List implementation of stack here)

int *findSpans(int *stockPrice, int n)


{
// initialize either an ArrayStack or a LinkedStack as per instructions
given in the lab.

// array representing the span of the stocks


int *spans = new int[n];

for (int day = 0; day < n; day++)


{
// while stack is not empty and TOP of the stack contains the day
// index whose stock price is LESS THAN or EQUAL to
// the stock price for this day
While (……………………… TASK 7 ………………………)
{
// POP from stack …………………………………… TASK 8 ………………………………………
}

// update spans array


if(stack.empty()) {
// if stack is empty, simply add 1 to current day index.
spans[day] = day + 1;
} else {
// span for this day is the consequtive day count
// i.e. current day – TOP of the stack.
// …………………………………………………………… TASK 9 …………………………………………………
}

// PUSH this day index into the stack ………………………… TASK 10 ………………

}
return spans;
}

Output:

----------------------------

You might also like