0% found this document useful (0 votes)
38 views14 pages

ASD2 Linked List

Linked List in c

Uploaded by

yanis xo
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)
38 views14 pages

ASD2 Linked List

Linked List in c

Uploaded by

yanis xo
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/ 14

Algiers 1 University

Faculty of Sciences
Computer Science department
1st year

Algorithmic and Data Structure 2


Linked List

2023/2024
CONTENTS
 linked List definition
 Structure of a list
 Construction of a node
 Handling simple linked lists
 Construction a list
 Insertion at “Head” of list

 Insertion into “Tail” of list

 Adding elements to a list


 Add to start of list (Head)

 Add to middle of list

 Add to end of list (Tail)

 Removing elements from a list


 Delete first element (Head)

 Delete an element in the middle of the list

 Delete last element (Tail)


LINKED LIST DEFINITION
 A linked list is a sequence of objects of the same
type accessible one by one from the first to the
last element.
Une liste chaînée est une séquence d'objets de même type
accessibles un à un du premier au dernier élément.
 The linked list is a dynamic data structure, that
is to say it allows memory allocations according
to demand.
La liste chaînée est une structure de données dynamique, c'est-
à-dire qu’elle permet des allocations de mémoire en fonction de
la demande.
 The size of the data or the list is not fixed in
advance , as is the case with arrays (which are
static data structures).
La taille des données ou de la liste n'est pas fixée à l'avance,
comme c'est le cas des tableaux (qui sont des structures de
données statiques).
STRUCTURE OF A LIST
 A linked list is a data structure, made up of
elements, called a “node”.
Une liste chaînée est une structure de données, composée
d'éléments, appelée nœud.
 Each node is made up of three parts, where each
part has its own importance:
3rd parts Address of the node

Data Link

1st part Part 2


Address of the next
Actual data node
STRUCTURE OF A LIST
 1st part (Data): This is the useful part which constitutes
the data of the node. The data can be structured or simple.
1ère partie (Données) : C'est la partie utile qui constitue les données du
nœud. Les données peuvent être structurées ou simples
 Part 2 (Link): It is this part which makes it possible to
make the links between the nodes. It is a pointer
containing the next node's address (if it exists) in the list.
Partie 2 (suivant) : C'est cette partie qui permet de faire les liens entre les
nœuds. C'est un pointeur contenant l'adresse du nœud suivant (s'il existe)
dans la liste
 3rd parts (Address): This part represents the address of
the node itself.
3ème partie (Adresse) : Cette partie représente l'adresse du nœud lui-
même
4 2 1 3 6

Head Nil
CONSTRUCTION OF A NODE

 The construction of a node requires the declaration of a


record type composed of:
 One or more data fields.
 A variable named "next" of the pointer type contains the
address of the next element in the list.

La construction d'un nœud nécessite la déclaration d'un


type d'enregistrement composé de :
 Un ou plusieurs champs de données.
 Une variable nommée "next" de type pointeur contient l'adresse de
l'élément suivant de la liste.
CONSTRUCTION OF A NODE
 The following example shows how to construct a
node that contains the “Hello!”.

Algorithm NewNode ;
Type
List=^Node;
Node = record
x: string;
Next: List;
End;
Var L
L:List;
Begin L
Allocate (L); Hello!
L^.x ‘’Hello !’’ ;
L^.Next Nil ;
End.
HANDLING SIMPLE LINKED LISTS
 A linked list can be composed:
 One or more nodes linked together by “next” pointers.
 A pointer type variable, called “head” contains the
address of the first element of the chain.
Une liste chaînée peut être composée.
- Un ou plusieurs nœuds reliés entre eux par des pointeurs « suivant ».
- Une variable de type pointeur, appelée « head » contient l'adresse du
premier élément de la chaîne.

 The following diagram shows a representation of


a simple linked list composed of 5 nodes:

L
024
024

4 2 1 3 6

Head of list Nil


HANDLING SIMPLE LINKED LISTS

 The linked list “L” is composed of the following


elements:
 An “L” pointer called the head of the list. It always
points to the first element in the list.
 The “next” pointer of each element contains the
address of the next element.
 The "next" pointer of the last element in the list
contains the value "Nil". The latter doesn't point
anywhere. It allows you to determine the end of a
list.
 A list is said to be empty if the “L” is “nil” (L = nil).
That is to say, the head is not pointing anywhere.
CONSTRUCTION A LIST
 A list can be constructed using two methods. By
inserting the nodes into:
 Head of the list.
 End of the list.

 Une liste peut être construite en utilisant deux méthodes


En insérant les nœuds dans:
 tête de liste.
 Fin de la liste.
CONSTRUCTION A LIST
Insertion at “Head” of list:

This method inserts the nodes by shifting the


existing ones to the right. To do this, we need,
initially, two pointers:
 The first pointer to point to the head of the list.
 The second pointer moves the first pointer to the new
node.
The following algorithm shows how to
manipulate these two pointers:
CONSTRUCTION A LIST
Insertion at “Head” of list:
Algorithm List1 ;
Type p^.a x ;
List = ^Node;
p^.Next L ;
Node = record
a : integer; Lp;
Next: List;
End; Next i;
Var End For ;
L, p :List; i, N, x : integer; End.
Begin
Write (‘’give the number of nodes in the new list’’);
Read(N);
LNil;
For i1 to N step 1
Write (‘Give a value’);
Read(x);
Allocate (P);
CONSTRUCTION A LIST
Insertion into “Tail” of list:

This method inserts the nodes at the end of the


list. To do this, we need, initially, three pointers:
 The first pointer to point to the head of the list.
 The second pointer moves the first pointer to the new
node.
 The third pointer to save the address of the previous
node.
The following algorithm shows how to manipulate
these three pointers:
CONSTRUCTION A LIST
Insertion into “Tail” of list:
Algorithm List2 ; For i1 to N-1 step 1
Type Write (‘’Give a value’’);
Read(x);
List =^Node;
r  p;
Node = record
a : integer; Allocate (p);
Next: List; p^.a x ;
End; p.NextNil;
Var
L,p,r :List; i,N,x : integer; r^.Next p ;
Begin Next i;
Write (‘’give the number of nodes in the new list’’); End For ;
Read(N); End.
Write (‘Give a value’);
Read(x);
Allocate (L);
L^.a x ;
L^.Next Nil ;

pL;

You might also like