0% found this document useful (0 votes)
11 views18 pages

Group8 Report

Uploaded by

Bryan Ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views18 pages

Group8 Report

Uploaded by

Bryan Ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

STACK

AND
HASH TABLE
STACK
• A stack is a Linear Data Structure
• STACK is a LIFO data structure (Last In First
Out)
• You can only add or remove plates from the
top
• It’s a stored Objects into a sort of “vertical
tower”
KEY OPERATIONS

• PUSH – Adds an elements to the top of the stack

• POP – Removes the top elements

• PEEK – Returns the Value of the top element without


removing it.

• IsEmpty – Cheeks if the stacks is empty.


EXAMPLE

IsFull IsEMPTY IsEmpty


Cheeks if
the stacks is
NO YES
empty.

ty.

STACK
EXAMPL
E
ALICE

KARINA

PHARSA CHANG’E

LYLIA

STACK
VARIABLES
Adds an elements
EXAMPLE to the top of the
ALICE
stack

KARINA

PHARSA CHANG’E

PUSH LYLIA

STACK
VARIABLES
EXAMPLE

IsFull –
Checks
LYLIA
if the
PHARSA Now Is it Full? stack is
YESS Full (for
CHANG’E
fixed-
KARINA size
stacks)
ALICE

STACK
VARIABLES
EXAMPL the Value of
E PEEK
the top
LYLIA element

PHARSA
CHANG’E
KARINA
ALICE

STACK
EXAMPLE POP –
Removes
the top
LYLIA elements

PHARSA
CHANG’E
KARINA
ALICE

STACK
VARIABLES
NEW PEEK
EXAMPLE

LYLIA

PHARSA

CHANG’E

KARINA

ALICE

STACK
VARIABLES
Code for Stack

import java. util. Stack;

public class Main {

public static void main(String[] args) {

Stack<String> stack = new Stack<String>() ;

//System.println(stack.empty());

stack.push(“Alice”);
stack.push(“Karina”);
stack.push(“Chang’e”);
stack.push(“Pharsa”);
stack.push(“Lylia”);
Continuation

stack. pop() ;

//System.println(stack.peek());

//System.println(stack.search(“Alice”));

//System.println(stack) ;

}
USES OF STACKS?

1. Undo / Redo features in text editors

• 2. Moving Back / Forward though browser history

• 3. Backtracking algorithms like (Maze, File Directories)

• 4. Calling functions its called (Call Stack)


HASH TABLE

• A data structure that stores unique keys to value


(ex. <Integers and String>)
• Each key/value pair is known as an Entry
• FAST Insertion, look up, deletion of key/value pairs
• Not Ideal for small data sets, great with large data
sets
CONTEXTS OF A HASH TABLE

1. HASHING = takes a key and computes an integers


(formula will vary based on key & data type)
In Hashtable , we use the hash % capacity to
calculate an index number
Ex. = key.hashCode() % capacity = index
CONTEXTS OF A HASH TABLE

2. Bucket = An indexed storage location for one or more Entries can


be store multiple Entries in a Case of Collision (linked-similarly a
LinkedList)

3. Collision = Hash Function generates the same index for


more than one key less collision = more efficiently

Ex. 0 105 105


HASHTABLE
K= 100
0 V=“Spong
e-”
K= 321
ENTRY <K,V> key.hashCod % 1 V=
“Sandy”
e() CAPACITY
100 “SpongeBob’
100
2
K= 123
123 “Patrick”
123
3 V=“Patric
k”
321 “Sandy”
321 4
555 “Squidward”
% 10 K= 555
555
777
5 V=“SquidIt’s called
COLLISIO
“Gary” -”
777 N
6
23 “Mr.Crab 237 K =237
K = 777
7 ”
7 V V
=“Gary” =“Mr.-”
8
9
Code for Hash Table
import java.util.*;

public class Main {

public static void main(String[] args) {

Hashtable <Integer, String> table = new Hashtable <>(10) ;

table.put(100, “SpongeBob”) ;
table.put(123, “Patrick”) ;
table.put(321, “Sandy”) ;
table.put(555, “Squidward”) ;
table.put(777, “Gary”) ;

for(Integer key : table. keySet()) {


System. out. println(key.hashCode() % 10 + “/t” + key + “/t” + table. get(key)) ;

}
}
}

You might also like