Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequences 8/2/2018 12:16 AM Linked Lists Linked Lists.

Similar presentations


Presentation on theme: "Sequences 8/2/2018 12:16 AM Linked Lists Linked Lists."— Presentation transcript:

1 Sequences 8/2/ :16 AM Linked Lists Linked Lists

2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem A B C D Linked Lists

3 The Node Class for List Nodes
public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } } Elements: The elements are of type <Type>. Note: To implement encapsulation, we can have getter/setter of data and next here. Linked Lists

4 Inserting at the Head Allocate a new node Insert new element
Have new node point to old head Update head to point to new node Linked Lists

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

6 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 Linked Lists

7 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 Linked Lists

8 For better understanding
Linked Lists


Download ppt "Sequences 8/2/2018 12:16 AM Linked Lists Linked Lists."

Similar presentations


Ads by Google