Download presentation
Presentation is loading. Please wait.
Published byClyde Lambert Modified over 9 years ago
1
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please do not erase the above note)
2
Data Structures Set of primitives used in algorithms, simulations, operating systems, applications –Store data required by algorithm –Provide only the access that is required –Disallow all other access There are 5 or 10 common data structures –We cover the basic version of the core structures –Many variations exist on each structure It’s common to make application-specific modifications We’ll both build them and use the Java built-in versions!
3
Stacks a d c b Top Single ended structure Last-in, first-out (LIFO) list Applications: 1. Simulation: robots, machines 2. Recursion: pending function calls 3. Reversal of data
4
Queues abcde FrontRear Double ended structure First-in, first-out (FIFO) list Applications: 1. Simulation: lines 2. Ordered requests: device drivers, routers, … 3. Searches
5
Double ended Queues (Dequeues) dabce Double ended structure Applications: 1. Simulation: production, operations 13323121322 Track 1 Track 2 Track 3 Engine Train Engine A dequeue can model both stacks and queues
6
Priority Queues or Heaps e a b c d Top Bottom Highest priority element at top “Partial sort” All enter at bottom, leave at top Applications: 1. Simulations: event list 2. Emergency response modeling 3. Searching (next most likely)
7
Heaps Modeled as Binary Tree c qd fevt a
8
c qd fevt a e a
9
c qd fevt a e a d a
10
Heaps Modeled as Binary Tree c qd fevt a e a d a c a
11
Binary Trees m pe fdvn LevelNodes 02 0 12 1 22 2 … k2 k Binary tree has 2 (k+1) -1nodes A maximum of k steps are required to find (or not find) a node E.g. 2 20 nodes, or 2,000,000 nodes, in 20 steps!
12
Binary Trees Applications: 1. Searching and sorting (general purpose) 2. Fast retrieval of data Find/insert any item quickly (bottom, middle or top) More general than earlier data structures
13
Trees m pe fdvo n a Each node has variable number of children Applications: 1. Set operations (unions, intersections) 2. Matrix operations (basis representation, etc.) 3. Graphics and spatial data (quadtrees, R-trees, …)
14
Graphs e b f g a h d i c Applications 1. Simulation 2. Matrix representation 3. General systems representation 4. Networks: telecom, transport, hydraulic, electrical,...
15
Relationships of Data Structures Stack Dequeue Priority queue Binary tree Tree Graph Queue Set Many variations Three possible implementations 1. Dynamic arrays 2. Linked lists 3. Built-in Java classes
16
Exercise What data structure would you use to model: –People getting on and off the bus –A truss in a CAD program –A conveyor belt –The emergency room at a hospital –The lines at United Airlines at Logan –The local city street network –Books to be reshelved at the library
17
Exercise What data structure would you use to model: –People getting on and off the bus Dequeue –A truss in a CAD programDequeue –A conveyor beltQueue –The emergency room at a hospitalHeap –The lines at United Airlines at LoganQueues –The local city street networkGraph –Books to be reshelved at the libraryStack
18
Stacks Top Stack s 0 1 2 3 4= Size -1
19
Stacks ‘a’ Top Stack s 0 1 2 3 4= Size -1 Push(‘a’) Top
20
Stacks ‘a’ ‘b’ Top Stack s 0 1 2 3 4= Size -1 Push(‘a’) Top Push(‘b’) Top
21
Stacks ‘a’ ‘c’ ‘b’ Top Stack s 0 1 2 3 4= Size -1 Push(‘a’) Top Push(‘b’) Top Push(‘c’) Top
22
Stacks ‘a’ ‘c’ ‘b’ Top Stack s 0 1 2 3 4= Size -1 Push(‘a’) Top Push(‘b’) Top Push(‘c’) Top Pop() ‘c’
23
Stacks ‘a’ ‘c’ ‘b’ Top Stack s 0 1 2 3 4= Size -1 Push(‘a’) Top Push(‘b’) Top Push(‘c’) Top Pop() ‘c’ Pop() ‘b’
24
Stack Interface public interface Stack { public boolean isEmpty(); public void push( Object o ); public Object pop() throws EmptyStackException; public void clear(); }
25
Using a Stack to Reverse an Array public class Reverse { public static void main(String args[]) { int [] array = { 1, 2, 3, 4, 5 }; int i; Stack stack = new ArrayStack1(); for ( i = 0; i < array.length; i++ ) stack.push( new Integer( array[ i ] )); i = 0; while ( !stack.isEmpty() ) System.out.println( ((Integer) stack.pop()).intValue()); }
26
ArrayStack, 1 import java.util.*; public class ArrayStack implements Stack { static public final int DEFAULT_CAPACITY = 8; private Object[] stack; private int top = -1; private int capacity; public ArrayStack(int cap) { capacity = cap; stack = new Object[capacity]; } public ArrayStack() { this( DEFAULT_CAPACITY ); }
27
ArrayStack, 2 public boolean isEmpty() { return ( top < 0 ); } public void clear() { for ( int i = 0; i < top; i++ ) stack[ i ] = null; // for garbage collection top = -1; }
28
ArrayStack, 3 public void push(Object o) { if (++top == capacity) grow(); stack[top] = o; } private void grow() { capacity *= 2; Object[] oldStack = stack; stack = new Object[capacity]; System.arraycopy(oldStack, 0, stack, 0, top); }
29
ArrayStack, 4 public Object pop() throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); else { // your code goes here // remove and return item on top of the stack; // adjust member variable(s); free memory }
30
ArrayStack, 4 public Object pop() throws EmptyStackException { if ( isEmpty() ) throw new EmptyStackException(); else { Object o = stack[top]; stack[top--] = null; return o; } }
31
What is a Stack A stack is a data structure that can contain objects Objects are inserted and removed in the last-in first-out (LIFO) fashion –Think of a stack as a can of tennis balls - the last ball inserted in the can is the first one taken out Methods: public interface Stack { public boolean isEmpty(); public void push(Object o); public Object pop() throws EmptyStackException; public void clear(); }
32
Stack Methods void push(Object item) –Pushes an item into the stack –Note that the item can be any object type (String, user- defined class, etc.) Object pop() –Pops the top item of the stack and returns it to the caller –Note that what returned is a generic Object How do we convert it into the original data type? void clear() –Empty the stack boolean isEmpty() –Return true if the stack is empty and false if otherwise
33
What is a Queue A queue is another simple type of object container Objects are inserted in and removed from the queue in the first-in first-out (FIFO) fashion –Think of a queue as a waiting line in the toll booth Methods public interface Queue { public boolean isEmpty(); public void add(Object o); public Object remove() throws NoSuchElementException; public void clear(); }
34
Queue Methods void add(Object item) –Insert an item into the queue –Note that the item can be any object type (String, user- defined class, etc.) Object remove() –Remove the first item from the queue and return it to the caller –If the queue is empty, throw NoSuchElementException –Note that what is returned is a generic Object How do we convert it into the original data type? void clear() –Empty the queue boolean isEmpty() –Return true if the queue is empty and false if otherwise
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.