1 / 42

Arrays and Linked Lists

Arrays and Linked Lists. "A list is only as strong as its weakest link." -Donald Knuth. Static vs Dynamic. Static data structures Arrays. Fixed, predetermined capacity. If full, have to allocate more space and copy values into that space. Dynamic data structures

wroach
Download Presentation

Arrays and Linked Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and Linked Lists "A list is only as strong as its weakest link." -Donald Knuth

  2. Static vs Dynamic • Static data structures • Arrays. • Fixed, predetermined capacity. • If full, have to allocate more space and copy values into that space. • Dynamic data structures • Linked structures, like linked lists and trees. • They grow / shrink one element at a time. • Avoid some inefficiencies of static containers. CS 221 - Computer Science II

  3. Efficiency of Array Operations • Big-O of Array Operations • To insert element at beginning of an array? • Space available? • No space available? • While maintaining relative order? • To insert element at the end of array? • To insert or delete an element in the middle of the array? • To access the kth element? CS 221 - Computer Science II

  4. Advantages of Arrays • Used to represent multiple data items of same type by using only single name. • Can be used to implement other data structures like stacks, queues, trees, graphs, etc. • 2D arrays are used to represent matrices. CS 221 - Computer Science II

  5. Disadvantages of Arrays • Array is static structure, has fixed size. • Must know in advance that how many elements need to be stored. • If allocate more memory than required, the memory space will be wasted. • If we allocate less memory than required, it will create problem. • Elements of array stored in consecutive memory locations, so insertions and deletions are time consuming. CS 221 - Computer Science II

  6. Single-Linked List • Single-linked list is implementation-dependent data structure. • Not defined by set of core operations. • Have to understand how to manipulate nodes. • Each node stores reference to element in linked list. • Nodes also maintain references to next node in the list. • Create new node when add new element to list. • Remove node when element is removed from list. • Allow garbage collector to reclaim that memory. CS 221 - Computer Science II

  7. Anatomy of Single-Linked List A B C head tail • A linked list consists of: • Sequence of nodes, can change during execution. • Successive nodes connected by node references. • Last node reference points to null. • Grows / shrinks during operation. • Not limit number of elements. • Keep references to first / last (optional) nodes in list. null pointer CS 221 - Computer Science II

  8. Terminology • Node’s successor is the next node in the sequence. • The tail (last node) has no successor. • Node’s predecessor is the previous node in the sequence. • The head (first node) has no predecessor. • List’s size is the number of elements in it. • A list may be empty (i.e. contain no elements). CS 221 - Computer Science II

  9. Single-Linked List Operations • Inserting New Elements • Inserting at beginning of list • Inserting at end of list • Inserting into middle of list • Deleting Elements • Deleting element from the beginning of list • Deleting element from end of list • Deleting element from middle of list • Traversing List CS 221 - Computer Science II

  10. Insertion: At Beginning • Steps: • Create a new node with new element. • Connect new node to list. • Update head and count variables. • Special Case: if empty • Same steps but have to update tail. CS 221 - Computer Science II

  11. Insertion: At Beginning A B C head tail newNode • General Case: One or more elements in list. • Create a new node with new element. 3 count D CS 221 - Computer Science II

  12. Insertion: At Beginning A B C head tail newNode • General Case: One or more elements in list. • Connect new node to list. 3 count D CS 221 - Computer Science II

  13. Insertion: At Beginning A B C head tail newNode • General Case: One or more elements in list. • Update head and count variables. 4 count D CS 221 - Computer Science II

  14. Insertion: At Beginning head tail newNode • Special Case: Empty list. • Create a new node with new element. • Connect new node to list. 0 count D null CS 221 - Computer Science II

  15. Insertion: At Beginning head tail newNode • Special Case: Empty list. • Update head and count variables. • Update tail. – Extra Step 1 count D CS 221 - Computer Science II

  16. Insertion: At End • Steps: • Create a new node with new element. • Connect list to new node. • Update tail and count variables. • Special Case: if empty • Most of same steps but can’t connect list to new node and have to update head. CS 221 - Computer Science II

  17. Insertion: At End A B C head tail newNode • General Case: One or more elements in list. • Create a new node with new element. 3 count D CS 221 - Computer Science II

  18. Insertion: At End A B C head tail newNode • General Case: One or more elements in list. • Connect list to new node. 3 count D CS 221 - Computer Science II

  19. Insertion: At End A B C head tail newNode • General Case: One or more elements in list. • Update tail and count variables. 4 count D CS 221 - Computer Science II

  20. Insertion: At End head tail newNode • Special Case: Empty list. • Create a new node with new element. • Can’t connect list to new node, because tail is null. – Remove Step 0 count D null CS 221 - Computer Science II

  21. Insertion: At End head tail newNode • Special Case: Empty list. • Update tail and count variables. • Update head. – Extra Step 1 count D CS 221 - Computer Science II

  22. Deletion: At Beginning • Steps: • Remove deleted node from list, have to use temporary variable. • Update head variable. • Update count variable. • Special Case: if only one node left • Most of same steps but don’t need temporary variable and have to update tail. CS 221 - Computer Science II

  23. Deletion: At Beginning B C D head tail next • General Case: Two or more elements in list. • Remove deleted node from list, have to use temporary variable. 4 count A CS 221 - Computer Science II

  24. Deletion: At Beginning B C D head tail next • General Case: Two or more elements in list. • Update head variable. 4 count A CS 221 - Computer Science II

  25. Deletion: At Beginning B C D head tail next • General Case: Two or more elements in list. • Update count variable. 3 count CS 221 - Computer Science II

  26. Deletion: At Beginning head tail • Special Case: One node left. • Remove deleted node from list. • Update head variable. • Update count variable. • Update tail. – Extra Step 0 count A null CS 221 - Computer Science II

  27. Deletion: At End • To delete last element, have to update link from node previous to last node. • In order to update this link, have to traverse nodes to that node. CS 221 - Computer Science II

  28. List Traversal • Steps: • Initialize temporary variable to head. • Advance temporary variable until find element or position. CS 221 - Computer Science II

  29. List Traversal A B C head tail current • Initialize temporary variable to head. D target 4 count CS 221 - Computer Science II

  30. List Traversal A B C head tail current • Advance temporary variable until find element or position. D target 4 count CS 221 - Computer Science II

  31. List Traversal A B C head tail current • Advance temporary variable until find element or position. D target 4 count CS 221 - Computer Science II

  32. Deletion: At End • Steps: • Use temporary variable to traverse nodes until reach node before last one. • Remove deleted node from list. • Update tail variable. • Update count variable. • Special Case: if only one node left • Most of same steps but don’t have to traverse nodes and have to update head. CS 221 - Computer Science II

  33. Deletion: At End B C D head current tail • General Case: Two or more elements in list. • Use temporary variable to traverse nodes until reach node before last one. 4 count A CS 221 - Computer Science II

  34. Deletion: At End B C D head current tail • General Case: Two or more elements in list. • Use temporary variable to traverse nodes until reach node before last one. 4 count A CS 221 - Computer Science II

  35. Deletion: At End B C D head current tail • General Case: Two or more elements in list. • Use temporary variable to traverse nodes until reach node before last one. 4 count A CS 221 - Computer Science II

  36. Deletion: At End B C D head current tail • General Case: Two or more elements in list. • Remove deleted node from list. 4 count A CS 221 - Computer Science II

  37. Deletion: At End B C D head current tail • General Case: Two or more elements in list. • Update tail variable. 4 count A CS 221 - Computer Science II

  38. Deletion: At End B C head current tail • General Case: Two or more elements in list. • Update count variable. 3 count A CS 221 - Computer Science II

  39. Deletion: At End head tail • Special Case: One node left. • Remove deleted node from list. • Update tail variable. • Update count variable. • Update head. – Extra Step 0 count D null CS 221 - Computer Science II

  40. Advantages of Linked Lists • Insertions / deletions don’t require shifting. • No wasted space. • Size is not fixed, can be extended or reduced. • Elements do not have to be stored in consecutive memory. CS 221 - Computer Science II

  41. Disadvantages of Linked Lists • Requires more space – need references to successor and stored elements. • No direct access to elements by position. • To find a particular element, have to go through all those elements that come before that element. • We can only traverse from the beginning. • Sorting the elements stored in linked list are more difficult and inefficient. CS 221 - Computer Science II

  42. CS 221 - Computer Science II

More Related