Download presentation
Presentation is loading. Please wait.
1
EEL 4854 IT Data Structures Linked Lists
8/2/ :17 AM EEL 4854 IT Data Structures Linked Lists
2
Linked Lists Singly Linked Lists Doubly Linked Lists
8/2/ :17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists
3
Linked Lists Singly Linked Lists Doubly Linked Lists
8/2/ :17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists
4
8/2/ :17 AM Problems with Arrays Arrays are convenient for storing objects in a certain order. However, the size of an array must be known in advance. Linked lists do not suffer form this drawback.
5
8/2/ :17 AM Linked Lists A linked list is a collection of nodes that together form a linear ordering. The ordering is such that each node is an object that stores a reference to an element and a reference, called next, to another node. The first and last node of a linked list are called the head and tail of the list respectively. next node elem A B C D
6
List Node Implementation
8/2/ :17 AM List Node Implementation
7
Linked List Implementation
8/2/ :17 AM Linked List Implementation
8
Insertion in a Linked List
8/2/ :17 AM Insertion in a Linked List It is easy to insert an element at the head and tail of the list. All we need to do is to: create a node set its next link to refer to the same object as head set head to point to the new node.
9
Insertion at the Head Algorithm addFirst(v): v.setNext(head)
8/2/ :17 AM Insertion at the Head Algorithm addFirst(v): v.setNext(head) //make v point to the old head node head v //make variable head point to new node size size + 1 // increment node count
10
Insertion at the Tail Inserting at the tail can be done as follows:
8/2/ :17 AM Insertion at the Tail Inserting at the tail can be done as follows: Allocate a new node Insert the new element Have the new node point to null Have the old last node point to the new node Update the tail to point to the new node
11
Insertion at the Tail Algorithm addLast(v): v.setNext(null)
8/2/ :17 AM Insertion at the Tail Algorithm addLast(v): v.setNext(null) //make new node v point to null object tail.setNext(v) // make old tail node point to new node tail v //make variable tail point to new node size size + 1 // increment node count
12
8/2/ :17 AM Removing at the Head The reverse operation of inserting a new element a the head of a linked list is to remove an element at the head.
13
Removing at the Head Algorithm removeFirst(): if head = null then
8/2/ :17 AM Removing at the Head Algorithm removeFirst(): if head = null then Indicate an error: list is empty t head head head.getNext() // make head point to next node (or null) t.setNext(null) // null out next pointer of removed node size size – 1 // decrement node count
14
8/2/ :17 AM Removing at the Tail Removing at the tail of a singly linked list is not efficient. There is no constant-time way to update the tail to point to the previous node.
15
Linked Lists Singly Linked Lists Doubly Linked Lists
8/2/ :17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists
16
8/2/ :17 AM Doubly linked Lists It is time consuming to remove any node other than the head in a singly linked list. There is a type of list, called doubly linked list, that allows to go in both directions in a linked list. Such list allows quick insertions and removals at both ends and in the middle of the list.
17
8/2/ :17 AM Doubly Linked Lists A doubly linked list provides a natural implementation of the List ADT. Nodes consist of: element link to the previous node link to the next node There are also special trailer and header nodes in the list. prev next elem node trailer header nodes/positions elements
18
List Node Implementation
8/2/ :17 AM List Node Implementation
19
Header and Trailer Nodes
8/2/ :17 AM Header and Trailer Nodes To simplify programming, it is convenient to add special nodes at both ends of a doubly linked list: a header node just before the head of the list, and a trailer node just after the tail of the list. These sentinel nodes do not store any elements. The header has a valid next reference but a null prev reference. The trailer has a valid prev reference but a null next reference.
20
Removing at the End of a List
8/2/ :17 AM Removing at the End of a List We visualize remove(p), where p = last() A B C D p A B C D p A B C
21
Removing at the End of a List
8/2/ :17 AM Removing at the End of a List
22
Inserting at the Head of a List
8/2/ :17 AM Inserting at the Head of a List
23
Inserting at the Head of a List
8/2/ :17 AM Inserting at the Head of a List
24
Inserting in the Middle of a List
8/2/ :17 AM Inserting in the Middle of a List Let w be the node following v in the linked list. Assume, we would like to insert node z after node v. We need to complete the following steps: make z’s prev link refer to v make z’s next link refer to w make w’s prev link refer to z make v’s next link refer to z
25
Inserting in the Middle of a List
8/2/ :17 AM Inserting in the Middle of a List We visualize operation insertAfter(p, X), which returns position q p A B C A B C p X q A B X C p q
26
Inserting in the Middle of a List
8/2/ :17 AM Inserting in the Middle of a List
27
Removing in the Middle of a List
8/2/ :17 AM Removing in the Middle of a List Assume node v is located between nodes u and w. To remove node v from the middle of the list, we do the following: Have u and w point to each other instead of v. This is called linking out of v. Null out v’s prev and next pointers so as not to retain old references into the list.
28
Removing in the Middle of a List
8/2/ :17 AM Removing in the Middle of a List
29
Removing in the Middle of a List
8/2/ :17 AM Removing in the Middle of a List
30
Linked Lists Singly Linked Lists Doubly Linked Lists
8/2/ :17 AM Linked Lists Singly Linked Lists Doubly Linked Lists Circularly Linked Lists
31
Circularly Linked Lists
8/2/ :17 AM Circularly Linked Lists A circularly linked list has the same kind of nodes as a singly linked list, that is each node has a next pointer and a reference to an element. But, there is no head or tail in a circularly linked list. For instead of having the last node’s next pointer be null, in a circularly linked list, it points back to the first node. Even though a circularly linked list has no beginning or end, we nevertheless need some node to be marked as a special node, called cursor, from which to start if we ever need to traverse the list.
32
Circularly Linked List Operations
8/2/ :17 AM Circularly Linked List Operations The following are simple update methods for circularly linked lists: add(v): Insert a new node v immediately after the cursor; if the list is empty, then v becomes the cursor and its next pointer points to itself. remove(): remove and return the node v immediately after the cursor (not the cursor itself, unless it is the only node in the list); if the list becomes empty, the cursor is set to null. advance(): Advance the cursor to the next node in the list.
33
Implementation of a Circularly Linked List
8/2/ :17 AM Implementation of a Circularly Linked List
34
-Fin- Linked Lists
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.