Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 of Computer Science II

Similar presentations


Presentation on theme: "Lecture 6 of Computer Science II"— Presentation transcript:

1 Lecture 6 of Computer Science II
Linked Lists

2 Definition LINKED LISTS
A sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. A linked list, in its simplest form, is a collection of nodes that together form a linear ordering. The first and last node of a linked list usually are called the head and tail of the list, respectively. Thus, we can link hop through the list starting at the head and ending at the tail. We can identify the tail as the node having a null next reference, which indicates the end of the list.  Page 2

3 Definition LINKED LISTS
A linked list is a self-referential datatype because it contains a pointer or link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time,[1] but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists.  Page 3

4 Applications LINKED LISTS Navigating pages in a novel.
Presenting test questions. Moving through a list of user settings on an application. head tail  Page 4

5 Vs. Arrays LINKED LISTS Space usage: Indexing Insertion Deletion tail
head tail The nicest thing about Linked Lists is that they are always whatever size you need them to be. Memory is conserved with linked lists in two ways: Linked lists avoid the inefficiencies of array capacity operations. Adding data to a linked list only involves one new node. Adding to an array beyond capacity requires a new, larger array to be created. Then, each element is copied to the bigger array. Once the elements are done copying, there are still many open spaces with no data, created to avoid re-copying. Linked lists also avoid wasted space in the middle; Nodes taken out of the sequence are dealt with by Java's automatic garbage collection utility  Page 5

6 Definition SINGLY LINKED LISTS
A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node head tail The singly-linked list is the most basic of all the linked data structures. A singly-linked list is simply a sequence of dynamically allocated objects, each of which refers to its successor in the list.  Page 6

7 Implementing a Singly Linked List
SINGLY LINKED LISTS Implementing a Singly Linked List To implement a singly linked list, we define a Node class,, which specifies the type of objects stored at the nodes of the list. Given the Node class, we can define a class, SLinkedList,, defining the actual linked list. This class keeps a reference to the head node and a variable counting the total number of nodes.  Page 7

8 Inserting at the Head SINGLY LINKED LISTS 1. Allocate a new node
2. Insert new element 3. Have new node point to old head 4. Update head to point to new node  Page 8

9 Removing the node at the beginning of a singly linked list.
SINGLY LINKED LISTS Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node Removing the node at the beginning of a singly linked list.  Page 9

10 Inserting a new node at the end of a singly linked list.
SINGLY LINKED LISTS Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node Inserting a new node at the end of a singly linked list. This method works also if the list is empty. Note that we set the next pointer for the old tail node before we make variable tail point to the new node  Page 10

11 SINGLY LINKED LISTS 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 Unfortunately, we cannot easily delete the tail node of a singly linked list. Even if we have a tail reference directly to the last node of the list, we must be able to access the node before the last node in order to remove the last node. But we cannot reach the node before the tail by following next links from the tail. The only way to access this node is to start from the head of the list and search all the way through the list. But such a sequence of link hopping operations could take a long time.  Page 11

12 DOUBLY LINKED LISTS Doubly Linked Lists There is a type of linked list that allows us to go in both directions—forward and reverse—in a linked list. It is the doubly linked list. A node in a doubly linked list stores two references: next link, which points to the next node in the list, and a prev link, which points to the previous node in the list. Java class DNode representing a node of a doubly linked list that stores a character string.  Page 12

13 Header and Trailer Sentinels
DOUBLY LINKED LISTS Header and Trailer Sentinels 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 "dummy" or sentinel nodes do not store any elements. The header has a valid next reference but a null prev reference, while the trailer has a valid prev reference but a null next reference.  Page 13

14 Removing the last node of a doubly linked list.
DOUBLY LINKED LISTS Removing the node at the end of a doubly linked list Inserting or removing elements at either end of a doubly linked list is straight- forward to do. Indeed, the prev links eliminate the need to traverse the list to get to the node just before the tail. Removing the last node of a doubly linked list. Variable size keeps track of the current number of elements in the list. Note that this method works also if the list has size one.  Page 14

15 Inserting a new node v at the beginning of a doubly linked list.
DOUBLY LINKED LISTS Adding an element at the front Likewise, we can easily perform an insertion of a new element at the beginning of a doubly linked list, as shown in the following Figure. Inserting a new node v at the beginning of a doubly linked list. Variable size keeps track of the current number of elements in the list. Note that this method works also on an empty list.  Page 15

16 Insertion in the Middle of a Doubly Linked List
DOUBLY LINKED LISTS Insertion in the Middle of a Doubly Linked List Given a node v of a doubly linked list (which could be possibly the header but not the trailer), we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute 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 Inserting a new node z after a given node v in a doubly linked list..  Page 16

17 Removal in the Middle of a Doubly Linked List
DOUBLY LINKED LISTS Removal in the Middle of a Doubly Linked List We access the nodes u and w on either side of v using v's getPrev and getNext methods. To remove node v, we simply have u and w point to each other instead of to v. We refer to this operation as the linking out of v. We also null out v's prev and next pointers so as not to retain old references into the list. Removing a node v in a doubly linked list. This method works even if v is the first, last, or only nonsentinel node  Page 17

18 Circularly Linked Lists
A circularly linked list has the same kind of nodes as a singly linked list. 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, which we call the cursor. The cursor node allows us to have a place to start from if we ever need to traverse a circularly linked list. And if we remember this starting point, then we can also know when we are done-we are done with a traversal of a circularly linked list when we return to the node that was the cursor node when we started.  Page 18

19 Circularly Linked Lists
We can then define some simple update methods for a circularly linked list: 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); if the list becomes empty, the cursor is set to null. advance(): Advance the cursor to the next node in the list. Look at the Java implementation of a circularly linked list Page No. 153 (Text Book)  Page 19

20 Circularly Linked Lists
Sorting a Linked List Following is the pseudo code for the insertion-sort algorithm for a doubly linked list. Insertion Sort For A doubly-linked List  Page 20


Download ppt "Lecture 6 of Computer Science II"

Similar presentations


Ads by Google