0% found this document useful (0 votes)
52 views7 pages

CSE 2151 Lecture 4

1. The document discusses stacks and their implementation and applications. A stack is a data structure that follows the LIFO (last-in, first-out) principle. Common applications include web browsers and undo mechanisms. 2. Stacks can be implemented using arrays or linked lists. Array implementations have a fixed capacity while linked lists can grow and shrink dynamically. The document provides code examples for both implementations. 3. Applications discussed include reversing an array, checking the validity of arithmetic expressions using matching brackets, converting infix to postfix notation, and evaluating postfix expressions using a stack. Pseudocode is provided to demonstrate the algorithms.

Uploaded by

saymyname.pt
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)
52 views7 pages

CSE 2151 Lecture 4

1. The document discusses stacks and their implementation and applications. A stack is a data structure that follows the LIFO (last-in, first-out) principle. Common applications include web browsers and undo mechanisms. 2. Stacks can be implemented using arrays or linked lists. Array implementations have a fixed capacity while linked lists can grow and shrink dynamically. The document provides code examples for both implementations. 3. Applications discussed include reversing an array, checking the validity of arithmetic expressions using matching brackets, converting infix to postfix notation, and evaluating postfix expressions using a stack. Pseudocode is provided to demonstrate the algorithms.

Uploaded by

saymyname.pt
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/ 7

Stack

Course No.: 0714 09 CSE 2151, Course Title: Data Structures and Algorithms
Electronics and Communication Engineering Discipline, Khulna University, Khulna
Md. Farhan Sadique
Email: [email protected]
This lecture is not a study material. Use this lecture as an outline. Follow the outline to study from the mentioned sources
after each section header. The sections of this lecture have been prepared from the mentioned sources.

1. Stack (Goodrich et al.: 6.1)


A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO)
principle. A user may insert objects into a stack at any time, but may only access or remove the most recently
inserted object that remains (at the so-called “top” of the stack).
Applications of Stack:
• Internet Web browsers store the addresses of recently visited sites on a stack.
• Text editors usually provide an “undo” mechanism that cancels recent editing operations and reverts
to former states of a document.

2. Implementing Stack (Goodrich et al.: 6.1.1)

• push(): Adds element e to the top of the stack.


• pop(): Removes and returns the top element from the stack (or null if the stack is empty).
• top(): Returns the top element of the stack, without removing it (or null if the stack is empty).
• size(): Returns the number of elements in the stack.
• isEmpty(): Returns a boolean indicating whether the stack is empty.
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique

2.1. A Simple Array-Based Stack Implementation (Goodrich et al.: 6.1.2)

By convention, when the stack is empty it will have t equal to −1 (and thus has size t + 1, which is 0).

Fig. ArrayStack class.


2
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
This array-based implementation has one negative aspect- it relies on a fixed-capacity array, which limits the
ultimate size of the stack.
2.2. Implementing a Stack with a Singly Linked List (Goodrich et al.: 6.1.3)
Unlike our array-based implementation, the linked-list approach has memory usage that is always proportional
to the number of actual elements currently in the stack, and without an arbitrary capacity limit.

Fig. LinkedStack class.

3. Applications of Stack

3.1. Reversing an array Using a Stack (Goodrich et al.: 6.1.4)


Read from the source and write the program yourself.
3.2. Checking the Validity of an Arithmetic Expression (Goodrich et al.: 6.1.5, Md. Rafiqul Islam and M. A. Mottalib:
5.4.1)

Arithmetic expressions that may contain various pairs of grouping symbols, such as
• Parentheses: “(” and “)”
• Braces: “{” and “}”
• Brackets: “[” and “]”
Each opening symbol must match its corresponding closing symbol.

3
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
• Correct: ()(()){([()])}
• Correct: ((()(()){([()])}))
• Incorrect: )(()){([()])}
• Incorrect: ({[])}
• Incorrect: (
The steps of checking the validity of an arithmetic expression using stack are

Example:

4
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
Implementation:

Fig. Method for matching delimiters in an arithmetic expression.


3.3. Prefix to Postfix Conversion (Md. Rafiqul Islam and M. A. Mottalib: 5.4.2, https://www.geeksforgeeks.org/convert-
infix-expression-to-postfix-expression/)

An arithmetic expression can be expressed in various forms such as prefix, infix or postfix.
Prefix: + A B
Infix: A + B
Postfix: A B +
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the expression: a + b * c + d
The compiler first scans the expression to evaluate the expression b * c, then again scans the expression to
add a to it. The result is then added to d after another scan. The repeated scanning makes it very inefficient.
Infix expressions are easily readable and solvable by humans whereas the computer cannot differentiate the
operators and parenthesis easily so, it is better to convert the expression to postfix (or prefix) form before
evaluation. The corresponding expression in postfix form is abc*+d+. The postfix expressions can be
evaluated easily using a stack.

5
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
Below are the steps to implement the above idea:
Scan the infix expression from left to right.

Example: Expression: 5 * (6 + 2) – (12 / 4)

3.4. Evaluation of Postfix Expression (Md. Rafiqul Islam and M. A. Mottalib: 5.4.3, https://www.javatpoint.com/convert-
infix-to-postfix-notation)

The steps of evaluating a postfix expression:


Scan the postfix expression from left to right.

6
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique

Example: Evaluating 5 6 2 + * 12 4 / -

Review Questions
1. What values are returned during the following series of stack operations, if executed upon an initially
empty stack? push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7),
push(6), pop(), pop(), push(4), pop(), pop().
2. Implement a method with signature transfer(S, T) that transfers all elements from stack S onto stack T , so
that the element that starts at the top of S is the first to be inserted onto T , and the element at the bottom
of S ends up at the top of T .

Bibliography
• Book: Data Structures and Algorithms in Java, Sixth Edition - Michael T. Goodrich, Roberto Tamassia and
Michael H. Goldwasser
• Book: Data Structures Fundamentals, Second Edition – Md. Rafiqul Islam and M. A. Mottalib
• Website: https://www.geeksforgeeks.org/convert-infix-expression-to-postfix-expression/
• Website: https://www.javatpoint.com/convert-infix-to-postfix-notation

You might also like