Doubly Linked List - PowerPoint PPT Presentation

About This Presentation
Title:

Doubly Linked List

Description:

Title: comp104 notes Author: Andrew Horner Last modified by: horner Created Date: 6/16/1996 12:02:10 AM Document presentation format: On-screen Show – PowerPoint PPT presentation

Number of Views:1959
Avg rating:3.0/5.0
Slides: 23
Provided by: Andrew1299
Category:
Tags: doubly | insert | linked | list | video

less

Transcript and Presenter's Notes

Title: Doubly Linked List


1
Programming
  • Doubly Linked List

2
Doubly Linked Lists
  • In a Doubly Linked List each item points to both
    its predecessor and successor
  • prev points to the predecessor
  • next points to the successor

20
Head
Cur-gtnext
Cur-gtprev
Cur
3
Motivation
  • Doubly linked lists are useful for playing video
    and sound files with rewind and instant
    replay
  • They are also useful for other linked data which
    require rewind and fast forward of the data

4
Doubly Linked List Definition
  • include ltiostreamgt
  • using namespace std
  • struct Node
  • int data
  • Node next
  • Node prev
  • typedef Node NodePtr

5
Doubly Linked List Operations
  • insertNode(NodePtr Head, int item)
  • //add new node to ordered doubly linked list
  • deleteNode(NodePtr Head, int item)
  • //remove a node from doubly linked list
  • searchNode(NodePtr Head, int item)
  • print(NodePtr Head, int item)

6
Deleting a Node
  • Delete a node Cur (not at front or rear)
  • (Cur-gtprev)-gtnext Cur-gtnext
  • (Cur-gtnext)-gtprev Cur-gtprev
  • delete Cur

Head
Cur
7
Inserting a Node
  • Insert a node New before Cur (not at front or
    rear)

New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New
Head
Cur
New
8
Doubly Linked Lists with Dummy Head Node
  • To simplify insertion and deletion by avoiding
    special cases of deletion and insertion at front
    and rear, a dummy head node is added at the head
    of the list
  • The last node also points to the dummy head node
    as its successor

9
Doubly Linked Lists with Dummy Head
  • Non-Empty List
  • Empty List

Dummy Head Node
Head
10
  • void createHead(NodePtr Head)
  • Head new Node
  • Head-gtnext Head
  • Head-gtprev Head

11
Deleting a Node
  • Delete a node Cur at front

(Cur-gtprev)-gtnext Cur-gtnext (Cur-gtnext)-gtprev
Cur-gtprev delete Cur
12
  • Delete a node Cur in the middle

(Cur-gtprev)-gtnext Cur-gtnext (Cur-gtnext)-gtprev
Cur-gtprev delete Cur // same as delete front!
13
  • Delete a node Cur at rear

(Cur-gtprev)-gtnext Cur-gtnext (Cur-gtnext)-gtprev
Cur-gtprev delete Cur // same as delete front
and middle!
14
  • void deleteNode(NodePtr Head, int item)
  • NodePtr Cur
  • Cur searchNode(Head, item)
  • if(Cur ! NULL)
  • Cur-gtprev-gtnext Cur-gtnext
  • Cur-gtnext-gtprev Cur-gtprev
  • delete Cur

15
Inserting a Node
  • Insert a Node New after dummy node and before Cur

New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New
Dummy Head Node
Head
Cur
New
16
  • Insert a Node New in the middle and before Cur

New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New // same as insert
front!
Dummy Head Node
Cur
New
Head
17
  • Insert a Node New at Rear (with Cur pointing to
    dummy head)

New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New // same as insert
front!
Dummy Head Node
New
Head
Cur
18
  • Insert a Node New to Empty List (with Cur
    pointing to dummy head node)

New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New
Dummy Head Node
Head
New
Cur
19
  • void insertNode(NodePtr Head, int item)
  • NodePtr New, Cur
  • New new Node
  • New-gtdata item
  • Cur Head-gtnext
  • while(Cur ! Head) //position Cur for insertion
  • if(Cur-gtdata lt item)
  • Cur Cur-gtnext
  • else
  • break
  • New-gtnext Cur
  • New-gtprev Cur-gtprev
  • Cur-gtprev New
  • (New-gtprev)-gtnext New

20
Searching a node
  • NodePtr searchNode(NodePtr Head, int item)
  • NodePtr Cur Head-gtnext
  • while(Cur ! Head)
  • if(Cur-gtdata item)
  • return Cur
  • if(Cur-gtdata lt item)
  • Cur Cur-gtnext
  • else
  • break
  • return NULL

21
Print the whole list
  • void print(NodePtr Head)
  • NodePtr CurHead-gtnext
  • while(Cur ! Head)
  • cout ltlt Cur-gtdata ltlt " "
  • Cur Cur-gtnext
  • cout ltlt endl

22
  • void main()
  • NodePtr Head, temp
  • createHead(Head)
  • print(Head)
  • insertNode(Head, 3)
  • print(Head)
  • insertNode(Head, 5)
  • print(Head)
  • insertNode(Head, 2)
  • print(Head)
  • insertNode(Head, 7)
  • insertNode(Head, 1)
  • insertNode(Head, 8)
  • print(Head)
  • deleteNode(Head, 7)
  • deleteNode(Head, 0)
  • print(Head)
  • temp searchNode(Head, 5)

Result is 3 3 5 2 3 5 1 2 3 5 7 8 1 2 3 5
8 Data is contained in the list
Write a Comment
User Comments (0)
About PowerShow.com