1 / 124

COP 3503 FALL 2012 Shayan Javed Lecture 18

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.

ciaran-fry
Download Presentation

COP 3503 FALL 2012 Shayan Javed Lecture 18

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 FALL 2012ShayanJavedLecture 18 Programming Fundamentals using Java

  2. Data Structures

  3. So far... • Looked at Arrays and ArrayLists as our data structures

  4. So far... • Looked at Arrays and ArrayLists as our data structures • Very useful, but not always the best options

  5. 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

  6. Data Structures • Stack • Queue • Linked List • Trees • Graph • Hashtable • etc.

  7. Data Structures • Stack • Queue • Linked List • Trees • Graph • Hashtable • etc.

  8. Stack • Last-In-First-Out (LIFO) Data Structure

  9. Stack • Last-In-First-Out (LIFO) Data Structure • Basically...a stack of data.

  10. Stack • Last-In-First-Out (LIFO) Data Structure • Basically...a stack of data. • Used when you want the oldest added data first.

  11. 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).

  12. Stack • Three Operations:

  13. Stack • Three Operations: • push(Object): Pushes an object on top of a stack

  14. 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

  15. 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

  16. Stack • Some uses:

  17. Stack • Some uses: • “Stack Frame” for method calls.

  18. Stack • Some uses: • “Stack Frame” for method calls. • Used for evaluating arithmetic expressions: • (prefix, postfix, infix)

  19. Stack • Java provides a Stack class (java.util.Stack)

  20. Stack • Java provides a Stack class (java.util.Stack) • Has all the operations

  21. 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”?)

  22. 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)

  23. Queue • A First-In-First-Out (FIFO) data structure.

  24. Queue • A First-In-First-Out (FIFO) data structure. • Used when you want the oldest added data first.

  25. 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)

  26. 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

  27. Queue • Java provides a Queue interface (java.util.Queue)

  28. Queue • Java provides a Queue interface (java.util.Queue) • Has all the operations • enqueue = add • dequeue = poll

  29. Queue • Java provides a Queue interface (java.util.Queue) • Has all the operations • enqueue = add • dequeue = poll • Interface implemented in classes like LinkedList

  30. 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>();

  31. Arrays and ArrayLists • Work well in a lot of cases.

  32. Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures

  33. Arrays and ArrayLists • Work well in a lot of cases. • Can use as “back-end” data structures • But where do they fall short?

  34. 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)

  35. 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

  36. 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

  37. 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

  38. 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

  39. Arrays and ArrayLists • What if your application requires a lot of adds but not retrievals?

  40. Arrays and ArrayLists • What if your application requires a lot of adds but not retrievals? • Use Linked Lists

  41. Linked List • A list – a collection of linked nodes.

  42. Linked List • A list – a collection of linked nodes. • Dynamic data structure – size is not fixed, and memory is not contiguous (unlike an array)

  43. 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

  44. 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

  45. 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.

  46. Node • Basic structure – collection of linked nodes make a linked list.

  47. Node • Basic structure – collection of linked nodes make a linked list. • Stores some data and a link to the next Node.

  48. Node • Basic structure – collection of linked nodes make a linked list. • Stores some data and a link to the next Node. int null

  49. Node • Basic structure – collection of linked nodes make a linked list. • Stores some data and a link to the next Node. int int null

  50. Singly-Linked List (SLL) • Nodes connected to each other

More Related