You are on page 1of 38

Syrian Arab Republic

Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication

Algorithms and data structures

Fourth lecture

By: Dr. Hayyan Hasan

Algorithms and data structures - First semester 2023-2024 1


Outline

• Doubly linked list Definition.


• Memory Representation of a doubly linked list.
• Operations on doubly linked list
• Time complexity.

Algorithms and data structures - First semester 2023-2024 2


Doubly linked list Definition

• Doubly linked list is a complex type of linked list in which a node


contains a pointer to the previous as well as the next node in the
sequence.
• Therefore, in a doubly linked list, a node consists of three parts:
• node data.
• pointer to the next node in sequence (next pointer).
• pointer to the previous node in sequence (previous pointer).
• Doubly linked list can be traversed in both directions.

Algorithms and data structures - First semester 2023-2024 3


Doubly linked list Definition Cont.

• A doubly linked list containing three nodes having numbers from 1 to 3 in


their data part, is shown in the following image.

• The prev part of the first node and the next part of the last node will always
contain null or (-1) indicating end in each direction.

Algorithms and data structures - First semester 2023-2024 4


Memory Representation of a doubly linked list

• The first element of the list that is i.e. 13 stored at address 1.


• The head pointer points to the starting address 1.
• Since this is the first element being added to the list therefore
the prev of the list contains null.
• The next node of the list resides at address 4 therefore the first node
contains 4 in its next pointer.
• And so on.

Algorithms and data structures - First semester 2023-2024 5


Operations on doubly linked list

• Node creation

struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head;

• We can Allocate the space for the new node using malloc or new
instruction.
• First method : ptr = (struct node *) malloc(sizeof(struct node *));
• Second method: node* newNode = new node();

Algorithms and data structures - First semester 2023-2024 6


Basic operations on Doubly Linked List

• Many operations
• Insertion.
• Deletion.
• Traversing.
• Searching.

Algorithms and data structures - First semester 2023-2024 7


Basic operations on Doubly Linked List Cont.

• Insertion
• The insertion into a doubly linked list can be performed at different positions.
• Based on the position of the new node being inserted, the insertion is
categorized into the following categories.
• Insertion at the bigging of the linked list.
• Insertion at the end of the linked list.
• Insertion after specific node.

Algorithms and data structures - First semester 2023-2024 8


Basic operations on Doubly Linked List Cont.

• Insertion in doubly linked list at beginning


• As in doubly linked list, each node of the list contain double pointers therefore
we have to maintain more number of pointers in doubly linked list as compare
to singly linked list.
• There are two scenarios of inserting any element into doubly linked list.
• The list is empty.
• The list contains at least one element.

Algorithms and data structures - First semester 2023-2024 9


Basic operations on Doubly Linked List Cont.

• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 10
• Step 4: Set PTR -> DATA = ITEM
• Step 5: If HEAD = NULL, Set PTR -> NEXT = NULL, Set PTR -> PREV =
NULL, Set HEAD = PTR, Goto Step 10
• Step 6: Set PTR -> PREV = NULL
• Step 7: Set PTR -> NEXT = HEAD
• Step 8: Set HEAD -> PREV = PTR
• Step 9: Set HEAD = PTR
• Step 10: End

Algorithms and data structures - First semester 2023-2024 10


Basic operations on Doubly Linked List Cont.

• Insertion in doubly linked list at beginning

Algorithms and data structures - First semester 2023-2024 11


Basic operations on Doubly Linked List Cont.
#include <iostream> void insertbeginning(int item)
using namespace std; {
//node structure struct Node *ptr = new Node;
struct Node if(ptr == NULL)
{ {
int data; cout<< "\nOVERFLOW";
struct Node *next; }
struct Node *prev; else
}; {
class LinkedList { ptr->data=item;
private:
Node* head; if(head==NULL)
public: {
LinkedList(){ ptr->next = NULL;
head = NULL; ptr->prev=NULL;
} head=ptr;
void PrintList() { }
Node* temp = head; else
if(temp != NULL) { {
while(temp != NULL) { ptr->prev=NULL;
cout<<temp->data<<" "; ptr->next = head;
temp = temp->next; head -> prev = ptr;
} head=ptr;
cout<<endl; }
} else { }
cout<<"The list is empty.\n"; }};
}
}
Algorithms and data structures - First semester 2023-2024 12
Basic operations on Doubly Linked List Cont.

// test the code


int main() {
LinkedList MyList;
//Insert an element 10 at the bilging
MyList.insertbeginning(10);
cout << "the list after insert the first element: ";
MyList.PrintList();
//Insert an element 20 at the bilging
MyList.insertbeginning(20);
cout << "the list after insert the second element: ";
MyList.PrintList();
//Insert an element 30 at the bilging
MyList.insertbeginning(30);
cout << "the list after insert the third element: ";
MyList.PrintList();

return 0;
}

Algorithms and data structures - First semester 2023-2024 13


Basic operations on Doubly Linked List Cont.

• Insertion in doubly linked list at the end


• In order to insert a node in doubly linked list at the end, we must make sure whether the list is empty or it
contains any element.
• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 12.
• Step 4: Set PTR -> DATA = ITEM
• Step 5: If HEAD = NULL, Set PTR -> NEXT = NULL, Set PTR -> PREV = NULL, Set HEAD = PTR,
Goto Step 12.
• Step 6: Set TEMP = HEAD
• Step 7: Repeat Step 8 While TEMP -> NEXT != NULL
• Step 8: Set TEMP = TEMP -> NEXT
• Step 9: Set TEMP -> NEXT = PTR
• Step 10: Set PTR-> PREV = TEMP
• Step 11: Set PTR-> NEXT = NULL
• Step 12: End

Algorithms and data structures - First semester 2023-2024 14


Basic operations on Doubly Linked List Cont.

• Insertion in doubly linked list at the end

Algorithms and data structures - First semester 2023-2024 15


Basic operations on Doubly Linked List Cont.
void insertlast(int item)
#include <iostream>
{
using namespace std;
struct Node *ptr = new Node;
//node structure
struct Node *temp;
struct Node
if(ptr == NULL)
{
{
int data;
cout << "\nOVERFLOW";
struct Node *next;
struct Node *prev;
}
};
else
class LinkedList {
{
private:
ptr->data=item;
Node* head;
if(head == NULL)
public:
{
LinkedList(){
ptr->next = NULL;
head = NULL;
ptr->prev = NULL;
}
head = ptr;
void PrintList() {
}
Node* temp = head;
else
if(temp != NULL) {
{
while(temp != NULL) {
temp = head;
cout<<temp->data<<" ";
while(temp->next!=NULL)
temp = temp->next;
{
}
temp = temp->next;
cout<<endl;
}
} else {
temp->next = ptr;
cout<<"The list is empty.\n";
ptr ->prev=temp;
}
ptr->next = NULL;
}
} } }};
Algorithms and data structures - First semester 2023-2024 16
Basic operations on Doubly Linked List Cont.

// test the code


int main() {
LinkedList MyList;
//Insert an element 10 at the end
MyList. insertlast(10);
cout << "the list after insert the first element: ";
MyList.PrintList();
//Insert an element 20 at the end
MyList. insertlast(20);
cout << "the list after insert the second element: ";
MyList.PrintList();
//Insert an element 30 at the end
MyList. insertlast(30);
cout << "the list after insert the third element: ";
MyList.PrintList();

return 0;
}

Algorithms and data structures - First semester 2023-2024 17


Basic operations on Doubly Linked List Cont.

• Insertion in doubly linked list after Specified node


• In order to insert a node after the specified node in the list, we need to skip the
required number of nodes in order to reach the mentioned node and then make the
pointer adjustments as required.

Algorithms and data structures - First semester 2023-2024 18


Basic operations on Doubly Linked List Cont.

• Algorithm
• Step 1: Start
• Step 2: Create NEW_NODE = PTR
• Step 3: If PTR = NULL, Write OVERFLOW, Goto Step 14.
• Step 4: Set PTR -> DATA = ITEM, Set I = 0
• Step 5: If LOC < 0, Write INVALED, TRY AGAIN, Goto Step 14.
• Step 6: Set TEMP = HEAD
• Step 7: Repeat 8 and 9 While I < LOC
• Step 8: Set TEMP = TEMP -> NEXT, Set I = I+1.
• Step 9: If TEMP = NULL, Write "LESS THAN DESIRED NO. OF ELEMENTS”, Goto Step 14
• Step 10: Set TEMP-> NEXT -> PREV = PTR
• Step 11: Set PTR-> NEXT = TEMP -> NEXT
• Step 12: Set PTR-> PREV = TEMP
• Step 13 : Set TEMP -> NEXT = PTR
• Step 14: End

Algorithms and data structures - First semester 2023-2024 19


Basic operations on Doubly Linked List Cont.

• Insertion in doubly linked list after Specified node

Algorithms and data structures - First semester 2023-2024 20


Basic operations on Doubly Linked List Cont.
#include <iostream> void insert_specified(int item){
using namespace std; struct Node *ptr = new Node;
//node structure struct Node *temp;
struct Node int i, loc;
{ if(ptr == NULL){
int data; cout <<"\n OVERFLOW";
struct Node *next; }else {
struct Node *prev; ptr->data = item;
}; cout << "\nEnter the location\n";
class LinkedList { cin >> loc;
private: if(loc < 0) {
Node* head; cout<<"\nLocation should be >= 1.";
public: } else{
LinkedList(){ temp=head;
head = NULL; } for(i=0;i<loc;i++){
//display the content of the list temp = temp->next;
void PrintList() { if(temp == NULL){
Node* temp = head; cout << "\ncan't insert\n";
if(temp != NULL) { return; }}
while(temp != NULL) { temp -> next ->prev = ptr;
cout<<temp->data<<" "; ptr->next = temp->next;
temp = temp->next; ptr -> prev = temp;
} temp->next = ptr;
cout<<endl; }}}
} else {
cout<<"The list is empty.\n";
}}

Algorithms and data structures - First semester 2023-2024 21


Basic operations on Doubly Linked List Cont.

//fill the list with the elements // test the code


void fill_List(int newElement) { int main() {
Node* ptr = new Node(); LinkedList MyList;
if(ptr == NULL) // fill the list
{ MyList.fill_List(10);
cout << "\nOVERFLOW"; MyList.fill_List(20);
} else { MyList.fill_List(30);
if(head==NULL) cout << "the list before insertion : ";
{ MyList.PrintList();
ptr->next = NULL; //Insert an element
ptr->prev=NULL; MyList.insert_specified(100);
ptr->data=newElement; cout << "the list after insertion: ";
head=ptr; MyList.PrintList();
} return 0;
else }
{
ptr->data=newElement;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}}}};

Algorithms and data structures - First semester 2023-2024 22


Basic operations on Doubly Linked List Cont.

• Deletion at beginning
• Deletion in doubly linked list at the beginning is the simplest operation. We
just need to copy the head pointer to pointer ptr and shift the head pointer to
its next.
• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write UNDERFLOW, Goto Step 7
• Step 3: Set PTR = HEAD
• Step 4: Set HEAD = HEAD -> NEXT
• Step 5: Set HEAD -> PREV = NULL
• Step 6: Free PTR
• Step 7: End

Algorithms and data structures - First semester 2023-2024 23


Basic operations on Doubly Linked List Cont.

• Deletion at beginning

Algorithms and data structures - First semester 2023-2024 24


Basic operations on Doubly Linked List Cont.
#include <iostream> void beginning_delete()
using namespace std; {
//node structure struct Node *ptr;
struct Node if(head == NULL)
{ {
int data; cout << "\n UNDERFLOW\n";
struct Node *next; }
struct Node *prev; else
}; {
class LinkedList { ptr = head;
private: head = head -> next;
Node* head; head -> prev = NULL;
public: free(ptr);
LinkedList(){ cout << "\nNode Deleted\n";
head = NULL; }
} }
//display the content of the list
void PrintList() {
Node* temp = head;
if(temp != NULL) {
while(temp != NULL) {
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
} else {
cout<<"The list is empty.\n";
}
} Algorithms and data structures - First semester 2023-2024 25
Basic operations on Doubly Linked List Cont.

//fill the list with the elements // test the code


void fill_List(int newElement) { int main() {
Node* ptr = new Node(); LinkedList MyList;
if(ptr == NULL) // fill the list
{ MyList.fill_List(10);
cout << "\nOVERFLOW"; MyList.fill_List(20);
} MyList.fill_List(30);
else cout << "the list before deletion : ";
{ MyList.PrintList();
if(head==NULL) //delete an element
{ MyList.beginning_delete();
ptr->next = NULL; cout << "the list after deletion: ";
ptr->prev=NULL; MyList.PrintList();
ptr->data=newElement; return 0;
head=ptr; }
}
else
{
ptr->data=newElement;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
}
}
};
Algorithms and data structures - First semester 2023-2024 26
Basic operations on Doubly Linked List Cont.

• Deletion in doubly linked list at the end


• Deletion of the last node in a doubly linked list needs traversing the list in
order to reach the last node of the list and then make pointer adjustments at
that position.
• In order to delete the last node of the list, we need to follow the following
steps.
• If the list is already empty then the condition head == NULL will become true and
therefore the operation can not be carried on.
• If there is only one node in the list then the condition head -> next == NULL become
true. In this case, we just need to assign the head of the list to NULL and free head in
order to completely delete the list.
• Otherwise, just traverse the list to reach the last node of the list.

Algorithms and data structures - First semester 2023-2024 27


Basic operations on Doubly Linked List Cont.

• Algorithm
• Step 1: Start
• Step 2: If HEAD = NULL, Write UNDERFLOW, Goto Step 9.
• Step 3: If HEAD - > NEXT = NULL, Set HEAD = NULL, Free HEAD ,
Goto Step 9.
• Step 4: Set PTR = HEAD
• Step 5: Repeat Step 4 While PTR ->NEXT != NULL
• Step 6: Set PTR = PTR ->NEXT
• Step 7: Set PTR ->PREV-> NEXT = NULL
• Step 8: Free PTR
• Step 9: End

Algorithms and data structures - First semester 2023-2024 28


Basic operations on Doubly Linked List Cont.

• Deletion in doubly linked list at the end

Algorithms and data structures - First semester 2023-2024 29


Basic operations on Doubly Linked List Cont.
#include <iostream> void last_delete()
using namespace std; {
//node structure struct Node *ptr;
struct Node if(head == NULL)
{ {
int data; cout << "\n UNDERFLOW\n";
struct Node *next; }
struct Node *prev; else if(head->next == NULL)
}; {
class LinkedList { head = NULL;
private: free(head);
Node* head; cout << "\nNode Deleted \n";
public: }
LinkedList(){ else
head = NULL; {
} ptr = head;
//display the content of the list while (ptr->next != NULL)
void PrintList() { {
Node* temp = head; ptr = ptr -> next;
if(temp != NULL) { }
while(temp != NULL) { ptr -> prev -> next = NULL;
cout<<temp->data<<" "; free(ptr);
temp = temp->next; cout << "\nNode Deleted \n";
} }
cout<<endl; }
} else {
cout<<"The list is empty.\n";
}
} Algorithms and data structures - First semester 2023-2024 30
Basic operations on Doubly Linked List Cont.

//fill the list with the elements // test the code


void fill_List(int newElement) { int main() {
Node* ptr = new Node(); LinkedList MyList;
if(ptr == NULL) // fill the list
{ MyList.fill_List(10);
cout << "\nOVERFLOW"; MyList.fill_List(20);
} MyList.fill_List(30);
else cout << "the list before deletion : ";
{ MyList.PrintList();
if(head==NULL) //delete an element
{ MyList. last_delete();
ptr->next = NULL; cout << "the list after deletion: ";
ptr->prev=NULL; MyList.PrintList();
ptr->data=newElement; return 0;
head=ptr; }
}
else
{
ptr->data=newElement;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
}
}
};
Algorithms and data structures - First semester 2023-2024 31
Basic operations on Doubly Linked List Cont.

• Searching for a specific node in Doubly Linked List


• We just need traverse the list in order to search for a specific element in the
list.
• Algorithm
• Step 1: Start.
• Step 2: If HEAD == NULL, Write "UNDERFLOW”, Goto Step 9
• Step 3: Set PTR = HEAD
• Step 4: Set I = 0
• Step 5: Repeat Steps 6 To 8 While PTR != NULL
• Step 6: If PTR -> DATA = ITEM, Return I, Goto Step 9.
• Step 7: I = I + 1
• Step 8: Set PTR = PTR -> NEXT
• Step 9: End

Algorithms and data structures - First semester 2023-2024 32


Basic operations on Doubly Linked List Cont.
#include <iostream> void search()
using namespace std; {
//node structure struct Node *ptr;
struct Node int item,i, flag = 0;
{ if(head == NULL)
int data; {
struct Node *next; cout << "\nEmpty List\n";
struct Node *prev; } else
}; { ptr = head;
class LinkedList { i=0;
private: cout << "\nEnter item which you want to search?\n";
Node* head; cin >> item;
public: while (ptr!=NULL)
LinkedList(){ {
head = NULL; if(ptr->data == item)
} {
//display the content of the list cout << "\nitem found at location " << i;
void PrintList() { flag=0;
Node* temp = head; break;
if(temp != NULL) { } else
while(temp != NULL) { {
cout<<temp->data<<" "; flag=1;
temp = temp->next; }
} i++;
cout<<endl; ptr = ptr -> next;
} else { }
cout<<"The list is empty.\n"; if(flag==1)
} { cout << "\nItem not found\n"; } } }
} Algorithms and data structures - First semester 2023-2024 33
Basic operations on Doubly Linked List Cont.

//fill the list with the elements // test the code


void fill_List(int newElement) { int main() {
Node* ptr = new Node(); LinkedList MyList;
if(ptr == NULL) // fill the list
{ MyList.fill_List(10);
cout << "\nOVERFLOW"; MyList.fill_List(20);
} MyList.fill_List(30);
else cout << "the list after fill : ";
{ MyList.PrintList();
if(head==NULL) //search an element
{ MyList.search();
ptr->next = NULL; return 0;
ptr->prev=NULL; }
ptr->data=newElement;
head=ptr;
}
else
{
ptr->data=newElement;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
}
}
};
Algorithms and data structures - First semester 2023-2024 34
Basic operations on Doubly Linked List Cont.

• Traversing in doubly linked list


• Traversing is the most common operation in case of each data structure.
• Algorithm
• Step 1: If HEAD == NULL, Write "UNDERFLOW”, Goto Step 6
• Step 2: Set PTR = HEAD
• Step 3: Repeat Step 4 and 5 While PTR != NULL
• Step 4: Write PTR -> DATA
• Step 5: Set PTR = PTR -> NEXT
• Step 6: End

Algorithms and data structures - First semester 2023-2024 35


Basic operations on Doubly Linked List Cont.
#include <iostream>
using namespace std; //display the content of the list
//node structure void PrintList() {
struct Node Node* ptr = head;
{
if(ptr != NULL) {
int data;
while(ptr != NULL) {
struct Node *next;
struct Node *prev;
cout<< ptr >data<<" ";
}; temp = ptr >next;
class LinkedList { }
private: cout<<endl;
Node* head; } else {
public: cout<<"The list is empty.\n";
LinkedList(){ }
head = NULL; }
}

Algorithms and data structures - First semester 2023-2024 36


Basic operations on Doubly Linked List Cont.

//fill the list with the elements // test the code


void fill_List(int newElement) { int main() {
Node* ptr = new Node(); LinkedList MyList;
if(ptr == NULL) // fill the list
{ MyList.fill_List(10);
cout << "\nOVERFLOW"; MyList.fill_List(20);
} MyList.fill_List(30);
else cout << "the list after fill : ";
{ MyList.PrintList();
if(head==NULL) return 0;
{ }
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=newElement;
head=ptr;
}
else
{
ptr->data=newElement;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
}
}
};
Algorithms and data structures - First semester 2023-2024 37
Time complexity

• For the Traversal operation the worst case time complexity is O(n).
• For the Insertion operation in general (Not the first element) the worst
case time complexity is O(n).
• For the Deletion operation the worst case time complexity is O(n).
• For the Search operation the worst case time complexity is O(n).

Algorithms and data structures - First semester 2023-2024 38

You might also like