G64ADS Advanced Data Structures Guoping Qiu Room C34, CS Building
Abstract Data Type (ADT) In Object Oriented Programming data and the operations that manipulate that data are grouped together in classes Abstract Data Types (ADTs) or data structures or collections store data and allow various operations on the data to access and change it
Why Abstraction Specify the operations of the data structure and leave implementation details to later Java uses an interface to specify operations Many, many different ADTs Picking the right one for the job is an important step in design High level languages often provide built in ADTs, The C++ STL, the Java standard library
The List ADT List of size N: A0, A1, …, AN-1 Each element Ak has a unique position in the list Elements can be arbitrarily complex Operations insert(X, k), remove(k), find(X), findKth(k), printList()
Lists Using Arrays Operations insert(X,k) – O(N) remove(k) – O(N) find(X) – O(N) findKth(k) – O(1) printList() – O(N)
Linked Lists Elements not stored in contiguous memory Nodes in list consist of data element and next pointer
Linked Lists Operation – insertion Insert (X, A) – O(1)
Linked Lists Operation – deletion delete (A) – O(1)
Linked Lists Operations find(X) – O(N) findKth(k) – O(N) printList() – O(N)
Linked Lists Doubly-linked list
List Implementation In Java
List Implementation Example: Using remove on a linkedlist Quadratic time on all list
List Implementation Example: Using remove on a linkedlist Quadratic time on arraylist, linear time on linkedlist
Stack Stack is a list where insert and remove take place only at the “top” Operations Push (insert) element on top of stack Pop (remove) element from top of stack Top: return element at top of stack LIFO (Last In First Out)
Stack Implementation Stack is a list, any list implementation will do Linked list implementation Array implementation
Stack Applications Balancing Symbols
Stack Applications Balancing Symbols
Stack Applications Postfix Expressions
Stack Applications Infix to Postfix Conversion
Stack Applications Method Calls
Queue Queue is a list where insert takes place at back, but remove takes place at the front Operations Enqueue (insert) element at the back of the queue Dequeue (remove and return) element from the front of the queue FIFO (First In First Out)
Queue
Queue Implementation Array implementation of Queue
Queue Application Job Scheduling Priority queues Graph traversals Queuing theory