ALGO LEAP Module 10
ALGO LEAP Module 10
OVERVIEW
This module discusses the concept of stack and its implementation using linked list. Stack
operations and its time complexity are also discussed here.
MODULE OBJECTIVES:
132 | P a g e
PRE-TEST
Name: Score:
Directions: Encircle ‘T’ if the statement is correct. Otherwise, encircle ‘F’. (10 points)
T | F 2. Stack follows the First In Last Out concept of adding and removing items in a list.
T | F 4. Non-linear data structures such as stacks arrange the data into a sequence and
follow some sort of order.
T | F 5. Push is a stack operation that adds an item at the beginning of the list.
133 | P a g e
CONTENTS
A stack is a basic data structure that can be logically thought of as a linear structure represented
by a real physical stack or pile, a structure where insertion and deletion of items takes place at
one end called top of the stack. The basic implementation of a stack is also called a LIFO (Last
In First Out) to demonstrate the way it accesses data.
There are many real-life examples of a stack. Consider an example of plates stacked over one
another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate
which has been placed at the bottommost position remains in the stack for the longest period of
time. So, it can be simply seen to follow LIFO (Last In First Out) order.
Push: Adds an item to the stack. If the stack is full, then it is said to be an Overflow
condition.
134 | P a g e
Pop: Removes an item from the stack. The items are popped in the reversed order in
which they are pushed. If the stack is empty, then it is said to be an Underflow condition.
push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these
operations.
Applications of Stack
Implementation
Using array
Using linked list
Although a linked list implementation of a stack is possible (adding and deleting from the head
of a linked list produces exactly the LIFO semantics of a stack), the most common applications
for stacks have a space restraint so that using an array implementation is a natural and efficient
one (In most operating systems, allocation and de-allocation of memory is a relatively expensive
operation, there is a penalty for the flexibility of linked list implementations).
135 | P a g e
To further understand how a stack works, consider our sample program.
Node.java
// a constructor that creates a node with initial data specified by the parameter
public Node(int data) {
this.data = data;
next = null;
}
Stacks.java
int popValue = 0;
if(count()==1) {
popValue = head.getData();
head = null;
}
else {
Node currentNode = head;
for(int i=1; i<count()-1; i++)
currentNode = currentNode.getNext();
Node tempNode = currentNode.getNext();
popValue = tempNode.getData();
currentNode.setNext(null);
tempNode = null;
}
return popValue;
}
MainProgram.java
import java.io.*;
public class MainProgram {
138 | P a g e
POST-TEST
Name: Score:
I. True or False. Encircle ‘T’ if the statement is correct. Otherwise, encircle ‘F’. (10 points)
T | F 2. Non-linear data structures such as stacks arrange the data into a sequence and
follow some sort of order.
T | F 4. Stack follows the First In Last Out concept of adding and removing items in a list.
T | F 7. Push is a stack operation that adds an item at the beginning of the list.
II. Multiple Answer Questions. Encircle the letter of the item that will answer the following
questions. Each question may have more than one answer. (2 points each)
140 | P a g e
MODULE TEST
Name: Score:
I. True or False. Encircle ‘T’ if the statement is correct. Otherwise, encircle ‘F’. (10 points)
T | F 3. Non-linear data structures such as stacks arrange the data into a sequence and
follow some sort of order.
T | F 7. Stack follows the First In Last Out concept of adding and removing items in a list.
T | F 9. Push is a stack operation that adds an item at the beginning of the list.
II. Programming
Problem
Using one-dimensional array of integers, create a program that will simulate the following stack
(LIFO) operations. (32 points)
Sample Output
[1] Push
[2] Pop
[3] Peek
[4] isEmpty
[5] isFull
[6] Exit
Enter choice: 1
[1] Push
[2] Pop
[3] Peek
[4] isEmpty
[5] isFull
[6] Exit
Enter choice: 3
Value of top is 5.
[1] Push
[2] Pop
[3] Peek
[4] isEmpty
[5] isFull
[6] Exit
Enter choice: 6
Good bye!
142 | P a g e
Grading Rubric
143 | P a g e
REFERENCES
Textbook:
[1] Malik D. S. 2019. C++ Programming including Data Structures. Cengage Learning.
Online Resources:
[2] HackerEarth. 2020. Basics of Stacks. Retrieved May 10, 2020 from
https://www.hackerearth.com/practice/data-structures/stacks/basics-of-stacks/tutorial/
[3] TutorialsPoint. 2020. Data Structures - Algorithms Basics. Retrieved May 10, 2020
https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
[4] Friesen J. 2017. Data Structures and Algorithms in Java, Part 1: Overview. Retrieved May
10, 2020 from https://www.javaworld.com/article/3215112/java-101-datastructures-and-
algorithms-in-java-part-1.html
144 | P a g e