Chapitre 02
Chapitre 02
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.
NB. For specific requirements of an application, we can choose the appropriate type of linked
list to optimize performance and memory .
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 ;
}
if (head ==NULL)
{
head =nouveau; // Updating the address
}
else
{
nouveau -> next= head;
head=nouveau;
}
}
DRIFA HADJIDJ 3
{
node* current= head;
while(current ->next!=NULL)//to reach the last node in the list
{
current = current ->next;
}
current ->next=nouveau;
}
}
DRIFA HADJIDJ 4
else
{
node* current=head;
while (current->next ->next != NULL)
{
current= current->next;
}
delete current-> next;
current-> next=NULL;
}
}
Traversal of a list
void traverse(node* head)
{
node* current;
current=head;
while(current!=NULL)
{
cout<< current->data;
current=current->next;
}
}
struct* node
{
int data;// The type can be simple or compound
node* precedent;
node* next;
}
Primitives
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;
}
}
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;
}
}
DRIFA HADJIDJ 7