Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI-255 LinkedList.

Similar presentations


Presentation on theme: "CSCI-255 LinkedList."— Presentation transcript:

1 CSCI-255 LinkedList

2 Review Basic Data Structures Related to Linked List
Array contiguous, fixed size at compile time, int a[10]; Dynamic array: contiguous, size not fixed at compile time. However, to increase the size at run time, the whole block of memory has to be created, and the whole block of data in the original dynamic array will be copied to the new dynamic array int *a; int size; cin>> size; a = new int[size];

3 Linked Lists vs. Arrays Linked Lists differ from arrays because they don’t have a fixed size but like arrays can only store elements of the same type Main advantage over arrays is easy insertion and deletion of nodes A well defined list may be the basis for the implementation of several other data structures such as queues, stacks, trees, and graphs

4 Lists as ADTs Data Operations
A collection of elements of the same type Operations IsEmpty IsFull Length Insert Delete IsPresent Print SortedList

5 Two Options to Implement List
Option 1: Use a dynamic array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed Option 2: Use a linked list (to avoid excessive data movement from insertions and deletions) not necessarily stored in contiguous memory locations

6 Self-referential Data Types
info next

7 Linked List A linked list is a list in which the order of the components is determined by an explicit link member in each node Each node contains a component member and also a link member that gives the location of the next node in the list An external pointer (or head pointer) points to the first node in the list

8 Linked List (cont’d) Nodes can be located anywhere in memory
The link member holds the memory address of the next node in the list

9

10 Insertion at the Beginning of the List

11 Insertion at the Beginning of the List (cont’d)
Time Complexity?

12 Insertion at the End of the List

13 Insertion at the End of the List (cont’d)
Time Complexity? Time Complexity if no tail (i.e., only head)?

14 Deletion from the Beginning of the List

15 Deletion from the Beginning of the List (cont’d)
Time Complexity? What if the list is empty?

16 Deletion from the End of the List

17 Deletion from the End of the List (cont’d)
Time Complexity? What if the list is empty?

18 Deleting from Anywhere in the List

19 Deleting from Anywhere in the List (cont’d)

20 Deleting from Anywhere in the List (cont’d)
Best case time complexity? Worst case time complexity? Average case time complexity? (done in class)

21 Comparison: Array, Dynamic Array, LinkedList
Indexing Insert at the beginning Insert at the end Delete at the beginning Delete at the end Insert/Delete in the middle Array O(1) N/A Dynamic Array O(n) LinkedList (with head) LinkedList (with head and tail)


Download ppt "CSCI-255 LinkedList."

Similar presentations


Ads by Google