1.25k likes | 1.39k Views
COP 3503 FALL 2012 Shayan Javed Lecture 18. Programming Fundamentals using Java. Data Structures. So far. Looked at Arrays and ArrayLists as our data structures. So far. Looked at Arrays and ArrayLists as our data structures Very useful, but not always the best options. So far.
E N D
COP 3503 FALL 2012ShayanJavedLecture 18 Programming Fundamentals using Java
So far... • Looked at Arrays and ArrayLists as our data structures
So far... • Looked at Arrays and ArrayLists as our data structures • Very useful, but not always the best options
So far... • Looked at Arrays and ArrayLists as our data structures • Very useful, but not always the best options • Going to look at some other data structures
Data Structures • Stack • Queue • Linked List • Trees • Graph • Hashtable • etc.
Data Structures • Stack • Queue • Linked List • Trees • Graph • Hashtable • etc.
Stack • Last-In-First-Out (LIFO) Data Structure
Stack • Last-In-First-Out (LIFO) Data Structure • Basically...a stack of data.
Stack • Last-In-First-Out (LIFO) Data Structure • Basically...a stack of data. • Used when you want the oldest added data first.
Stack • Last-In-First-Out (LIFO) Data Structure • Basically...a stack of data. • Used when you want the oldest added data first. • Abstract data type (can store any kind of data).
Stack • Three Operations:
Stack • Three Operations: • push(Object): Pushes an object on top of a stack
Stack • Three Operations: • push(Object): Pushes an object on top of a stack • pop(): Returns the object at the top of the stack and removes it
Stack • Three Operations: • push(Object): Pushes an object on top of a stack • pop(): Returns the object at the top of the stack and removes it • peek(): Returns the object at the top without removing it
Stack • Some uses:
Stack • Some uses: • “Stack Frame” for method calls.
Stack • Some uses: • “Stack Frame” for method calls. • Used for evaluating arithmetic expressions: • (prefix, postfix, infix)
Stack • Java provides a Stack class (java.util.Stack)
Stack • Java provides a Stack class (java.util.Stack) • Has all the operations
Stack • Java provides a Stack class (java.util.Stack) • Has all the operations • But how does it actually store the data? (What’s the “back-end”?)
Stack • Java provides a Stack class (java.util.Stack) • Has all the operations • But how does it actually store the data? (What’s the “back-end”?) • Uses the java.util.Vector class (similar to ArrayList)
Queue • A First-In-First-Out (FIFO) data structure.
Queue • A First-In-First-Out (FIFO) data structure. • Used when you want the oldest added data first.
Queue • A First-In-First-Out (FIFO) data structure. • Used when you want the oldest added data first. • Often used for scheduling (handle first in line)
Queue • Three Operations: • enqueue(Object): Adds an object to the queue • dequeue(): Returns the object at the front of the queue and removes it • peek(): Returns the object at the front without removing it
Queue • Java provides a Queue interface (java.util.Queue)
Queue • Java provides a Queue interface (java.util.Queue) • Has all the operations • enqueue = add • dequeue = poll
Queue • Java provides a Queue interface (java.util.Queue) • Has all the operations • enqueue = add • dequeue = poll • Interface implemented in classes like LinkedList
Queue • Java provides a Queue interface (java.util.Queue) • Has all the operations • enqueue = add • dequeue = poll • Interface implemented in classes like LinkedList Queue<Integer> queue = newLinkedList<Integer>();
Arrays and ArrayLists • Work well in a lot of cases.
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short?
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short? • Data retrieval – excellentO(1)
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short? • Data retrieval – excellentO(1) • Adding data
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short? • Data retrieval – excellentO(1) • Adding data – inefficientO(n) in some cases
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short? • Data retrieval – excellentO(1) • Adding data – inefficientO(n) in some cases
Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short? • Data retrieval – excellentO(1) • Adding data – inefficientO(n) in some cases • Fixed size – have to shift/copy to new array
Arrays and ArrayLists • What if your application requires a lot of adds but not retrievals?
Arrays and ArrayLists • What if your application requires a lot of adds but not retrievals? • Use Linked Lists
Linked List • A list – a collection of linked nodes.
Linked List • A list – a collection of linked nodes. • Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array)
Linked List • A list – a collection of linked nodes. • Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array) • Insertion to the list is very fast – O(1) in most cases
Linked List • A list – a collection of linked nodes. • Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array) • Insertion to the list is very fast – O(1) in most cases • Indexing is slow. Random access also not possible
Linked List • Many variations: • Singly-Linked List(can only traverse forward) • Doubly-Linked List (can traverse in reverse too) • Circular Linked Lists • Multi-Linked Lists • etc.
Node • Basic structure – collection of linked nodes make a linked list.
Node • Basic structure – collection of linked nodes make a linked list. • Stores some data and a link to the next Node.
Node • Basic structure – collection of linked nodes make a linked list. • Stores some data and a link to the next Node. int null
Node • Basic structure – collection of linked nodes make a linked list. • Stores some data and a link to the next Node. int int null
Singly-Linked List (SLL) • Nodes connected to each other