Linked List Stacks Queues Linked Lists 2 Linked

  • Slides: 27
Download presentation
Linked List, Stacks Queues

Linked List, Stacks Queues

Linked Lists 2

Linked Lists 2

Linked Lists • Used for modelling lists. • Different to array. Lists • Can

Linked Lists • Used for modelling lists. • Different to array. Lists • Can only add/remove at the start and end of a list.

Methods for single Linked list • public void add. First(String. Node n) • public

Methods for single Linked list • public void add. First(String. Node n) • public void remove. First() • public void add. Last(String. Node node) • public void remove. Last() {

Singly Linked List node • A singly linked list is a concrete data structure

Singly Linked List node • A singly linked list is a concrete data structure consisting of a sequence of nodes • Each node stores: next – an element – a link to the next node elem A B C Linked Lists D 5

The String. Node Class for List Nodes public class String. Node { // class

The String. Node Class for List Nodes public class String. Node { // class Attributes – data instances private String element; // The data to be stored in this node IE. A STRING private String. Node next. Node; // A link to the next node in the chain IE A // POINTER or object reference /* Constructor Creates a node with the given element and next node. */ public String. Node(String e, String. Node n) { element = e; next. Node = n; } /* Constructor Creates a node with null references to its element and next node*/ public String. Node() { this(null, null); // call the construtor above i. e. public // String. Node(String e, String. Node n) }

The String. Node Class for List Nodes public String get. Element() // Accessor method

The String. Node Class for List Nodes public String get. Element() // Accessor method { return element; } public String. Node get. Next() // Accessor method { return next. Node; } public void set. Element(String new. Elem) // Modifier methods: { element = new. Elem; } public void set. Next(String. Node new. Next) // Modifier methods: { next. Node = new. Next; }

For any data structure/storage: Operations Location of Operation • How to add • Beginning

For any data structure/storage: Operations Location of Operation • How to add • Beginning • How to retreive • End • How to remove/delete. • Middle Linked Lists 8

Inserting at the Head of a List 1. The current list. . . head

Inserting at the Head of a List 1. The current list. . . head A 2. 3. Create a new node with content X node B C X Insert the node A. X Have new node point to old head B. Update head to point to new node A B C node head X Linked Lists A B C 9

String. Linked. List data members public class String. Linked. List { String. Node head

String. Linked. List data members public class String. Linked. List { String. Node head = null; // start of the list String. Node tail = null; // last element in the list

Linked List – add. First method public void add. First(String. Node n) { if

Linked List – add. First method public void add. First(String. Node n) { if (n == null) // Check we have a node to add. . . return; // Set our new node to point to the head of the current list. n. set. Next(head); // Our new node 'n' will now become the head of the list head = n; // If the list was empty, make the tail point to // the new node as well if (tail == null) tail = n; } Linked Lists 11

Removing from the Head head A 1. 2. Update head to point to next

Removing from the Head head A 1. 2. Update head to point to next node in the list B C D head A Allow garbage collector to reclaim the former first node B C D head A Linked Lists B C D 12

Linked List – remove. First method public void remove. First() { // If list

Linked List – remove. First method public void remove. First() { // If list is empty, we can't remove anything so leave if (head == null) return; // Move head to the next item in the list head = head. get. Next(); // If the list is empty, set the tail reference to null if (head == null) tail = null; // The original item that was at the head of the list // no longer has anything referencing it and will be // garbage collected in Java. In other programming languages // e. g. C++, you would have to delete it other wise you // would get a memory leak. }

Inserting at the Tail tail 1. The current list. . . head A 2.

Inserting at the Tail tail 1. The current list. . . head A 2. 3. Create a new node pointing to null node B X Insert new element A. B. Have old last node point to new node C node tail X head A B C node Update tail to point to new node tail head A Linked Lists B C X 14

Linked List – add. Last method public void add. Last(String. Node node) { //

Linked List – add. Last method public void add. Last(String. Node node) { // If we were not given a node, leave if (node == null) return; // If list is empty, our new node will // be the head and tail of list if (head == null) { head = node; tail = node; return; } // Make the current last node point to our new node tail. set. Next(node); // Now update the tail to be our new node tail = node; Linked Lists } 15

Removing at the Tail • Removing at the tail of a singly linked list

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 tail head Linked Lists A B C D 16

Linked List – remove. Last method • • • public void remove. Last() {

Linked List – remove. Last method • • • public void remove. Last() { if (head == null) return; // If list is empty, leave • • // If head is also the tail, the list // will be empty if (head == tail) { head = null; tail = null; return; } • • // Start at the head of the list String. Node n = head; • • • // Now look for the last item while (n. get. Next() != tail) n = n. get. Next(); • • • // n should now be pointing to the last but one // node in the list. This will be the new tail // We are going to drop the last element in the list // so make the current node's next pointer null n. set. Next(null); • • // The old tail node is now replaced with 'n'. The // old tail node has no reference and will be garbage tail = n; } Linked Lists 17

Linked List – remove. Last method public void remove. Last() { if (head ==

Linked List – remove. Last method public void remove. Last() { if (head == null) return; // If list is empty, leave // If head is also the tail, the list // will be empty if (head == tail) { head = null; tail = null; return; } // Start at the head of the list String. Node n = head; Linked Lists 18

Linked List – remove. Last method // Now look for the last item while

Linked List – remove. Last method // Now look for the last item while (n. get. Next() != tail) n = n. get. Next(); // n should now be pointing to the last but one // node in the list. This will be the new tail // We are going to drop the last element in the list // so make the current node's next pointer null n. set. Next(null); // The old tail node is now replaced with 'n'. The // old tail node has no reference and will be garbage tail = n; } Linked Lists 19

Stack

Stack

queue

queue

Doubly Linked List 1. A doubly linked list provides a natural implementation of the

Doubly Linked List 1. A doubly linked list provides a natural implementation of the Node List 2. Nodes implement Position and store: 1. element 2. link to the previous node 3. link to the next node prev next elem node 3. Special trailer and header nodes/positions header trailer elements Lists 24

Insertion • We visualize operation insert. After(p, X), which returns position q p A

Insertion • We visualize operation insert. After(p, X), which returns position q p A B C p A q B C X p A q B Lists X C 25

Insertion Algorithm add. After(p, e): Create a new node v v. set. Element(e) v.

Insertion Algorithm add. After(p, e): Create a new node v v. set. Element(e) v. set. Prev(p) {link v to its predecessor} v. set. Next(p. get. Next()) {link v to its successor} (p. get. Next()). set. Prev(v) {link p’s old successor to v} p. set. Next(v) {link p to its new successor, v} return v {the position for the element e} Lists 26

Deletion • We visualize remove(p), where p = last() A B C p D

Deletion • We visualize remove(p), where p = last() A B C p D A B Lists C 27

Deletion Algorithm remove(p): t = p. element {a temporary variable to hold the return

Deletion Algorithm remove(p): t = p. element {a temporary variable to hold the return value} (p. get. Prev()). set. Next(p. get. Next()) {linking out p} (p. get. Next()). set. Prev(p. get. Prev()) p. set. Prev(null) {invalidating the position p} p. set. Next(null) return t Lists 28

Performance q In the linked list implementation of the List ADT, q. The space

Performance q In the linked list implementation of the List ADT, q. The space used by a list with n elements is O(n) n The space used by each position of the list is O(1) n Operations of the List ADT run in up to O(n) time Lists 29