Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data Structures

Data Structures

Lecturer slide on Data Structures

rakhmad

May 27, 2012
Tweet

Other Decks in Education

Transcript

  1. Data Structures • Static Data Structures • Array • Fixed

    Length • Fast speed for read-write operation • Not flexible enough
  2. Data Structures • Dynamic Data Structures • Linear: Linked List,

    Stack, Queue, etc. • Binary Trees • Size can grow easily • Dynamic Memory Allocation • Slow in read-write operations
  3. Linked List • Linear collection of “Node” • Node •

    Can contain data of any type • Contain reference to next Node
  4. Linked List • A linked list object usually contains only

    reference to first object • Accessing each subsequent node via the link reference stored in previous node
  5. Linked List • Fast in adding and removing elements •

    Visiting in a sequential order is efficient • Random Access is inefficient
  6. Linked List • Single Linked List • Each node contains

    one reference to the next node in the list • Double Linked List • Each node contains reference to next and previous node in the list
  7. Linked List • Java implementation located in java.util.LinkedList • Generic

    class • Can not contain primitive • Must used Wrapper class
  8. Linked List • Accessing Node inside a linked list •

    Encapsulate a position inside a linked list • Protects the linked list whilst giving acccess • Solution in Java: ListIterator interface
  9. ListIterator • Initially, iterator points before first element • iterator:

    like a cursor between 2 letters • Move the iterator: next() method • Throws NoSuchElementException if pass the end of the list
  10. ListIterator • Checking for next element in a list: hasNext()

    method • Other methods available: • previous() • hasPrevious()
  11. ListIterator • Removing element • Can call remove() once after

    next() or previous() • Can not remove() after add() • Throws IllegalStateException
  12. ListIterator • Adding new element • Using add() method •

    Add an object after iterator position • Moves iterator pass new object
  13. Linked List • Implementing our own LinkedList • Simplified version

    • Singly Linked List • Use Object, not Type parameter • Only provide access to first element
  14. Linked List • Node • Store data as Object •

    Link (reference) to next Node • Make Node as private inner class for LinkedList
  15. Linked List • Linked List • Hold reference to first

    Node • Has methods: • getFirst() • addFirst() • removeFirst()
  16. Linked List • Adding new Node to the list •

    New Node becomes head of the list • The old ‘head’ becomes its next Node
  17. Linked List • Remove first Node • Save data for

    later being returned • The successor becomes the first Node
  18. Linked List • Implements ListIterator interface as inner class •

    LinkedListIterator • Has access to Node class and first Node
  19. Linked List Iterator • Implementing next() method • Store last

    visited node as position • Store before last position as previous • next() called, position is advanced to position.next • The old position stored in previous
  20. LinkedListIterator • Implementing remove() method • If the element is

    the head of list, call removeFirst() method • Otherwise, update the reference in preceding Node to skip the removed element • It is illegal to call remove() twice in a row
  21. LinkedListIterator • Implement add() method • The most complex operation

    • Inserting new Node after current position • Update next Node reference in current position and new Node
  22. Abstract Data Types • Define the fundamental operation • Do

    not specify the implementation • Example • Linked List and Array List • List • Array
  23. Stack • Last In, First Out • Allows insertion and

    removal at one end • Top of the stack • Using push() and pop() • Stack of Books
  24. Stack • Stack usually used by: • Virtual machine to

    trace program execution • Compiler to evaluate arithmetic expression
  25. Queue • First In, First Out • Adding items to

    the end of the queue • Tail of a Queue • Removing items from the start of the queue • Head of a Queue
  26. Queue • Items are removed in the same order which

    they have been added • People lining up