Presentation is loading. Please wait.

Presentation is loading. Please wait.

LINKED LISTS CSCD 216 - Linked Lists.

Similar presentations


Presentation on theme: "LINKED LISTS CSCD 216 - Linked Lists."— Presentation transcript:

1 LINKED LISTS CSCD Linked Lists

2 Linked Lists Definition of Linked Lists Examples of Linked Lists
Operations on Linked Lists Linked Lists Implementations Singly-linked lists Doubly-linked lists CS 103

3 Introduction When dealing with many problems we need a dynamic list, dynamic in the sense that the size requirement need not be known at compile time. Thus, the list may grow or shrink during runtime. A linked list is a data structure that is used to model such a dynamic list of data items, so the study of the linked lists as one of the data structures is important. CSCD Linked Lists

4 Linear Data Structure with no restriction
Linked list consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes CSCD Linked Lists

5 Linear Data Structure with no restriction
CSCD Linked Lists

6 Singly Linked Lists A singly linked list is a data structure consisting of a sequence of nodes nodes are not adjacent in memory Each node stores element link to the next node next element node A B C D CSCD Linked Lists

7 Definition Details Each item has a data part (one or more data members), and a link that points to the next item One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list It makes good sense to view each item as an object, that is, as an instance of a class. We call that class: Node The last item does not point to anything. We set its link member to NULL. CS 103

8 Examples of Linked Lists (A Waiting Line)
A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line) A linked list of strings can represent this line: John Mary Dan Sue tail head CS 103

9 Examples of Linked Lists (A Stack of Numbers)
A stack of numbers (from top to bottom): 10, 8, 6, 8, 2 A linked list of ints can represent this stack: 10 8 6 8 2 Tail Head CS 103

10 Examples of Linked Lists (A Sorted Set of Non-redundant Elements)
A set of characters: a, b, d, f, c The elements must be arranged in sorted order: a, b, c, d, f A linked list of chars can represent this set: a b c d f tail_ptr head_ptr CS 103

11 Examples of Linked Lists (A Polynomial)
A polynomial of degree n is the function Pn(x)=a0+a1x+a2x2+…+anxn. The ai’s are called the coefficients of the polynomial The polynomial can be represented by a linked list (2 data members and a link per item): a0,0 a1,1 a2,2 an,n head_ptr tail_ptr CS 103

12 Operations on Linked Lists
Insert a new item At the head of the list, or At the tail of the list, or Inside the list, in some designated position Search for an item in the list The item can be specified by position, or by some value Delete an item from the list Search for and locate the item, then remove the item, and finally adjust the surrounding pointers size( ); isEmpty( ) CS 103

13 Inserting at the Head Allocate a new node Insert new element
Make new node point to old head Update head to point to new node CSCD Linked Lists

14 Inserting at the Tail Allocate a new node Insert new element
Have new node point to null Have old last node point to new node Update tail to point to new node CSCD Linked Lists

15 Inserting inside a linked list
CSCD Linked Lists

16 Removing at the Head Update head to point to next node in the list
Allow garbage collector to reclaim the former first node CSCD Linked Lists

17 Removing at the Tail Removing at the tail of a singly linked list cannot be efficient! There is no constant-time way to update the tail to point to the previous node CSCD Linked Lists

18 Deletion from inside a linked list
CSCD Linked Lists

19 Linked List Implementation of Lists
Need to know where the first node is the rest of the nodes can be accessed No need to move the list for insertion and deletion operations No memory waste CSCD Linked Lists

20 Linked List Implementation of Lists
Linked List Array Traverse List O(N) O(N) Find FindKth (L,i) O(i) O(1) Delete O(1) O(N) Insert CSCD Linked Lists

21 Programming Details There are 3 special cases for linked lists
Insert an element at the front of the list there is no really obvious way Delete an element from the front of the list changes the start of the list Delete an element in general requires to keep track of the node before the deleted one How can we solve these three problems ? CSCD Linked Lists

22 Programming Details Keep a header node in position 0
Write a FindPrevious routine returns the predecessor of the cell To delete the first element FindPrevious routine returns the position of header CSCD Linked Lists

23 Variations of Linked Lists
Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A B C Head CSCD Linked Lists

24 Variations of Linked Lists
Doubly linked lists Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A B C Head CSCD Linked Lists

25 Doubly Linked List Traversing list backwards
not easy with regular lists Insertion and deletion more pointer fixing Deletion is easier Previous node is easy to find CSCD Linked Lists

26 Doubly Linked List A doubly linked list is often more convenient!
Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next elem node trailer header nodes/positions elements CSCD Linked Lists

27 Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C CSCD Linked Lists

28 Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v
link v to its predecessor link v to its successor link p’s old successor to v link p to its new successor, v return v (the position for the element e) CSCD Linked Lists

29 Deletion We visualize remove(p), where p == last() A B C D p A B C p D
CSCD Linked Lists

30 Deletion Algorithm Algorithm remove(p): CSCD Linked Lists

31 Worst-case running time
In a doubly linked list + insertion at head or tail is in O(1) + deletion at either end is on O(1) -- element access is still in O(n) CSCD Linked Lists

32 Array versus Linked Lists
Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of an array is fixed at compilation time. Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers. CSCD Linked Lists


Download ppt "LINKED LISTS CSCD 216 - Linked Lists."

Similar presentations


Ads by Google