0% found this document useful (0 votes)
8 views7 pages

Chapitre 02

Uploaded by

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

Chapitre 02

Uploaded by

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

UMBB/ FS/ DI

The essentials on linked Lists(Version 2)

1.Introduction to Linked Lists


A linked list is a linear data structure where elements are not stored in contiguous memory
locations. Instead, each element, called a node, contains data and a pointer to the next node in
the list. This allows for dynamic memory allocation and efficient insertion and deletion of
elements.

Advantages of Linked Lists:


• Dynamic Size: Linked lists can expand and contract dynamically as needed.
• Efficient Insertion and Deletion: Inserting or deleting elements at the beginning or any
specific position is relatively efficient.
• Flexible Memory Allocation: Memory is allocated for each node individually, leading to
efficient memory utilization.

Disadvantages of Linked Lists:


• Random Access Inefficiency: Accessing a specific element requires traversing the list
from the beginning, making it slower than arrays for random access.
• Extra Memory Overhead: Each node requires additional memory for the pointer.

Types of Linked Lists:

1. Singly Linked List (simple linked list):


• Each node points to the next node.

Ex: A list with 3 nodes

head

100
data

14 110 3 111 16 0

DRIFA HADJIDJ 1
2. Doubly Linked List (Bidirectional linked list):
• Each node points to both the next and previous nodes.
• More flexible for insertion and deletion at any position.

3. Circular Linked List:


• The last node points back to the first node, forming a circular structure.

4. Circular Doubly Linked List:


• Combines the features of circular and doubly linked lists, allowing traversal in
both directions and forming a circular structure.

NB. For specific requirements of an application, we can choose the appropriate type of linked
list to optimize performance and memory .

Basic Operations on Linked Lists


a) Insertion:
• At the beginning
• At the end
• At a specific position
b) Deletion:
• At the beginning
• At the end
• At a specific position:
c) Traversal:
• Starts from the first node and iterate through the list, following the next
pointers.

Applications of Linked Lists


• Implementing stacks and queues
• Representing polynomials
• Representing graphs
• Garbage collection
• Task Scheduling
• Etc…

DRIFA HADJIDJ 2
2. Simple linked lists
Declaration
struct node
{
int data ;// The type can be simple or compound
node* next;
};

Primitives
Initialize a list
void initialize(node* &head)
{
head =NULL ;
}

Insert a node in the list at the beginning


void insertBiginning(node* &head, int x)
{
node* nouveau=new node;
nouveau -> data=x; // Fill the new node
nouveau -> next=NULL;

if (head ==NULL)
{
head =nouveau; // Updating the address
}
else
{
nouveau -> next= head;
head=nouveau;
}
}

Insert a node at the end of the list


void insertEnd(node* &head, int x)
{
node* nouveau=new node;
nouveau ->data=x;
nouveau ->next=NULL;
if (head ==NULL)
{
head =nouveau;
}
else

DRIFA HADJIDJ 3
{
node* current= head;
while(current ->next!=NULL)//to reach the last node in the list
{
current = current ->next;
}
current ->next=nouveau;
}
}

Insert a node in the list after a node whose address is current


void insertAfter(node* current, int x)
{
node* nouveau=new node;
nouveau ->data=x;
nouveau->next=current->next;
current->next= nouveau;
}

Delete a node at the beginning of the list


void deletionBiginning( node* &head)
{
if (head==NULL)
{
cout<<"Empty list "<<endl;
}
else
{
node *temp=head;
head=head->next; //if the list contains at least 1 node
delete temp;
}
}

Delete a node at the end of the list


void deletionEnd( node* &head)
{
if(head==NULL)
{
Cout<<" Empty list" <<endl;
}
else if (head->next==NULL) // If the list contains 1 node
{
delete head;
head=NULL;
}

DRIFA HADJIDJ 4
else
{
node* current=head;
while (current->next ->next != NULL)
{
current= current->next;
}
delete current-> next;
current-> next=NULL;
}
}

Delete a node that follows the node whose address is current


void DeletionAfterCurrent(node* current)
{
node* sup=current->next;
current->next= current->next->next;
delete sup;

Traversal of a list
void traverse(node* head)
{
node* current;
current=head;
while(current!=NULL)
{
cout<< current->data;
current=current->next;
}
}

3.Bidirectional linked lists


Declaration

struct* node
{
int data;// The type can be simple or compound
node* precedent;
node* next;
}

Primitives

Insert a node at the beginning of the list


void insertBiginningB(node* &head, int x)
{

DRIFA HADJIDJ 5
node* nouveau=new node;
nouveau ->data=x;
nouveau->precedent=NULL;

if(head==NULL)
{
nouveau->next=NULL;
head=nouveau;
}
else
{
head->precedent=nouveau;
nouveau->next=head;
head=nouveau;
}
}

Insert a node at the end of the list


void insertEndB(node* &head, int x)
{
node* nouveau=new node;
nouveau ->data=x;

if(head==NULL)
{
nouveau->next=NULL;
nouveau->precedent=NULL;
head=nouveau;
}
else
{
node* current=head;
while(current->next!=NULL)
Current=current->next;
Current->next=nouveau;
nouveau->precedent=current;
nouveau->next=NULL;
}
}

Insert a node after the node whose address is current


void insertAfterB(node* &current, int x)
{
node* nouveau=new node;
nouveau ->data=x;
nouveau->next=current->next;
nouveau->next->precedent=nouveau;
nouveau->precedent=current;
current->next=nouveau;
DRIFA HADJIDJ 6
}

Delete a node at the beginning of the list


void deletionBiginningB(node* &head)
{
if (head==NULL)
cout<< "empty list ";
else if (head->next==NULL)
{
delete head;
head=NULL;
}
else
{
node* temp=head;
head=head-> next;
head->precedent=NULL;
delete temp;
}
}

Delete a node at the end of the list


void deletionEndB(node* &head)
{
if (head==NULL)
cout<<" Liste vide " ;
else if (head->next==NULL)
{
delete head;
head=NULL;
}
else
{
node* current=head;
while(current->next !=NULL)
current=current->next;
current->precedent->next=NULL;
delete current;
}
}

Delete a node whose address is current


Void deletionCurrent(node* &current)
{
current->precedent->next=current->next;
current->next->precedent=current->precedent;
delete current;
}

DRIFA HADJIDJ 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy