430 likes | 455 Views
Explore the efficiency, advantages, and disadvantages of dynamic and static data structures, focusing on arrays and linked lists. Learn the anatomy, terminology, and operations of single-linked lists.
E N D
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 • 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
List Traversal • Steps: • Initialize temporary variable to head. • Advance temporary variable until find element or position. CS 221 - Computer Science II
List Traversal A B C head tail current • Initialize temporary variable to head. D target 4 count CS 221 - Computer Science II
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
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
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
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
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
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
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
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
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
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
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
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