0% found this document useful (0 votes)
27 views

In The Name of Allah The Most Beneficent The Most Merciful: Subject: Data Structures & Algorithms

The document discusses linked lists and doubly linked lists. It provides C++ code to implement a linked list with functions to add, remove, get, and traverse nodes. It then explains the advantages of doubly linked lists, such as allowing traversal in both directions. The code for a doubly linked list node class is shown with previous and next pointers. Guidelines are provided for adding a node to a doubly linked list by updating the pointers in the correct order.

Uploaded by

Abdullah Jutt
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)
27 views

In The Name of Allah The Most Beneficent The Most Merciful: Subject: Data Structures & Algorithms

The document discusses linked lists and doubly linked lists. It provides C++ code to implement a linked list with functions to add, remove, get, and traverse nodes. It then explains the advantages of doubly linked lists, such as allowing traversal in both directions. The code for a doubly linked list node class is shown with previous and next pointers. Guidelines are provided for adding a node to a doubly linked list by updating the pointers in the correct order.

Uploaded by

Abdullah Jutt
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/ 37

In the Name of Allah the Most

Beneficent the Most Merciful

Subject : Data Structures & Algorithms

Lecture : 09

Monday, January 18, 20 1


21
C++ Code for Linked List
void add(int addObject) {
Node* newNode = new Node();
newNode->set(addObject);
if( currentNode != NULL ){
newNode->setNext(currentNode->getNext());
currentNode->setNext( newNode );
lastCurrentNode = currentNode;
currentNode = newNode;
}
else {
newNode->setNext(NULL);
headNode->setNext(newNode);
lastCurrentNode = headNode;
currentNode = newNode;
}
size++;
 };
Building a Linked List
List list; headNode size=0
Building a Linked List
List list; headNode size=0

currentNode

headNode 2 size=1
list.add(2);
lastcurrentNode
Building a Linked List
List list; headNode size=0

currentNode

headNode 2 size=1
list.add(2);
lastcurrentNode

currentNode

list.add(6); headNode 2 6 size=2

lastcurrentNode
Building a Linked List

List.add(8); list.add(7); list.add(1);

currentNode

headNode 2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List

int get() {
if (currentNode != NULL)
return currentNode->get();
};
C++ Code for Linked List
bool next() {
if (currentNode == NULL) return false;

lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
};
C++ Code for Linked List
// position current before the first
// list element
void start() {
lastCurrentNode = headNode;
currentNode = headNode;
};
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
lastCurrentNode->setNext(currentNode->getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
};
currentNode
headNode

2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode->getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
};
currentNode
headNode
1

2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode->getNext());
2 delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
};
currentNode
headNode
1

2 8 7 1 size=5
2
lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode->getNext());
2 delete currentNode;
3 currentNode = lastCurrentNode->getNext();
4 size--;
}
3
};
currentNode
headNode
1 4
2 8 7 1 size=4
2
lastcurrentNode
C++ Code for Linked List
int length()
{
return size;
};

private:
int size;
Node *headNode;
Node *currentNode, *lastCurrentNode;
Example of List Usage
#include <iostream>
#include <stdlib.h>
#include "List.cpp"

int main(int argc, char *argv[])


{
List list;

list.add(5); list.add(13); list.add(4);


list.add(8); list.add(24); list.add(48);
list.add(12);
list.start();
while (list.next())
cout << "List Element: "<< list.get()<<endl;
}
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is also a one-step operation
 find
 worst-case: may have to search the entire list
 back
 moving the current pointer back one node
requires traversing the list from the start until
the node whose next pointer points to current
node.
Advantages of Linked List

1. Insertions and Deletions can be done easily.


2. It does not need movement of elements for insertion and
deletion.
3. It space is not wasted as we can get space according to
our requirements.
4. Its size is not fixed.
5. It can be extended or reduced according to requirements.
6. Elements may or may not be stored in consecutive
memory available, even then we can store the data in
computer.
AL 17
Disadvantages of Linked List

1. It requires more space as pointers  are also stored  with


information.
2. Different amount of time is required to access each
element.
3. If we have to go to a particular element then we have to go
through all those elements that come before that element.
4. we can not traverse it from last & only from the beginning.
5. It is not easy to sort the elements stored in the linear linked
list.

AL 18
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 22
Doubly-linked List

AL 23
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
Doubly-linked List

• A node can be added in four ways:

1) At the front of the DLL


2) After a given node.
3) At the end of the DLL
4) Before a given node.

AL 31
Represent a Doubly Linked list graphically

• Construct a doubly linked list from the


following data elements:
• A, B, C, D, E

AL 32
Represent a Doubly Linked list graphically

• Describe the steps to insert data item “F” at the


starting of a doubly linked list.

AL 33
Represent a Doubly Linked list graphically

• Describe the steps to insert data item “G” after the


data item “B” of a doubly linked list.

• What is the difference between singly and doubly


linked lists?

AL 34
Represent a Doubly Linked list graphically

• Describe the steps to remove data item “D” from a


doubly linked list.

AL 35
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 Linked list previous node cannot be reached .

AL 36
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 37

You might also like