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

Third Lecture

Linked List definition. • Why use linked list over array? • Singly linked list. • Node creation. • Basic operations on Singly linked list. • Time complexity

Uploaded by

Rami Rami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Third Lecture

Linked List definition. • Why use linked list over array? • Singly linked list. • Node creation. • Basic operations on Singly linked list. • Time complexity

Uploaded by

Rami Rami
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

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

Third lecture

By: Dr. Hayyan Hasan

Algorithms and data structures - First semester 2023-2024 1


Outline

• Linked List definition.


• Why use linked list over array?
• Singly linked list.
• Node creation.
• Basic operations on Singly linked list.
• Time complexity.

Algorithms and data structures - First semester 2023-2024 2


Linked List definition

• Linked List can be defined as collection of objects called nodes that


are randomly stored in the memory.
• A node contains two fields i.e. data stored at that particular address
and the pointer which contains the address of the next node in the
memory.
• The last node of the list contains pointer to the null (or -1) called the
tail.

Algorithms and data structures - First semester 2023-2024 3


Why use linked list over array?

• Till now, we were using array data structure to organize the group of elements
that are to be stored individually in the memory. However, Array has several
advantages and disadvantages which must be known in order to decide the data
structure which will be used throughout the program.
• Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process.
3. All the elements in the array need to be contiguously stored in the memory.
• Linked list is the data structure which can overcome all the limitations of an array.
Using linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-contiguously stored
in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of declaration.
List grows as per the program's demand and limited to the available memory space.

Algorithms and data structures - First semester 2023-2024 4


Singly linked list

• Singly linked list can be defined as the collection of ordered set of


elements.
• The number of elements may vary according to need of the program.
• A node in the singly linked list consist of two parts: data part and link
part.
• Data part of the node stores actual information that is to be represented by the
node.
• The link part of the node stores the address of its immediate successor.
• Singly linked list can be traversed only in one direction. In other
words, we can say that each node contains only next pointer, therefore
we can not traverse the list in the reverse direction.

Algorithms and data structures - First semester 2023-2024 5


Node creation

• We can implement the node in the linked list using structures.


#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};

• 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 Singly Linked List

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

Algorithms and data structures - First semester 2023-2024 7


Basic operations on Singly Linked List Cont.

• Insertion
• The insertion into a singly linked list can be performed at different positions.
• Examples
• Insertion at the bigging of the linked list.
• Insertion at the end of the linked list.
• Insertion before specific node.

Algorithms and data structures - First semester 2023-2024 8


Basic operations on Singly Linked List Cont.

• Insertion at the bigging of the linked list

Algorithms and data structures - First semester 2023-2024 9


Basic operations on Singly Linked List Cont.

• Insertion at the begging of the linked list


• It involves inserting any element at the front of the list. We just need
to a few link adjustments to make the new node as the head of the list.
• Algorithm
• STEP 1: Start
• STEP 2: Create NEW_NODE = PTR
• STEP 3: IF PTR = NULL, Write OVERFLOW, Go to Step 7.
• STEP 4: SET PTR -> DATA = Item
• STEP 5: SET PTR -> NEXT = HEAD
• STEP 6: SET HEAD = PTR
• STEP 7: End

Algorithms and data structures - First semester 2023-2024 10


Basic operations on Singly Linked List Cont.
#include <iostream>
using namespace std; void beginsert(int item)
void beginsert(int); {
struct node struct node *ptr = (struct node
{ *)malloc(sizeof(struct node *));
int data; if(ptr == NULL)
struct node *next; {
}; cout << "\nOVERFLOW\n";
struct node *head; }
int main () else
{ {
int choice,item; ptr->data = item;
do ptr->next = head;
{ head = ptr;
cout << "\nEnter the item which you want cout << "\nNode inserted\n";
to insert?\n"; }
cin >> item; }
beginsert(item);
cout <<"\nPress 0 to insert more ?\n";
cin >> choice;
}while(choice == 0);
return 0;
}

Algorithms and data structures - First semester 2023-2024 11


Basic operations on Singly Linked List Cont.

• Output

Algorithms and data structures - First semester 2023-2024 12


Basic operations on Singly Linked List Cont.

• Insertion in singly linked list at the end


• In order to insert a node at the last, there are two following scenarios
which need to be mentioned.
1.The node is being added to an empty list
2.The node is being added to the end of the none empty list.

Algorithms and data structures - First semester 2023-2024 13


Basic operations on Singly Linked List Cont.

Insertion in singly linked list at the end

Algorithms and data structures - First semester 2023-2024 14


Basic operations on Singly Linked List Cont.

• Algorithm
• STEP 1: Start.
• STEP 2: Create NEW_NODE = PTR.
• STEP 3: IF PTR = NULL Write OVERFLOW, Go to Step 11.
• STEP 4: SET PTR -> DATA = Item.
• STEP 5: IF Head == NULL, HEAD = PTR, SET PTR - > NEXT = NUL, Go
to Step 11.
• 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 - > NEXT = NUL
• STEP 11: End.

Algorithms and data structures - First semester 2023-2024 15


Basic operations on Singly Linked List Cont.
void lastinsert(int item)
#include <iostream>
{
using namespace std; struct node *ptr = (struct
void lastinsert(int); node*)malloc(sizeof(struct node));
struct node struct node *temp;
{ if(ptr == NULL)
int data; {
struct node *next; cout << "\nOVERFLOW";
}; }
struct node *head; else
int main () {
ptr->data = item;
{
if(head == NULL)
int choice,item; {
do ptr -> next = NULL;
{ head = ptr;
cout << "\nEnter the item which you cout << "\nNode inserted";
want to insert?\n"; }
cin >> item; else
lastinsert(item); {
cout <<"\nPress 0 to insert more ?\n";
cin >> choice; temp = head;
}while(choice == 0); while (temp -> next != NULL)
{
return 0;
temp = temp -> next;
}
}
temp->next = ptr;
ptr->next = NULL;
cout << "\nNode inserted";
} }}
Algorithms and data structures - First semester 2023-2024 16
Basic operations on Singly Linked List Cont.

• Output

Algorithms and data structures - First semester 2023-2024 17


Basic operations on Singly Linked List Cont.

• Insertion in singly linked list before specified Node


• 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 POSITION < 0, WRITE INVALED, TRY AGAIN, GOTO STEP 11.
• STEP 6: IF POSITION =0, PTR-> NEXT = HEAD, HEAD = PTR, GOTO STEP
11.
• STEP 7: REPEAT STEP 8 UNTIL POSITION-1.
• STEP 8: TEMP = TEMP → NEXT
• STEP 9: PTR → NEXT = TEMP → NEXT
• STEP 10: TEMP → NEXT = PTR
• STEP 11: END

Algorithms and data structures - First semester 2023-2024 18


Basic operations on Singly Linked List Cont.

Algorithms and data structures - First semester 2023-2024 19


Basic operations on Singly Linked List Cont.
//Inserts a new element before specific
node
#include <iostream>
void push_at(int newElement, int
using namespace std;
position) {
//node structure
Node* ptr = new Node();
struct Node {
ptr->data = newElement;
int data;
ptr->next = NULL;
Node* next;
if(ptr == NULL)
};
{
class LinkedList {
cout << "\nOVERFLOW";
private:
}else
Node* head;
{
public:
if(position < 0) {
LinkedList(){
cout<<"\nposition should be >= 1.";
head = NULL;
} else if (position == 0) {
}
ptr->next = head;
//fill the list with the elements
head = ptr;
void fill_List(int newElement) {
} else {
Node* newNode = new Node();
Node* temp = head;
newNode->data = newElement;
for(int i = 0; i < position-1; i++) {
newNode->next = NULL;
if(temp != NULL) {
if(head == NULL) {
temp = temp->next;
head = newNode;
}}
} else {
if(temp != NULL) {
Node* temp = head;
ptr->next = temp->next;
while(temp->next != NULL)
temp->next = ptr;
temp = temp->next;
} else {
temp->next = newNode;
cout<<"\nThe previous node is null.";
}
}}}}
}
Algorithms and data structures - First semester 2023-2024 20
Basic operations on Singly Linked List Cont.
//display the content of the list // test the code
void PrintList() { int main() {
Node* temp = head; LinkedList MyList;
if(temp != NULL) { //Add three elements at the end of the list.
MyList.fill_List(10);
MyList.fill_List(20);
while(temp != NULL) { MyList.fill_List(30);
cout<<temp->data<<" "; cout << "the list before insertion ";
temp = temp->next; MyList.PrintList();
} //Insert an element before node 2
cout<<endl; MyList.push_at(100, 2);
} else { cout << "the list after inserting the item 100
cout<<"The list is empty.\n"; before node 2 : ";
} MyList.PrintList();
} //Insert an element before node 1
}; MyList.push_at(200, 1);
cout << "the list after inserting the item 200
before node 1 : ";
MyList.PrintList();

return 0;
}

Algorithms and data structures - First semester 2023-2024 21


Basic operations on Singly Linked List Cont.

• Output

Algorithms and data structures - First semester 2023-2024 22


Basic operations on Singly Linked List Cont.

• Deletion in singly linked list at beginning


• Deleting a node from the beginning of the list is the simplest operation of all.
• It just need a few adjustments in the node pointers.
• Since the first node of the list is to be deleted, therefore, we just need to make
the head, point to the next of the head.
• Algorithm
• STEP 1: IF HEAD = NULL, Write UNDERFLOW, Go to Step 5
• STEP 2: SET PTR = HEAD
• STEP 3: SET HEAD = PTR -> NEXT
• STEP 4: FREE PTR
• STEP 5: End

Algorithms and data structures - First semester 2023-2024 23


Basic operations on Singly Linked List Cont.

Algorithms and data structures - First semester 2023-2024 24


Basic operations on Singly Linked List Cont.
#include <iostream>
void begdelete()
using namespace std;
{
//node structure
struct Node *ptr;
struct Node {
if(head == NULL)
int data;
{
Node* next;
cout << "\nList is empty";
};
}
class LinkedList {
else
private:
{
Node* head;
ptr = head;
public:
head = ptr->next;
LinkedList(){
free(ptr);
head = NULL;
cout <<"\n Node deleted from the begining
}
...";
//fill the list with the elements
} }
void fill_List(int newElement) {
Node* newNode = new Node();
newNode->data = newElement;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
} else {
Node* temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
}
Algorithms and data structures - First semester 2023-2024 25
Basic operations on Singly Linked List Cont.
//display the content of the list // test the code
void PrintList() { int main() {
Node* temp = head; LinkedList MyList;
if(temp != NULL) { //Add three elements at the end of the
list.
MyList.fill_List(10);
while(temp != NULL) { MyList.fill_List(20);
cout<<temp->data<<" "; MyList.fill_List(30);
temp = temp->next; cout << "the list before deletion ";
} MyList.PrintList();
cout<<endl; //delete an element
} else { MyList.begdelete();
cout<<"The list is empty.\n"; cout << "the list after deletion : ";
} MyList.PrintList();
}
}; return 0;
}

Algorithms and data structures - First semester 2023-2024 26


Basic operations on Singly Linked List Cont.

• Deletion in singly linked list at the end


• Two scenarios
1. There is only one node in the list and that needs to be deleted.
2. There are more than one node in the list and the last node of the list will be deleted.
• Algorithm
• STEP 1: Start
• STEP 2: IF HEAD = NULL, Write UNDERFLOW, Go to Step 10.
• STEP 3 : IF HEAD -> NEXT = NULL, HEAD = NULL, FREE HEAD, GO to Step 10.
• STEP 4: SET PTR = HEAD
• STEP 5: Repeat Steps 6 AND 7 while PTR -> NEXT!= NULL
• STEP 6: SET PTR1 = PTR
• STEP 7: SET PTR = PTR -> NEXT
• STEP 8: SET PTR1 -> NEXT = NULL
• STEP 9: FREE PTR
• STEP 10: End

Algorithms and data structures - First semester 2023-2024 27


Basic operations on Singly Linked List Cont.

Algorithms and data structures - First semester 2023-2024 28


Basic operations on Singly Linked List Cont.
void end_delete()
#include <iostream> {
using namespace std; struct Node *ptr,*ptr1;
//node structure if(head == NULL)
struct Node { {
int data; cout << "\nlist is empty";
Node* next; }
}; else if(head -> next == NULL)
class LinkedList { {
private: head = NULL;
Node* head; free(head);
public: cout << "\nOnly node of the
LinkedList(){ list deleted ...";
head = NULL; }
} else
//fill the list with the elements {
void fill_List(int newElement) { ptr = head;
Node* newNode = new Node(); while(ptr->next != NULL)
newNode->data = newElement; {
newNode->next = NULL; ptr1 = ptr;
if(head == NULL) { ptr = ptr ->next;
head = newNode; }
} else { ptr1->next = NULL;
Node* temp = head; free(ptr);
while(temp->next != NULL) cout << "\n Deleted Node
temp = temp->next; from the last ...";
temp->next = newNode; }
} }
}

Algorithms and data structures - First semester 2023-2024 29


Basic operations on Singly Linked List Cont.
//display the content of the list // test the code
void PrintList() { int main() {
Node* temp = head; LinkedList MyList;
if(temp != NULL) { //Add three elements at the end of the
list.
MyList.fill_List(10);
while(temp != NULL) { MyList.fill_List(20);
cout<<temp->data<<" "; MyList.fill_List(30);
temp = temp->next; cout << "the list before deletion ";
} MyList.PrintList();
cout<<endl; //delete an element
} else { MyList.end_delete();
cout<<"The list is empty.\n"; cout << "the list after deletion : ";
} MyList.PrintList();
}
}; return 0;
}

Algorithms and data structures - First semester 2023-2024 30


Basic operations on Singly Linked List Cont.

• Deletion in singly linked list in a specified location


• In order to delete the node, which is present in a specified location, We need
to keep track of the two nodes.
• The one which is to be deleted
• The other one if the node which is present before that node.
• For this purpose, two pointers are used: ptr and ptr1.

Algorithms and data structures - First semester 2023-2024 31


Basic operations on Singly Linked List Cont.

• Algorithm
• STEP 1: START
• STEP 2: IF HEAD = NULL, WRITE UNDERFLOW, GOTO STEP 12
• STEP 3: SET PTR = HEAD
• STEP 4: SET I = 0, ENTER LOC.
• STEP 5: REPEAT STEP 6 TO 9 WHILE I < LOC
• STEP 6: PTR1 = PTR
• STEP 7: PTR = PTR → NEXT
• STEP 8: IF PTR = NULL, WRITE "DESIRED NODE NOT PRESENT”,
GOTO STEP 12
• STEP 9: I = I+1
• STEP 10: PTR1 → NEXT = PTR → NEXT
• STEP 11: FREE PTR
• STEP 12: END

Algorithms and data structures - First semester 2023-2024 32


Basic operations on Singly Linked List Cont.

Algorithms and data structures - First semester 2023-2024 33


Basic operations on Singly Linked List Cont.
#include <iostream>
using namespace std;
void delete_specified()
//node structure
{
struct Node {
struct Node *ptr, *ptr1;
int data;
int loc,i;
Node* next;
cout << "Enter location ";
};
cin >> loc;
class LinkedList {
ptr=head;
private:
for(i=0;i<loc;i++)
Node* head;
{
public:
ptr1 = ptr;
LinkedList(){
ptr = ptr->next;
head = NULL;
}
//fill the list with the elements
if(ptr == NULL)
void fill_List(int newElement) {
{
Node* newNode = new Node();
cout << "\nThere are less than " <<
newNode->data = newElement;
loc <<" elements in the list..\n";
newNode->next = NULL;
return;
if(head == NULL) {
}
head = newNode;
}
} else {
ptr1 ->next = ptr ->next;
Node* temp = head;
free(ptr);
while(temp->next != NULL)
cout << "\nDeleted "<< loc<< " node ";
temp = temp->next;
}
temp->next = newNode;
}
}
Algorithms and data structures - First semester 2023-2024 34
Basic operations on Singly Linked List Cont.
//display the content of the list // test the code
void PrintList() { int main() {
Node* temp = head; LinkedList MyList;
if(temp != NULL) { //Add three elements at the end of the
list.
while(temp != NULL) { MyList.fill_List(10);
cout<<temp->data<<" "; MyList.fill_List(20);
temp = temp->next; MyList.fill_List(30);
} cout << "the list before deletion ";
cout<<endl; MyList.PrintList();
} else { //delete an element
cout<<"The list is empty.\n"; MyList. delete_specified();
} cout << "the list after deletion : ";
} MyList.PrintList();
};
return 0;
}

Algorithms and data structures - First semester 2023-2024 35


Basic operations on Singly Linked List Cont.

• Traversing in singly linked list


• Traversing means visiting each node of the list once in order to perform some
operation on that
• Algorithm
• STEP 1: START
• STEP 2: SET PTR = HEAD
• STEP 3: IF PTR = NULL, WRITE "EMPTY LIST”, GOTO STEP 7
• STEP 4: REPEAT STEPS 5 AND 6 WHILE PTR != NULL
• STEP 5: PRINT PTR→ DATA
• STEP 6: PTR = PTR → NEXT
• STEP 7: END

Algorithms and data structures - First semester 2023-2024 36


Basic operations on Singly Linked List Cont.
#include <iostream> //display the content of the list
using namespace std; void PrintList() {
//node structure Node* temp = head;
struct Node { if(temp != NULL) {
int data;
Node* next; while(temp != NULL) {
}; cout<<temp->data<<" ";
class LinkedList { temp = temp->next;
private: }
Node* head; cout<<endl;
public: } else {
LinkedList(){ cout<<"The list is empty.\n";
head = NULL; }
} }
//fill the list with the elements };
void fill_List(int newElement) { // test the code
Node* newNode = new Node(); int main() {
newNode->data = newElement; LinkedList MyList;
newNode->next = NULL; //Add three elements at the end of the list.
if(head == NULL) { MyList.fill_List(10);
head = newNode; MyList.fill_List(20);
} else { MyList.fill_List(30);
Node* temp = head; cout << "print the list ";
while(temp->next != NULL) MyList.PrintList();
temp = temp->next;
temp->next = newNode; return 0;
} }
}
Algorithms and data structures - First semester 2023-2024 37
Basic operations on Singly Linked List Cont.

• Searching in singly linked list


• Searching is performed in order to find the location of a particular element in
the list.
• Searching any element in the list needs traversing through the list and make
the comparison of every element of the list with the specified element.
• If the element is matched with any of the list element then the location of the
element is returned from the function.

Algorithms and data structures - First semester 2023-2024 38


Basic operations on Singly Linked List Cont.

• Algorithm
• Step 1: START
• Step 2: SET PTR = HEAD
• Step 3: Set I = 0
• STEP 4: IF PTR = NULL, WRITE "EMPTY LIST”, GOTO STEP 9.
• STEP 5: REPEAT STEPS 6 TO 8 WHILE PTR != NULL
• STEP 6: IF PTR - > DATA = ITEM, RETURN I
• STEP 7: I = I + 1
• STEP 8: PTR = PTR - > NEXT
• STEP 9: END

Algorithms and data structures - First semester 2023-2024 39


Basic operations on Singly Linked List Cont.
#include <iostream> void search()
using namespace std; {
//node structure struct Node *ptr;
struct Node { int item,i=0,flag;
int data;
ptr = head;
Node* next;
if(ptr == NULL)
};
class LinkedList { {
private: cout << "\nEmpty List\n";
Node* head; }
public: else
LinkedList(){ {
head = NULL; cout << "\nEnter item which you
} want to search?\n";
//fill the list with the elements cin >> item;
void fill_List(int newElement) { while (ptr!=NULL)
Node* newNode = new Node(); {
newNode->data = newElement;
newNode->next = NULL;
if(ptr->data == item)
if(head == NULL) { {
head = newNode; cout << "item found at
} else { location " << i << endl;
Node* temp = head;
while(temp->next != NULL) }
temp = temp->next; i++;
temp->next = newNode; ptr = ptr -> next;
} } } }
}
Algorithms and data structures - First semester 2023-2024 40
Basic operations on Singly Linked List Cont.
//display the content of the list // test the code
void PrintList() { int main() {
Node* temp = head; LinkedList MyList;
if(temp != NULL) { //Add three elements at the end of the
list.
while(temp != NULL) { MyList.fill_List(10);
cout<<temp->data<<" "; MyList.fill_List(20);
temp = temp->next; MyList.fill_List(30);
} // print the list
cout<<endl; MyList.PrintList();
} else { // invoke the search function
cout<<"The list is empty.\n"; MyList.search();
}
} return 0;
}; }

Algorithms and data structures - First semester 2023-2024 41


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 42

You might also like