190 likes | 610 Views
List ADT. Operations Insert into List at front, at end after a specific value, before a specific value Delete from List first value, last value, a specific value Search for a specific value. Implementing a List. Using Vector /Array
E N D
List ADT • Operations • Insert into List • at front, at end • after a specific value, before a specific value • Delete from List • first value, last value, a specific value • Search • for a specific value
Implementing a List • Using Vector /Array • requires estimate of maximum list length • may grow dynamically • Ø = empty slots • Can contain varied data/objects (not necessarily homogeneous) L 212 rules! Golf #1 30−0 Ø Ø Ø
Implementing a List • Using Linked List • flexible, adjusts to problem size • implementing a linked list • nodes and references/links/pointers L Ø 212 rules! Golf #1 30−0
Implementing a List 0 1 2 3 4 5 6 7 8 2 -1 7 -1 1 4 3 6 0 Ø • Using Linked List • implementing a linked list • cursor implementation 212 rules! Ø Ø 30−0 L= 5 Golf #1 Free = 8 Ø Ø Ø
Implementing a List • Linked List • types • singly- vs. doubly-linked • linear vs. circular • with vs. without dummy head node • notes • http://www.cs.clemson.edu/~pargas/courses/cs212/common/notes/txt/Lists.txt
Implementing a List Vector/Array Linked List • insertAtFront O(n) O(1) • insertAtEnd O(1) O(1) • insertInMiddle O(n) O(n) • deleteFromFront O(n) O(1) • deleteFromEnd O(1) O(1) • deleteFromMiddle O(n) O(n) • search • linear O(n) << O(n) • binary O(log n) N/A
Doubly Linked ListMethods void insertAtHead (ListNode node) Node removeFromHead() void insertAtTail (ListNode node) Node removeFromTail() void insertAfter(ListNode node, Object obj)
insertAtHead(node)Case 1: empty list head=/ tail=/ // empty list if (head == null) { head = node; tail = node; } node A
insertAtHead(node) Case 1: empty list head tail=/ // empty list if (head == null) { head = node; tail = node; } node A
insertAtHead(node) Case 1: empty list head tail // empty list if (head == null) { head = node; tail = node; } node A
insertAtHead(node)Case 2: non-empty list tail head C B A // non-empty list else { head.setPrev(node); node.setNext(head); head = node; } D node
insertAtHead(node) Case 2: non-empty list tail head C B A // non-empty list else { head.setPrev(node); node.setNext(head); head = node; } D node
insertAtHead(node) Case 2: non-empty list tail head C B A // non-empty list else { head.setPrev(node); node.setNext(head); head = node; } D node
insertAtHead(node) Case 2: non-empty list tail C B A // non-empty list else { head.setPrev(node); node.setNext(head); head = node; } D node head
insertAtHead(node) Case 2: non-empty list tail C B A D head