Doubly Linked Lists - PowerPoint PPT Presentation


PPT – Doubly Linked Lists PowerPoint presentation | free to download - id: 270c7d-NzM5M


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Doubly Linked Lists


Create a new node t //Make 't- prev' point to node head ... sz ; Algorithm: removeLast() //Save the address of the last node as 'old' ... – PowerPoint PPT presentation

Number of Views:342
Avg rating:3.0/5.0
Slides: 13
Provided by: charle113
Learn more at:
Tags: create | doubly | linked | lists | size


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Doubly Linked Lists

Doubly Linked Lists

Doubly Linked Lists
  • One powerful variation of a linked list is the
    doubly linked list. The doubly linked list
    structure is one in which each node has a pointer
    to both its successor and its predecessor.

Doubly Linked Lists
  • struct Node
  • char element
  • Node prev
  • Node next
  • Node(const char echar(),
  • Node pNULL, Node nNULL)
  • element(e), prev(p), next(n)

Sentinel Nodes
  • As always, implementation of a data structure
    is up to the individual. However, one way of
    implementing a double linked list is to create
    sentinel nodes at the beginning and end of the
  • Pointers to the sentinel nodes can be stored in a
    data structure as data members
  • Advantage Not necessary to treat the front or
    rear differently when inserting or removing

Empty Double Linked List
  • head new Node
  • tail new Node
  • head-gtnext tail
  • tail-gtprev head
  • sz 0

Deque - Double-Ended Queues
  • A Deque (pronounced deck) is a data structure
    in which items may be added to or deleted from
    the head or the tail. They are also known as
    doubly-ended queues.
  • The Deque is typically implemented with a linked
    list, providing easy access to the front and the
    back of the list, although a dynamic array could
    also be used for this.

Deque - Double-Ended Queues
  • The deque data structure typically has the
    following features
  • How are the data related?
  • Sequentially
  • Operations
  • insertFirst - inserts an element at the front of
    a deque
  • insertLast - inserts an element at the rear of a
  • removeFirst - removes an element from the front
    of a deque
  • removeLast - removes an element from the rear of
    a deque
  • (Note that the previous 2 functions did not
    return and data values)
  • First - returns a reference to the element at the
  • last - returns a reference to the element at the
  • size, isEmpty

Deque Advantages
  • Removing a node from the rear of a singly linked
    list was inefficient because we had to find the
    next-to-last by searching from the head
  • The deque is implemented with a doubly linked
    list. Therefore, now, removing from the rear is
    not a problem.

Algorithm insertFirst()
  • //Create a new node t
  • //Make t-gtprev point to node head
  • //Make t-gtnext point to node head-gtnext (i.e.,
  • Node t new Node(e,head,head-gtnext)
  • //Put the address of t into head-gtnexts prev
  • //Put the address of t into heads next pointer
  • head-gtnext-gtprev t
  • head-gtnext t

Algorithm insertFirst()
Node t new Node(e,head,head-gtnext) head-gtnext-
gtprev t head-gtnext t sz
Algorithm removeLast()
  • //Save the address of the last node as old
  • Node old tail-gtprev
  • //Make old-gtprev-gtnext point to node tail
  • old-gtprev-gtnext tail
  • //Make tail-gtprev point to node old-gtprev
  • tail-gtprev old-gtprev
  • //Delete old
  • delete old-

Algorithm removeLast()
Node old tail-gtprev old-gtprev-gtnext
tail tail-gtprev old-gtprev delete old
//Recover the memory sz--