Arrays and Linked Lists A list is only










































- Slides: 42

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

Static vs Dynamic 8 Static data structures – Arrays. – Fixed, predetermined capacity. – If full, have to allocate more space and copy values into that space. 8 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 2

Efficiency of Array Operations 8 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 3

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

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

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

Anatomy of Single-Linked List 8 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. head tail A B C CS 221 - Computer Science II null pointer 7

Terminology 8 Node’s successor is the next node in the sequence. – The tail (last node) has no successor. 8 Node’s predecessor is the previous node in the sequence. – The head (first node) has no predecessor. 8 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 8

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

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

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

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

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

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

Insertion: At Beginning 8 Special Case: Empty list. 3. Update head and count variables. 4. Update tail. – Extra Step new. Node head count 1 tail D CS 221 - Computer Science II 15

Insertion: At End 8 Steps: 1. Create a new node with new element. 2. Connect list to new node. 3. Update tail and count variables. 8 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 16

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

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

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

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

Insertion: At End 8 Special Case: Empty list. 3. Update tail and count variables. 4. Update head. – Extra Step new. Node head count 1 tail D CS 221 - Computer Science II 21

Deletion: At Beginning 8 Steps: 1. Remove deleted node from list, have to use temporary variable. 2. Update head variable. 3. Update count variable. 8 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 22

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

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

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

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

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

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

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

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

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

Deletion: At End 8 Steps: 1. Use temporary variable to traverse nodes until reach node before last one. 2. Remove deleted node from list. 3. Update tail variable. 4. Update count variable. 8 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 32

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

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

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

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

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

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

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

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

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

CS 221 - Computer Science II 42