0% found this document useful (0 votes)
3 views15 pages

Lecture 9

A doubly-linked list is a type of linked list that allows traversal in both forward and backward directions by using two pointers (prev and next) in each node. While it offers advantages such as easier reversal and access to any node, it also has drawbacks including increased space usage and more complex insertion and deletion processes. The document includes a code example for a doubly-linked list node and outlines the steps for adding a new node.

Uploaded by

Rana Modified
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

Lecture 9

A doubly-linked list is a type of linked list that allows traversal in both forward and backward directions by using two pointers (prev and next) in each node. While it offers advantages such as easier reversal and access to any node, it also has drawbacks including increased space usage and more complex insertion and deletion processes. The document includes a code example for a doubly-linked list node and outlines the steps for adding a new node.

Uploaded by

Rana Modified
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Dr.

Ghulam Farooque
Assistant Professor
Department: CS & IT
The University of Lahore
Email:
[email protected]
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
 To move back one node, we have to start at
the head of the singly-linked list and move
forward until the node before the current.
Doubly-linked List
 Moving forward in a singly-linked list is easy;
moving backwards is not so easy.
 To move back one node, we have to start at
the head of the singly-linked list and move
forward until the node before the current.
 To avoid this we can use two pointers in a
node: one to point to next node and another to
point to the previous node:

prev element next


Doubly-linked List
• A doubly Linked list is a special type of linked
list where the nodes contain three fields i.e one
data field and two link fields . The data Field
contains the data element and the two link fields
contains two pointers i.e prev and next . The
pointer prev points to the previous node or points
to a NULL value if it is the first node in the list .
The pointer next points to the next node or points
to a NULL value of it is the last node in the list .
AL 5
Doubly-linked List

AL 6
Doubly-Linked List Node
class Node {
public:
int get() { return object; };
void set(int object) { this->object = object; };

Node* getNext() { return nextNode; };


void setNext(Node* nextNode)
{ this->nextNode = nextNode; };
Node* getPrev() { return prevNode; };

void setPrev(Node* prevNode)
{ this->prevNode = prevNode; };
private:
int object;
Node* nextNode;
 Node* prevNode;
};
Doubly-linked List
 Need to be more careful when adding or
removing a node.
 Consider add: the order in which pointers are
reorganized is important:

head 2 6 8 7 1 size=5

current
Doubly-linked List
1. newNode->setNext( current->getNext() );

current

head 2 6 8 7 1 size=5

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );

current

head 2 6 8 7 1 size=5

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);

current

head 2 6 8 7 1 size=5

2 3

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
4. current->setNext( newNode );

current

head 2 6 8 7 1 size=5

2 4 3

newNode 9 1
Doubly-linked List
1. newNode->setNext( current->getNext() );
2. newNode->setprev( current );
3. (current->getNext())->setPrev(newNode);
4. current->setNext( newNode );
5. current = newNode;
6. size++;

head 2 6 8 7 1 size=6

2 4 3

newNode 9 1

current
Advantages of Doubly Linked List

1. The Main advantage of using double linked list is that , the


list can be traversed in forward as well as backward
direction .

2. Linked List can be easily reversed when compared


to Single Linked List .

3. We can traverse to any node in a Doubly linked list But


in Single Linkedlist previous node cannot be reached .

AL 14
Disadvantages of Doubly Linked List

1. It takes more space in Doubly Linked List than


in Single Linked list because of an extra pointer .

2. Insertion and Deletion take more time than linear linked


list because more pointer operations are required than
linear linked list

AL 15

You might also like