Lecture 13 - Stack Implementation
Lecture 13 - Stack Implementation
Data Structures
2
b) Linked List implementation
• Important to keep track of these:
• Head - first element in the list
• Tail - last element in the list
• Size – number of elements in the list
• 2 classes : Node class & LinkedList class
• Various operations, e.g.:
• Check if empty & its size
• Add elements: in front, middle or end
• Remove elements: from front, middle or end
• Search for a specific element
3
Today …
Implementation of a Stack
ADT
4
LECTURE OBJECTIVES
5
1. Stack Review
● A linear data structure
• 2 main operations: push() and pop()
● Additions and deletions are restricted to one end
pop
• (AKA “top”)
push Top
• Stack uses LIFO principle!
• Adding new element: push operation
• Removing element: pop operation
• Checking top element: peek operation
● Many stack application areas, e.g.:
• Recursion
• Undo mechanisms in text editor
• Web browsers – “Back” button
• Evaluation of expression 6
Illustration of Stack methods
48
52
19
52 48 48
19 19 19 19
Empty
Stack
7
Exercise 1
219
7
6 Stack grows this way
5
4
3
top 2 51
1 20
0 46
12
• Rem (about Stacks):
• Push, pop & peek happen at the top
• Hence, need to keep track of top
• An index value because we are using an array
15
Writing Stack methods …
i. Constructor
• It must
• Create an array of a specified size
• Initialise top to -1 // -1 used to indicate an empty stack
16
ii. isEmpty()
• For checking if Stack has elements or not
• Need to check this before removing items & peeking
• Rem: top variable? // keeps track of top element
public boolean isEmpty(){
return (top == -1) ;
}
• Note:
• statement inside ( ) evaluates to true when top is -1,
false otherwise 17
iii. size()
• For determining number of elements currently in the
stack
• We can use top: it stores index of top element
• BUT +1: // array indexed from 0 while top is
initially -1
24
Exercise 2
a) Type all code segments provided into a Java file
b) Compile it.
• Fix any errors, if any
c) Implement a tester class to test all the methods.
25
More exercises
● Implement a class that accommodates other data types -
➔ Generics!
a) Implement a generic array-based Stack and call it GenStack1.
Class GenStack1 must have 2 instance variables:
• An ArrayList
• Rem:You will need to use ArrayList methods to manipulate
the ArrayList, not [] to access data elements
• An int variable to keep track of the top element
Let us call our class Stack2, and it will store String values
public class Stack2{
private class Node{
String data; // node's data part
Node next; // a pointer to next node in the Stack
Node(String data) { // Node Constructor
this.data = data;
this.next = null;
}
}
} 29
Writing Stack methods
Instance variables
public Node top; // to keep track of Top node
i. Constructor
• It will be used to create an empty Stack2 object
public Stack2(){
top = null;
}
30
ii. isEmpty()
• Checks if Stack has Nodes or not
public boolean isEmpty(){
return top == null;
}
iii. size()
• Returns number of Nodes in the Stack
public int size(){
// Can you think of possible implementations?
} 31
iv. push() else {
– Insert Node in the Stack Node oldTop = top;
top = newNode; // new top
public void push(String data){ newNode.next = oldTop;
Node newNode = new Node (data); }
if (isEmpty()){ }
top = newNode; // 1st node
} // This is adding a node in front
of the LinkedList!
32
iv. peek()
• Returns top Node (Does not delete it!)
public String peek() {
if (isEmpty() ) { // Nothing to peek()
System.out.println("Stack is empty");
return null;
}
else
return top.data; // return value of top Node
} 33
v. pop() else{
– Removes and returns top String data = top.data;
Node top = top.next; // new top
public String pop() { return data;
if(isEmpty()) { }
System.out.println("Stack is }
empty"); // Removes node from front of the
return null; LinkedList!
}
34
vi. search()
• This method must return checking if a given element is in
the stack
• Take-home work!
• Note
• Method must accept an element to look for
• It must return the index of the element if found, otherwise
-1 (not found)
35
Incomplete sample tester class
Exercise 3
a) Write a Java program that reads a String from the
keyboard then display the reversed String using push and
pop Stack operations.
b) Create a generic linkedList-based stack and call it GenStack2.
c) Test it by creating stack objects of Integer, String and Double
37
Advantages vs disadvantages
Advantages Disadvantages
• Easy to implement • No random access (Rem
LIFO?)
– Operations done on • Possibilities of Stack
one side (the top) overflow if not
implemented properly
• Top element is quickly
• Limited Capacity if
accessible
implemented using arrays
• Limited functionality, E.g.
cannot modify middle
element unless we pop all
those before it first
38
6. Summary
● Stack
● Examples of Stack application areas
● Different implementation strategies
• Refers to structure used to store data
• An array
• ArrayList
• LinkedList
● Generic implementation!
• Using arrays or ArrayList
• Using LinkedList 39
Next lesson …
Implementation
of a queue
40
Q &A
1641