Salut à tous
Je n'avais encore jamais étudié et encore moins travaillé avec des listes chainées, j'ai décidé de combler cette lacune.
A titre d'exercice je me suis donc monté une petite librairie pour les listes simplement chainées, dont voici quelques samples de code:
Dans le .h
Dans le .c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 /* One node of a Singly linked List */ typedef struct SNode SNode; struct SNode { /* The data being stored in the node */ int16u Data; /* A pointer to the next node, NULL if last node */ SNode* Next; }; /* Pointer to the Linked list (= pointer to the first node) */ typedef SNode* SListPtr; /****************************************************************************** * Add a node at the beginning of a list * * @param ListPtr pointer to the linked list * ui16Value Data to store in the first node * * @return the pointer to the modified list if success (= ptr to the 1st node ) * NULL if failure *****************************************************************************/ SListPtr SLL_InsertNode(SListPtr ListPtr, int16u ui16Value); /****************************************************************************** * Add a node after a specified Node * * @param Node Pointer to the node after which the new one will be * inserted * ui16Value Data to store in the new node * * @return the pointer to the new node if success * NULL if failure *****************************************************************************/ SNode* SLL_InsertNodeAfter(SNode* Node, int16u ui16Value); /****************************************************************************** * Locate the first occurence of a value in a specified list. * * @param LisPtr pointer to the linked list * ui16Value The value to find * * @return If SUCCESS returns a pointer to the node containing ui16Value * Otherwise it returns a NULL pointer. *****************************************************************************/ SNode* SLL_SearchValue(SListPtr ListPtr, int16u ui16Value); /****************************************************************************** * Remove the first node of a linked list. * * @param ListPtr pointer to the linked list * * @return the pointer to the new list if success (= ptr to the 1st node) * NULL if failure or if list had only 1 node *****************************************************************************/ SListPtr SLL_RemoveFirstNode(SListPtr ListPtr);
J'aimerais maintenant rendre ma structure "SNode" générique, de façon à pouvoir l'utiliser avec n'importe quel type de donnée (char, int, long, etc.), selon les besoins de l'utilisateur, sachant que toutes mes fonctions qui manipulent le champs "Data" ne réalisent strictement que de la copie (du paramètre de la fonction vers le nouveau noeud) ou de la lecture (recherche d'occurrence ou retour du champs "Data" du noeud N).
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 SListPtr SLL_InsertNode(SListPtr ListPtr, int16u ui16Value) { SListPtr NewNode = malloc(sizeof(SNode)); if(NewNode != NULL) { /* If memory correctly allocated: - store the value in this new node - place this node at the beginning of the list */ NewNode->Data = ui16Value; NewNode->Next = ListPtr; } return NewNode; } SNode* SLL_InsertNodeAfter(SNode* Node, int16u ui16Value) { SNode* NewNode = NULL; if(Node != NULL) { NewNode = malloc(sizeof(SNode)); if(NewNode != NULL) { /* If memory correctly allocated: - the previous element now become the next one - store value in this new node - previous node points to the new one */ NewNode->Next = Node->Next; NewNode->Data = ui16Value; Node->Next = NewNode; } } return NewNode; } SListPtr SLL_SearchValue(SListPtr ListPtr, int16u ui16Value) { /* Read the whole list to find ui16Value in a Node */ while(ListPtr != NULL && ListPtr->Data != ui16Value) { ListPtr = ListPtr->Next; }; return ListPtr; } SListPtr SLL_RemoveFirstNode(SListPtr ListPtr) { SListPtr TmpPtr = NULL; if(ListPtr != NULL) { /* Get the pointer to the second node and delete the 1st node */ TmpPtr = ListPtr->Next; free(ListPtr); } return TmpPtr; }
Je n'ai vraiment aucune idée de comment y parvenir (j'espère au moins que c'est possible), je n'ai rien trouvé dans mes notes que j'ai faites sur le C.
Toutes critiques (constructive) sur le code seront aussi les bienvenues
Merki !![]()
Partager