IT2070 Lecture 01 2021
IT2070 Lecture 01 2021
Lecture 01
Introduction to Stack
1
IT2070| DSA | Stack |Samantha Rajapaksha
Subject Group
Malabe Campus
• Mr. Samantha Rajapaksha
• Ms. Dinuka Wijendra
• Ms.Jenny
• Ms.Namali Walgampaya
• Dr.Charika Weerasiriwardena
Metro Campus
• Mr. Samantha Rajapaksha
Kandy Center
• Ms. Chathurika Pinnaduwage
Mathara Center
• Mr.Ravi Supunya
2
IT2070| DSA | Stack |Samantha Rajapaksha
Teaching Methods
• Lectures – 2 hours/week
• Tutorials – 1 hour/week
• Labs – 2 hours /week
3
IT2070| DSA | Stack |Samantha Rajapaksha
Student Evaluation
• Assessments (Two exams) - 40 %
• Final Examination - 60 %
4
IT2070| DSA | Stack |Samantha Rajapaksha
Lectures will cover
Data Structures
- Stack data structure
- Queue data structure
- Linked list data structure
- Tree data structure
Algorithms
- Asymptotic Notations
- Algorithm designing techniques
- Searching and Sorting algorithms
5
IT2070| DSA | Stack |Samantha Rajapaksha
Tutorials and Labs will
cover
- Solve problems using the knowledge acquired in the lecture
- Get hands on experience in writing programs
- Java
- Python
6
IT2070| DSA | Stack |Samantha Rajapaksha
Data Structures and
Algorithms
• Data Structures
- Data structure is an arrangement of data in a computer’s memory
or sometimes on a disk.
Ex: stacks, queues, linked lists, trees
• Algorithms
- Algorithms manipulate the data in these structures in various ways.
Ex: searching and sorting algorithms
7
IT2070| DSA | Stack |Samantha Rajapaksha
Data Structures and
Algorithms
• Usage of data structures
• Programmers Tools
- stacks, queues are used to facilitate some other
operations
8
IT2070| DSA | Stack |Samantha Rajapaksha
Data Structures and
Algorithms
Algorithms
An algorithm should be
- correct.
- unambiguous.
- give the correct solution for all cases.
- simple.
- terminate.
9
IT2070| DSA | Stack |Samantha Rajapaksha
Academic Integrity Policy
Are you aware that following are not accepted in SLIIT???
From year 2018 the committing above offenses come with serious
consequences !
See General support section of Courseweb for full information.
10
IT2070| DSA | Stack |Samantha Rajapaksha
References
1. Mitchell Waite,Robert Lafore, Data Structures and
Algorithms in Java,2nd Edition, Waite Group Press,1998.
11
IT2070| DSA | Stack |Samantha Rajapaksha
Data Structures and Algorithms
Stacks
12
IT2070| DSA | Stack |Samantha Rajapaksha
Stack
Allows access to only one data item; the last item inserted
If you remove this item, then you can access the next-to-last item inserted
13
IT2070| DSA | Stack |Samantha Rajapaksha
Application of Stacks
• String Reverse
• Page visited history in Web browser.
• Undo sequence of text editor.
• Recursive function calling.
• Auxiliary data structure for Algorithms.
• Stack in memory for a process
14
IT2070| DSA | Stack |Samantha Rajapaksha
Stack
Top
In a stack all insertions and deletions are made at one end (Top). Insertions and deletions
are restricted from the Middle and at the End of a Stack
Elements are removed from a Stack in the reverse order of that in which the elements
were inserted into the Stack
The elements are inserted and removed according to the Last-In-First-Out (LIFO) principle.
15
IT2070| DSA | Stack |Samantha Rajapaksha
Stack - Push
Push item 49
16
IT2070| DSA | Stack |Samantha Rajapaksha
Stack - Pop
Pop
17
IT2070| DSA | Stack |Samantha Rajapaksha
Stack - Peek
49
Peek
Peek is used to read the value from the top of the stack without
removing it. You can peek only the Top item, all the other
items are invisible to the stack user.
18
IT2070| DSA | Stack |Samantha Rajapaksha
Question
Draw the stack frame after performing the below operations to the stack
given below.
25
Top
300
of
i ze
s 4
s
u ck i
m
x im ta
s
i) Push item 50 Ma his
ii) Push item 500 t
iii) Peek
iv) Push item 100
v) Pop
vi) Pop
vii) Pop
viii)Pop
19
IT2070| DSA | Stack |Samantha Rajapaksha
Uses of Stack
The stack operations are built into the microprocessor.
When a method is called, its return address and arguments are pushed
onto a stack, and when it returns they’re popped off.
20
IT2070| DSA | Stack |Samantha Rajapaksha
Stack - Implementation
Stack implementation using an array
class StackX {
Constructor creates a new stack
of a size specified in its argument. private int maxSize; // size of stack array
private double[] stackArray;
private int top; //top of the stack
…………………….
…………………….
}
21
IT2070| DSA | Stack |Samantha Rajapaksha
Stack – Implementation - push
class StackX{
// increment top
// insert item
}
22
IT2070| DSA | Stack |Samantha Rajapaksha
Stack – Implementation - push
class StackX {
private int maxSize; // size of stack array
private double[] stackArray;
private int top; //top of the stack
23
IT2070| DSA | Stack |Samantha Rajapaksha
Stack – Implementation - push
class StackX
{
private int maxSize; // size of stack array
private double[] stackArray;
private int top; //top of the stack
24
IT2070| DSA | Stack |Samantha Rajapaksha
Stack – Implementation – pop/peek
25
IT2070| DSA | Stack |Samantha Rajapaksha
Stack – Implementation – pop/peek
26
IT2070| DSA | Stack |Samantha Rajapaksha
Question
isEmpty() method returns true if the stack is empty and isFull() method return true if the
Stack is full.
Implement isEmpty() and isFull() methods of the stack class.
27
IT2070| DSA | Stack |Samantha Rajapaksha
Creating a stack
Question
Using the implemented StackX class, Write a program to create a stack with
maximum size 10 and insert the following items to the stack.
30 80 100 25
Delete all the items from the stack and display the deleted items.
28
IT2070| DSA | Stack |Samantha Rajapaksha
Creating a stack
class StackApp {
public static void main(String[] args) {
StackX theStack = new StackX(10); // create a stack with max size 10
29
IT2070| DSA | Stack |Samantha Rajapaksha
References
30
IT2070| DSA | Stack |Samantha Rajapaksha