Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17 Linked List Saurav Karmakar Spring 2007.

Similar presentations


Presentation on theme: "Chapter 17 Linked List Saurav Karmakar Spring 2007."— Presentation transcript:

1 Chapter 17 Linked List Saurav Karmakar Spring 2007

2 LISTS Easiest implementation of LIST --- ARRAY :: has Disadvantages. 1) To insert an item at the beginning or middle of an array  sliding lots of items over one place to make room :: ~ O(n) 2) Arrays have a fixed length :: can't be changed.  Adding items to a list  Allocation a whole new array if the array containing LIST is full and then move all the items from the old array to the new one.

3 Linked List Consists of connected, dynamically allocated nodes. A linked list is made up of "nodes". Each node has two components: an item, and a reference to the next node in the list.

4 Comparison with Array 1.Arrays contiguous direct access of elements insertion / deletion difficult 2.Linked Lists noncontiguous must scan for element insertion /deletion easy 1.Arrays contiguous direct access of elements insertion / deletion difficult 2.Linked Lists noncontiguous must scan for element insertion /deletion easy arrayname

5 for (int i = 0; i < length; i++) cout<< a[i]; for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ; for (int i = 0; i < length; i++) cout<< a[i]; for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ; a Iterating through the data structure

6 class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: Last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2 firstlast Adding an element

7 class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: Last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2 firstlast

8 class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2 firstlast

9 class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2x firstlast

10 class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: Last->next = new ListNode(); last = last->next; Last->data = x; Last->next = null; A0A1A2x firstlast

11 class ListNode { Object element; ListNode* next; } At any point, we can insert a new item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; last Inserting an element A0A1A2 first current

12 class ListNode { Object element; ListNode* next; } At any point, we can insert a new item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; Inserting an element A0A1A2 first last current tmp

13 class ListNode { Object element; ListNode* next; } At any point, we can insert a new item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; Inserting an element A0A1A2 first last current x tmp

14 class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; current->next = tmp; Inserting an element A0A1A2 first last current x tmp

15 class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); Tmp->element = x; Tmp->next = current->next; Current->next = tmp; Inserting an element A0A1A2 first last current x tmp

16 Simplified version Current->next = new ListNode(x, current->next) tmp = new ListNode(); Current->next = tmp; Tmp->next = current->next; Tmp->element = x;

17 class ListNode { Object element; ListNode* next; } Current->next = current->next->next; Deleting an element A0A1A2 current last

18 class ListNode { Object element; ListNode* next; } Current->next = current->next->next; Memory leak! Deleting an element A0A1A2 last current

19 Delete a Node Node *deletedNode = current->next; Current- >next = current->next->next; Delete deletedNode;

20 Length Function The Length function takes a linked list and computes the number of elements in the list. /*Given a linked list head pointer, compute and return the number of nodes in the list.*/ int Length(struct node* head) { struct node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; }

21 abc header Header nodes allow us to avoid special cases [in the code] such as insertion of the first element and removal of the last element. The header node holds no data but serves to satisfy the requirement that every node have a previous node. Not necessarily a standard implementation. Header node

22 abc head tail class DoubleListNode { Object element; ListNode* next; ListNode* prev; } class DoubleListNode { Object element; ListNode* next; ListNode* prev; } A doubly linked list allows bidirectional traversal by storing two pointers per node. Doubly Linked Lists

23 head tail // constructor DoubleList() { head = new DoubleListNode (); tail = new DoubleListNode (); head->next = tail; tail->prev = head; } Empty Doubly Linked List

24 newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail current

25 Inserting into a Doubly Linked List ac head tail newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current

26 Inserting into a Doubly Linked List ac head tail newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current

27 Inserting into a Doubly Linked List ac head tail newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current

28 newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail b current

29 newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail b current

30 newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail b current

31 Circular Linked Lists abcd first

32 Sorted Linked List A sorted link list is one in which items are in sorted order. The major difference from linked list is the insertion operation.


Download ppt "Chapter 17 Linked List Saurav Karmakar Spring 2007."

Similar presentations


Ads by Google