Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Linked Lists

Similar presentations


Presentation on theme: "Arrays and Linked Lists"— 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 Dynamic 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 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 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 Computer Science II

5 Disadvantages of Arrays
Array is static structure. Arrays are of fixed size. We must know in advance that how many elements are to be stored in array. Since array has fixed size, if allocate more memory than required, the memory space will be wasted. And if we allocate less memory than required, it will create problem. The elements of array are stored in consecutive memory locations. So insertions and deletions are very difficult and time consuming. CS 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 Computer Science II

7 Anatomy of Single-Linked List
A linked list consists of: A sequence of nodes, which can change during execution. Successive nodes are connected by node references. Last node reference points to null. Grows / shrinks during operation. Not limit number of elements. Keep references to first / last nodes in list. head A B C tail CS 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 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

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.

11 Insertion: At Beginning
General Case: One or more elements in list. Create a new node with new element. new node count 3 D head A B C tail

12 Insertion: At Beginning
General Case: One or more elements in list. Connect new node to list. count 3 new node head A B C tail D

13 Insertion: At Beginning
General Case: One or more elements in list. Update head and count variables. count 4 new node head A B C tail D

14 Insertion: At Beginning
Special Case: Empty list. Create a new node with new element. Connect new node to list (no change). new node count D head null tail

15 Insertion: At Beginning
Special Case: Empty list. Update head and count variables. Update tail. new node count 1 head tail D

16 Insertion: At End Steps: Special Case: if empty
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.

17 Insertion: At End General Case: One or more elements in list.
Create a new node with new element. new node count 3 D head A B C tail

18 Insertion: At End General Case: One or more elements in list.
Connect list to new node. count 3 new node tail head A B C D

19 Insertion: At End General Case: One or more elements in list.
Update tail and count variables. count 4 tail new node head A B C D

20 Insertion: At End Special Case: Empty list.
Create a new node with new element. Can’t connect list to new node, because tail is null. new node count D head null tail

21 Insertion: At End Special Case: Empty list.
Update head and count variables. Update tail. new node count 1 head tail D

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.

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

24 Deletion: At Beginning
General Case: Two or more elements in list. Update head variable. 4 head count next B C D tail A

25 Deletion: At Beginning
General Case: Two or more elements in list. Update count variable. head count 3 next B C D tail

26 Deletion: At Beginning
Special Case: One node left. Remove deleted node from list (no change). Update head variable. Update count variable. Update tail. count A head null tail

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.

28 List Traversal Steps: Initialize temporary variable to head.
Advance temporary variable until find element or position.

29 List Traversal Initialize temporary variable to head. target A B C D 4
current tail head A B C D target count 4

30 List Traversal Advance temporary variable until find element or position. current tail head A B C D target count 4

31 List Traversal Advance temporary variable until find element or position. current tail head A B C D target count 4

32 Deletion: At End Steps: Special Case: if only one node left
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.

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

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

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

36 Deletion: At End General Case: Two or more elements in list.
Remove deleted node from list. count 4 current tail head B C D A

37 Deletion: At End General Case: Two or more elements in list.
Update tail variable. count 4 current tail head B C D A

38 Deletion: At End General Case: Two or more elements in list.
Update count variable. count 3 current tail head B C A

39 Deletion: At End Special Case: One node left.
Remove deleted node from list (no change). Update tail variable. Update count variable. Update head. count D head null tail

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 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 Computer Science II

42 CS 221 - Computer Science II


Download ppt "Arrays and Linked Lists"

Similar presentations


Ads by Google