Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Doubly-Linked Lists, Exercises of Data Structures and Algorithms

LinkedList has a field that refers to the last node in the list. Java's LinkedList remove(int index) method runtime is O(1). How is it possible even if your ...

Typology: Exercises

2021/2022

Uploaded on 09/12/2022

shahid_88c
shahid_88c 🇺🇸

4.4

(26)

261 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Doubly-Linked Lists
15-121 Fall 2020
Margaret Reid-Miller
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Doubly-Linked Lists and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Doubly-Linked Lists

15 - 121 Fall 2020

Margaret Reid-Miller

Today

  • Exam 1: mean 86, high 98

Amazing!!

  • Hw6 will be posted tonight
    • Change Hw due dates?

Plan for Today

  • Singly-linked list removed at index method
  • Java's LinkedList class
  • Doubly-linked lists

Exercise: Remove at given index

// Removes the object at the given index.

// Returns the object removed

public E remove(int index) { if (index < 0 || index > size()) throw new IndexOutOfBoundsException();

Remove at index 3 goal

first remove here (index = 3) null 2 obj

Remove at given index (cont’d)

Node nodeBefore = first; for (int i = 0; ; i++) { nodeBefore = nodeBefore.next; } // remove the node … size--; return obj; }

Remove at index 3

first remove here (index = 3) current

WRONG

  1. current.next = current.next.next;
  2. E obj = current.next.data; null 2 2 obj 1

Remove at given index (cont’d)

Node nodeBefore = first; for (int i = 0; i < index- 1 ; i++) { nodeBefore = nodeBefore.next; } // remove the node E obj = nodeBefore.next.data; nodeBefore.next = nodeBefore.next.next; size--; return obj; }

Remove at given index (cont’d alternate)

Node nodeBefore = first; for (int i = 0; i < index- 1 ; i++) { nodeBefore = nodeBefore.next; } // remove the node Node nodeToRemove = nodeBefore.next; nodeBefore.next = nodeToRemove.next; size--; return nodeToRemove.data; }

Disadvantages of

Singly-Linked Lists

• Insertion into a list is generally linear.

• In order to insert a node at an index greater

than 0, we need a reference to the previous

node.

• In order to remove a node that is not the first

node, we need a reference to the previous

node.

• We can only traverse the list in one direction.

Java's LinkedList class

Java's LinkedList add(E obj) method runtime is O(1).

How?

LinkedList has a field that refers to the

last node in the list.

Java's LinkedList remove(int index) method runtime

is O(1). How is it possible even if your have the last

node?

The nodes inside LinkedList are doubly linked in

order to remove from the end of the list in O(1) time.

Doubly-Linked Lists

  • Each data entry is stored in its own node
  • Each node as two references: one reference is to the

next node and one is to the previous node ( prev ).

  • A reference, often referred to as head , points to the

first node in the list and one reference, often referred

to as tail , points the the last node in the list.

  • The head node’s prev reference is null and the tail

node’s next reference is null.

  • An empty list would have head and tail references

equal to null

Node class

private class Node { private E data; private Node prev; private Node next; private Node(E obj, Node previous, Node next){ data = obj; prev = previous; this.next = next; }

4. Result of adding at given index

null last null first

How would you implement addFirst? null last null first How would you implement addLast?