Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212D: Data Structures Week 5-6 Linked List.

Similar presentations


Presentation on theme: "CS212D: Data Structures Week 5-6 Linked List."— Presentation transcript:

1 CS212D: Data Structures Week 5-6 Linked List

2 Outline Singly Linked Lists Doubly Linked Lists Recursions

3 Data Structures and Algorithms
A data structure is a way of storing data in a computer so that it can be used efficiently. Algorithms An algorithm is a finite sequence of well-defined instructions for solving a problem and guaranteed to terminate in a finite time. An algorithm does not necessarily need to be executable in a computer, but can be converted into a program.

4 Algorithms versus Heuristics
A heuristic is a finite sequence of well-defined instructions for partially solving a hard problem and guaranteed to terminate in a finite time. A heuristic is not always guaranteed to find a solution while an algorithm is. Descriptions of algorithms We will use Java-like pseudo code mixed with English to describe algorithms.

5 Abstract Data Types An Abstract Data Type (ADT) is a specification of a set of data and the set of operations that can be performed on the data. Such a data type is abstract in the sense that it is independent of various concrete implementations. The definition can be mathematical, or it can be programmed as an interface. We need to implement an ADT by using a class to make it usable.

6 Linked Lists

7 Singly Linked List linked list, in its simplest form, is a collection of nodes that collectively form a linear sequence. In a singly linked list, each node stores a reference to an object that is an element of the sequence, as well as a reference to the next node of the list. next node elem A B C D

8 Singly Linked List (Example)
The node’s element field refers to an object that is an element of the sequence (the airport code MSP, in this example), while the next field refers to the subsequent node of the linked list (or null if there is no further node). 9-Dec-18 Computer Science Department

9 Singly Linked List The linked list instance must keep a reference to the first node of the list, known as the head. Without an explicit reference to the head, there would be no way to locate that node. The last node of the list is known as the tail. The tail of a list can be found by traversing the linked list 9-Dec-18 Computer Science Department

10 Implementing a single linked list class
9-Dec-18 Computer Science Department

11 Implementing a single linked list class cont.
9-Dec-18 Computer Science Department

12 Implementing a single linked list class cont.
9-Dec-18 Computer Science Department

13 Inserting an Element at the Head of Single Linked List(1/2)
Allocate a new node Insert the new element Have the new node point to old head Update head to point to the new node

14 Inserting an Element at the Head of Single Linked List (2/2)

15 Inserting an Element at the Tail of Single Linked List (1/2)
Allocate a new node Insert the new element Have the new node point to null Have the old last node point to new node Update tail to point to new node

16 Inserting an Element at the Tail of Single Linked List(2/2)

17 Removing an Element from the Head of Single Linked List (1/2)
Update head to point to next node in the list Allow garbage collector to reclaim the former first node

18 Removing an Element from the Head of Single Linked List(2/2)

19 Removing at the Tail (1/2)
Removing the tail node of a singly linked list cannot be done in constant time. We need to traverse the whole linked list to remove the tail node, which takes O(n) time, where n is the number of nodes in the linked list.

20 Removing at the Tail (2/2)
Algorithm removeLast() { if ( size = 0 ) Indicate an error: the list is empty; else { if ( size = 1 ) // only one node head = null; tail=null; else // at least two nodes { t = head; /* make t point to the head */ while ( t.getNext != null ) /* find the last node */ { s = t; t = t.getNext(); } s.setNext(null); //null out the next pointer of the last node } size = size - 1; //decrement the node count

21 Exercises Add a member method called Print to the Single Linked List class, that prints all the list elements. State wither the following sentence is True or False, explain your answer: we cannot easily delete the last node of a singly linked list. 9-Dec-18 Computer Science Department

22 Doubly Linked List A doubly linked list is a linked list in which each node keeps an explicit reference to the node before it and a reference to the node after it. Each node stores: element link to the previous node “prev” link to the next node “next” Special trailer and header nodes prev next elem node

23 Inserting with Double Linked List
Every insertion into our doubly linked list representation will take place between a pair of existing nodes. 9-Dec-18 Computer Science Department

24 Inserting at the Head of the Double Linked List (1/2)
9-Dec-18 Computer Science Department

25 Inserting at the Head of the Double Linked List (1/2)
9-Dec-18 Computer Science Department

26 Inserting an Element in the Middle of the Double Linked List
9-Dec-18 Computer Science Department

27 Inserting an Element at the Trailer
9-Dec-18 Computer Science Department

28 Deletion with Double Linked List
The deletion of the node proceeds in the opposite fashion of an insertion. The two neighbors of the node to be deleted are linked directly to each other, thereby bypassing the original node. As a result, that node will no longer be considered part of the list and it can be reclaimed by the system. 9-Dec-18 Computer Science Department

29 Removing in the Middle 9-Dec-18 Computer Science Department

30 Implementing a Doubly Linked List Class
9-Dec-18 Computer Science Department

31 Implementing a Doubly Linked List Class Cont.
9-Dec-18 Computer Science Department

32 Implementing a Doubly Linked List Class Cont.
9-Dec-18 Computer Science Department

33 Implementing a Doubly Linked List Class Cont.
9-Dec-18 Computer Science Department

34 Exercises Add a member method called PrintOpposite to the Double Linked List class, that prints all the double linked list elements in an opposite order. 9-Dec-18 Computer Science Department

35 References Chapter 3, Data Structures and Algorithms by Goodrich and Tamassia. Garbage Collection by Bill Venners (


Download ppt "CS212D: Data Structures Week 5-6 Linked List."

Similar presentations


Ads by Google