



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of stacks and queues in java collections framework. It explains their differences, use cases, implementations, and properties. The document also includes examples of using stacks for matching brackets and traversing a tree, and queues for finding matches in a file. It is useful for university students studying computer science or software engineering, particularly those focusing on data structures and algorithms.
Typology: Slides
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























316
PriorityQueue
«interface» Queue
317
push
pop
LIFO (Last-In-First-Out) access method
add
remove
FIFO (First-In-First-Out) access method
319
Stack pointer sp
myElements[5] myElements[4] myElements[3] value myElements[2] value myElements[1] value myElements[0] value
public void push (Object x) { myElements [sp] = x; sp++; }
public Object pop ( ) { sp--; return myElements [sp]; }
320
import java.util.ArrayList;
public class ArrayStack { private ArrayList ;
public ArrayStack () { items = new ArrayList
public boolean isEmpty () { return items. isEmpty (); } public void push (Object x) { items. add (x); } public Object pop () { return items. remove (items. size () - 1); } public Object peek ( ) { return items. get (items. size () - 1); } }
322
323
325
Stack Example:Traversing a Tree Stack stk = new Stack
Save for future processing
if no children, take the next node from the stack
326
Queueing print jobs
Entering keystrokes
Processing mouse clicks
328
329
boolean isEmpty () boolean add (E obj) E remove () E peek ()
331
Set interface
TreeSet
HashSet
332
«interface» Collection
«interface» Iterator
TreeSet HashSet
«interface» Set Methods of Set < E > are the same as methods of Collection < E >
Set ‟s semantics are different from Collection (no duplicates), but Set does not add any new methods.
334
TreeSet HashSet
«interface» Set
import java.util.*; public class SetExample { public static void main(String args[]) { Set set = new HashSet(); set.add("Bernadine"); set.add("Elizabeth"); set.add("Gene"); set.add("Elizabeth"); set.add("Clara"); System.out.println(set); Set sortedSet = new TreeSet(set); System.out.println(sortedSet); } } adrish.b@ardentcollaboratio
335