11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains two parts: data and link. The name of the list is the same as the name of this pointer variable. Figure 11.9 shows a linked list called scores that contains four elements. We define an empty linked list to be only a null pointer: Figure 11.9 also shows an example of an empty linked list.
Figure 11.9 Linked lists
Abstract Data Types (ADT) Definition : Is a set of operation Mathematical abstraction No implementation detail Example : Lists, sets, graphs, stacks are examples of ADT along with their operations
THE LIST ADT Ordered sequence of data items called elements A1, A2, A3, …,AN is a list of size N size of an empty list is 0 Ai+1 succeeds Ai Ai-1 preceeds Ai position of Ai is i first element is A1 called “head” last element is AN called “tail” Operations ?
Outline Linked list nodes Linked list operations Insertion Append Deletion Linked list representation & implementation Other types of linked lists Sorted Doubly-linked Circular 1st March 2007
Array vs Linked List node node Array node Linked List
Linked List Implementation of Lists Series of nodes not adjacent in memory contain the element and a pointer to a node containing its succesor Avoids the linear cost of insertion and deletion !
Linked List Implementation of Lists Series of nodes not adjacent in memory contain the element and a pointer to a node containing its succesor Avoids the linear cost of insertion and deletion !
Linked List Implementation of Lists Linked List Array PrintList O(N) (traverse the list) O(N) Find FindKth (L,i) O(i) O(1) Delete O(1) O(N) Insert
Doubly Linked List Traversing list backwards not easy with regular lists Insertion and deletion more pointer fixing Deletion is easier Previous node is easy to find
Circulary Linked List Last node points the first
Cursor Implementation of Linked List If L = 5, then L represents list (A, B, E) If M = 3, then M represents list (C, D, F)
Linked List: Insertion a b c d current Insert X immediately after current position a b c d x current 1st March 2007
Inserting a Node When inserting a node in the middle of a linked list, we must first find the spot to insert it Let current refer to the node before the spot where the new node will be inserted
Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b x current tmp 1st March 2007
Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; a b x current tmp 1st March 2007
Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current tmp 1st March 2007
Implementing Insertion: Shortest Version An even shorter version: // create a new node current.next = new ListNode<DataType>(x,current.next); a b x current 1st March 2007
Inserting a Node A method called insert could be defined to add a node anywhere in the list, to keep it sorted, for example Inserting at the front of a linked list is a special case
Implementing Basic Deletion Delete an item immediately after current position Basic deletion is a bypass in the linked list. a x b current a b current 1st March 2007
Other Linked Lists Doubly-linked lists: Each list node stores both the previous and next nodes in the list. Useful for traversing linked lists in both directions. Circular-linked lists: Last node's next references the first node. Works with or without headers. A head tail prev next prev A B C next first 1st March 2007