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 and Queues, Study Guides, Projects, Research of Data Structures and Algorithms

The concept of doubly linked lists and their comparison to singly linked lists. It also introduces queues as a data structure, discussing their implementation and applications. Code examples and diagrams.

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 09/12/2022

sadayappan
sadayappan 🇺🇸

4.5

(15)

246 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Doubly Linked Lists
Overview!
-Assume we have a linked list, and now we want to append a node to the end of the list.
What we can do to make it happen? [We need to have a temporary, use it to go through the
list, and make it points at the last node of the list. Then update the next pointer of the last
node and set it points to the new node.] !
-This means if we have a long list, it will take a while to find the last node and do the
operation. Same thing happens to removing the last node. The time complexity depends on
the size of the list. !
-How can we modify the linked list, so that it will be easier to access the tail of the list?!
Add a tail pointer.!
-Or we can make it even more convenient in a way that we can move forward and backward
in the list. To achieve this, we can add a prev pointer to node in addition to the next pointer. !
-And this is what we called doubly linked lists. !
-Pros: more flexible !
-Cons: more memory and maintenance of references needed!
-Note: Header and trailer are sentinel nodes. A sentinel node is a specifically designed node
used with linked lists and trees as a traversal path terminator. This type of node does not
hold or reference any data managed by the data structure.!
-Each node in a doubly linked list contains three fields: the data, and two pointers: prev and
next.!
Private inner class Node !
private class Node {
private Node prev;
private Node next;
private T data;
pf3
pf4

Partial preview of the text

Download Doubly Linked Lists and Queues and more Study Guides, Projects, Research Data Structures and Algorithms in PDF only on Docsity!

Doubly Linked Lists

Overview

- Assume we have a linked list, and now we want to append a node to the end of the list.

What we can do to make it happen? [We need to have a temporary, use it to go through the

list, and make it points at the last node of the list. Then update the next pointer of the last

node and set it points to the new node.]

- This means if we have a long list, it will take a while to find the last node and do the

operation. Same thing happens to removing the last node. The time complexity depends on

the size of the list.

- How can we modify the linked list, so that it will be easier to access the tail of the list?

• Add a tail pointer.

- Or we can make it even more convenient in a way that we can move forward and backward

in the list. To achieve this, we can add a prev pointer to node in addition to the next pointer.

- And this is what we called doubly linked lists.

- Pros: more flexible

- Cons: more memory and maintenance of references needed

- Note: Header and trailer are sentinel nodes. A sentinel node is a specifically designed node

used with linked lists and trees as a traversal path terminator. This type of node does not

hold or reference any data managed by the data structure.

- Each node in a doubly linked list contains three fields: the data, and two pointers: prev and

next.

Private inner class Node

private class Node { private Node prev; private Node next; private T data;

public Node (T item) { data = item; next = null; prev = null; } //getters and setters … }

Diagram Example

  • (^) Original doubly linked list
  • (^) Delete head node
  • (^) Delete middle node
  • (^) Delete last node
  • (^) Please node: when adding/removing a node, it is necessary to update both prev and next

pointers of the node and the two pointers of the node before it if there is one.

Implementation Example

/* Returns the element at the specified position in the list */ public T get (int index) {

sure those queue classes that implement the interface will provide the basic operations at

least.

  • Java 8 also provides a Queue interface. This interface inherits size and isEmpty from another

interface called Collection. It also provide two set of methods to enqueue, dequeue, and

peek. One set returns a special value if the operation fails. The other set throws exception.

  • In the Lab 5, you will implement your queue, using the idea of the set that returns a special

value if the operation fails. But, you do not need to implement the Queue interface in this lab,

as we only implement one set of methods.

Applications of Queues

  • (^) Line of cars at a light
  • (^) Line of people at cafe
  • (^) Printer buffer

Implementation of Queues

  • (^) We can use many ways to implement queues:
    • Array: enqueue is easy to dequeue requires shifting the rest in the array
    • ArrayList: enqueue is easy, ArrayList takes care of the shifting cased by dequeue, resize

memory is necessary

  • SinglyLinkedList: dequeue is easy, but enqueue need to traverse to the end of the list
  • DoublyLinedList: enqueue (addLast) and dequeue (removeFirst/remove(0)) are easy.

Disclaimer: Notes adapted from previous CS 231 lecture materials at Colby College.