You are on page 1of 4

EXPERIMENT 5 Date Performed: _________

To construct stack using arrays and linked lists in C++


OBJECTIVE:
• Creating stack data structure using arrays
• Creating stack data structure using linked list

EQUIPMENT:
• Computer equipped with Microsoft Visual Studio 15 and Office 16

BACKGROUND:
In computer science, a stack is an abstract data type that serves as a collection of elements, with
two principal operations:

• push, which adds an element to the collection, and


• pop, which removes the most recently added element that was not yet removed.
The order in which elements come off a stack gives rise to its alternative name, LIFO (last in,
first out). Additionally, a peek operation may give access to the top without modifying the
stack. The name "stack" for this type of structure comes from the analogy to a set of physical
items stacked on top of each other, which makes it easy to take an item off the top of the stack,
while getting to an item deeper in the stack may require taking off multiple other items first.
Considered as a linear data structure, or more abstractly a sequential collection, the push and
pop operations occur only at one end of the structure, referred to as the top of the stack. This
makes it possible to implement a stack as a singly linked list and a pointer to the top element.
A stack may be implemented to have a bounded capacity. If the stack is full and does not contain
enough space to accept an entity to be pushed, the stack is then considered to be in an overflow
state. The pop operation removes an item from the top of the stack.

Figure 1
LAB TASKS:

Problem 1:

We need to implement CharStack data structure using arrays that can store characters and have
the following public functionalities:

• Constructor: Ask the size of stack from user than allocate memory to the dynamic pointer
• Destructor : to delete the dynamic allocation done in the constructor
• bool push(char x): pushes x to stack
• bool pop(char & poppedElem): pops an element from stack in poppedElem and then
returns the success status
• IsFull(): return true if the stack is full
• IsEmpty(): return true if the stack is empty

Note: Implement this data structure using classes.

Problem 2:

Write a C++ program to that reverses a string using stack. You should use your Stack class
implemented in problem1.

Problem 3:

You have to implement a program that can solve the parenthesis problem using stack.
Parenthesis problem says that every opening bracket should have corresponding closing bracket.

Assume that the user enters an expression (containing operators, operands, and brackets) in the
form of string. Create a function bracketAnalyzer with prototype bool bracketAnalyzer(string
exp); and perform the followings in its body:-

1. Declare a character stack S implemented in problem 1.


2. Now traverse the expression string exp.
a. If the current character is a starting bracket (‘(‘ or ‘{‘ or ‘[‘) then push it to stack.
b. Else If the current character is a closing bracket (‘)’ or ‘}’ or ‘]’) then pop an
element from stack
i. If popping the stack returns false, it means the stack is empty. This
inherently means that current closing bracket do not have the
corresponding starting bracket. So, return false.
ii. if the popped character is the matching starting bracket then fine else
parenthesis are not balanced so return false.
c. Else the current character is an operator or an operand, so we will simply move
to the next symbol from the string exp
3. After complete traversal if there are still some starting bracket left in stack then brackets
are not balanced. So, return false.

Problem 4:

We need to implement IntStack data structure using linked list that can store integer numbers
and have the following public functionalities:
• Constructor: Initializes the head of the stack
• Destructor
• Push(element)
• Pop()

Note: Implement this data structure using classes.


CL2002 – Data Structures and Algorithms Lab
The focus of this lab is to cover some of the commonly used data structures and algorithms in
software development. It is aimed at helping the students to understand the reasons for choosing
certain structures or algorithms while implementing them using Microsoft Visual Studio.
CLO Statement ↓ Exemplary Proficient Developing Beginning Novice
Score → (5) (4) (3) (2) (1)

Demonstrate
Understand
elementary skills Attempts Attempts lab
Attempts all the given
to construct partial lab tasks with
1 lab tasks with concept but No attempt
various data tasks with incorrect
correct output unable to
structures. correct output output
apply it

Completes
Behaves Completes
the lab task
responsibly, Completes the lab task Poorly
3 with help of Does not
and individually the lab task on with minor attempts lab
instructor and work
performs the his own help of tasks
students
lab. instructor
around

CLO MARKS OBTAINED


1
3

You might also like