Académique Documents
Professionnel Documents
Culture Documents
STIA2024
Data Structures & Algorithm
Analysis
1
Lecture Outline
Stack
Using an array
Using linked list
Applications
2
Learning Objective
To elaborate the concepts of stack,
To apply the concept of stack in programming,
3
Stack
Additions are made to one end, the top
The item most recently added is always on the top
Most commonly used in computer science world,
especially by many algorithms (E.g.: compiler, run-time
environment)
Analogy:
4
Stack
More restrictive data structure because client can
access only a single element at topmost of the stack.
A stack is a data structure in which only the top
element can be accessed.
Storing a data item in a stack is called pushing it onto
the stack
Removing a value from a stack is called popping the
stack
A stack is a container for an object in which the insert
and remove process will happen at one end called as
top
Concepts : Last In First Out (LIFO)
5
Item removed items added at
from this end only this end only
Therefore:
Stack[0] for the lowest item (bottom)
Stack[7] for the top item
Stack using arrays
• An integer variable top tells us which is the top element.
Push 7
5 12 5 12 7
Pop returns 7
top top
16
Stack using arrays
push()
the top counter will increase by 1 for each time the item was inserted
into the stack.
current stack after push()
3 3
2 top = 2 2 30
top = 1 1 20 1 20
0 10 0 10
pop()
the top counter will decrease by 1 for each time the item was removed
from the stack. current stack after pop()
3 3
top = 2 2 30 2
1 20 top = 1 1 20
0 10 0 10
Stack using arrays
3 3 3
2 2 2
1 1 top = 1 1 3
top = -1 0 top= 0 0 1 0 1
3 3 3
top =2 2 8 2 top = 2 2 7
1 3 top = 1 1 3 1 3
0 1 0 1 0 1
To access the lower item in the stack, the top item should be removed
until the item is found.
(a) A new node that references the top of the stack; (b) the new node
is now at the top of the stack.
26
Stack using linked list
Before creating a stack, pointer “top” must be initialized to
“null”.
It means the stack is empty
27
Stack using chain of linked nodes
The concept is same as linked list.
Each node is a item/object in stack.
The implementation using 2 classes:
1. Node class
2. StackList class
Stack using chain of linked nodes
The declarations of class Node:
/* Node.java Authors: Koffman & Wolz
* Represents a node with information and link fields.
*/
class Node
{
// Data fields for Node
private Object info; // data stored in the node
private Node link; // link to next node
// Methods
// Constructors
// postcondition: Creates a new empty node.
public Node() {
info = null;
link = null;
}
// postcondition: Creates a new node storing obj.
public Node(Object obj) {
info = obj;
link = null;
}
// postcondition: Creates a new node storing obj
// and linked to node referenced by next.
public Node(Object obj, Node next) { ***Object – can be
info = obj;
link = next; primitive data type
}
(int,double, char, Float
dll) or Object
Stack using chain of linked nodes
The declarations of class Node:
// accessors
public Object getInfo()
{
return info;
}
public Object getLink()
{
return link;
}
// mutators
public void setInfo(Object newInfo)
{
info = newInfo;
}
public void setLink(Node newLink)
{
link = newLink;
}
}
Class StackList
public class StackList {
// Data fields
private Node top; // reference to
// top of stack
// Methods
public StackList() {
top = null; // new stack is empty
}
/* push - pushes an item onto a stack
* postcondition: item is at the top of the stack
*/
public void push(Object item) {
//Allocate a new node, store item in it, and
// link it to old top of stack.
top = new Node(item, top);
}
32
Stack - Removing node
Algo.
Retrieve item at top
Remove item at top of stack – link top to second element
Return item at top of stack
code
public Object pop() {
Object item = peek(); // Retrieve item at top
//Remove old top of stack
top = top.getLink(); // Link top to second element
return item; // Return data at old top
}
Stack - Removing node
Use this statement
top = top.getLink() // top = top.link
X 2 4 5 7
top
4 5 7
top
remove 2 34
Stack - Inserting a node
Algo.
If stack is empty
Set pointer “top” to new node
else
Set pointer “link” of new node to the first node
Set pointer “top” to the new node
.
top
2
newNode
35
Stack - Inserting a node:empty list
code
Node newNode = new Node(new Integer(8))
if (top == null)
top = newNode;
else {
newNode.setLink(top); // newNode.link = top
top = newNode;
}
count++;
top = newNode
X 8
top
newNode
8
top
36
Insert 8
Stack-Inserting a node:non empty
list
code
Node newNode = new Node(new Integer(7))
if (top == null)
top = newNode;
else {
newNode.setLink(top); // newNode.link = top
top = newNode;
}
count++;
X 8
top
top = newNode newNode.link = top
7 X
newNode
7 8
top 37
Insert 7
Java Collections Framework
Java 2 provides various classes that implement
abstract data types involving collections of
elements, such as lists, stacks and sets.
These are in the java.util package , and are
known as the Java Collections Framework.
The framework contains interfaces, which can be
considered to be specifications of abstract data
types; abstract classes, which implement some
methods, but not all; and classes which fully
implement all methods. The following chart shows
the main components of the Collections
Framework.
Java Collections Framework
A collection can be a set or a list,
defined in the interfaces Set and
List, which are sub-interfaces of
Collection. AbstractSet TreeSet
HashSet
Set SortedSet
AbstractCollection Collection
List
AbstractSequentialList
The Stack Class
Vector The Stack class
represents a last-in-first-
out stack of objects. The
Stack elements are accessed
+empty() : boolean only from the top of the
+peek() : Object
+pop() : Object stack. You can retrieve,
+push(element: Object) : void
+search(element: Object) : int insert, or remove an
element from the top of
the stack.
Example
import java.util.Stack;
public class StackExample {
public static void main(String args[]) {
Stack s = new Stack();
s.push("Ali");
s.push("Siti");
s.push("Zila");
42
Conclusion
Q & A Session
43
Example (full code) – Stack
using chain of linked nodes
class Node {
// Data fields for Node
private Object info; // data stored in the node
private Node link; // link to next node
// Constructors
public Node(Object obj) {
info = obj;
link = null;
}
// postcondition: Creates a new node storing obj and linked to node
//referenced by next.
public Node(Object obj, Node next) {
info = obj;
link = next;
}
// accessors
public Object getInfo() {
return info;
}
public Object getLink() {
return link;
}
// mutators
public void setInfo(Object newInfo) {
info = newInfo;
}
public void setLink(Node newLink) {
link = newLink;
}
} //class node
import java.io.*;
import java.util.EmptyStackException;
public class StackList {
// Data fields
private Node top; // reference to top of stack
private int itemNum;
// Methods
public StackList() {
top = null; // new stack is empty
itemNum=0;
}
/* push - pushes an item onto a stack postcondition: item is at the top of the
stack*/
public void push(Object item) {
//Allocate a new node, store item in it, and
// link it to old top of stack.
top = new Node(item, top);
itemNum++;
}
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
stack.display();
System.out.println("Top Item = "+ stack.peek());
System.out.println("Stack size = "+ stack.size());
System.out.println("\nItem removed = "+ stack.pop());
System.out.println("Item removed = "+ stack.pop());
stack.display();
System.out.println("Item at top of stack = "+ stack.peek());
System.out.println("Stack size = "+ stack.size());
}
} // class StackList
Output
top -->50-->40-->30-->20-->10-->null
Top Item = 50
Stack size = 5
Item removed = 50
Item removed = 40
top -->30-->20-->10-->null
Item at top of stack = 30
Stack size = 3 Process Exit...
Exercise –Tracing for output
import java.util.Stack;
public class StackExample {
public static void main(String args[]) {
Stack myStack = new Stack();
myStack.push (16);
myStack.push (8);
myStack.push (20);
myStack.push (3);
System.out.println("The top element of myStack: " +
myStack.peek());
myStack.pop();
System.out.println("After the pop operation, the top element of
myStack: " + myStack.peek());
System.out.println("myStack elements:");
while (!myStack.isEmpty()){
System.out.print(myStack.peek()+ " ");
myStack.pop();
}
}
}