Commit cef7fd9f authored by fabio.rodrigue1's avatar fabio.rodrigue1
Browse files

Fixed Free and Init

parent a7a322f6
File added
main: main.o circ_list.o
gcc main.o circ_list.o -o main -lm
Joseph: joseph.o circ_list.o
gcc joseph.o circ_list.o -o Joseph -fsanitize=address -fsanitize=leak -lm -g
circ_list.o:
gcc -c circ_list.c
main.o:
gcc -c main.c
joseph.o:
gcc -c joseph.c
clean:
rm -f *.o main
rebuild: clean main
\ No newline at end of file
rm -f *.o Joseph
rebuild: clean Joseph
\ No newline at end of file
......@@ -6,9 +6,7 @@
//Crée une nouvelle liste vide. Renvoie simplement un pointeur NULL
element* list_create(){
element *e;
e->next = malloc(sizeof(element *));
return e;
return NULL;
}
//Reste si la liste est vide
bool list_empty(element* head){
......@@ -40,26 +38,102 @@ element* list_move(element* head){
}
//Insère un élément après le pointeur head. Retourne le pointeur sur l’élément inséré
element* list_insert_after(element* head, int data){
element *insert = malloc(sizeof(element));
insert->data = data;
if (list_empty(head)) {
head = insert;
insert->next = head;
} else {
insert->next = head->next;
head->next = insert;
}
return insert;
/* element *new_e = malloc(sizeof(element));
new_e->data = data;
if(list_empty(head))
{
head->next = list_create();
head=new_e;
new_e->next = head;
}
else {
element *temp;
if(head->next->next != NULL)
{
temp = head->next->next;
new_e->next = temp;
}
head->next = new_e;
}
element *new_e = head->next;
new_e->next = list_create();
new_e->data = data;
return new_e;
return new_e; */
}
//Insère un élément avant le pointeur head. Retourne le pointeur sur l’élément inséré
element* list_insert_before(element* head, int data){
element *runner = head;
if(list_empty(head))
{
element *new_e = malloc(sizeof(element));
new_e->data = data;
new_e->next = head;
return head = new_e;
}
else{
while(runner->next != head)
{
runner = runner->next;
}
runner->next = list_insert_after(runner, data);
return runner;
}
}
//Recherche un élément dans la liste. Retourne un pointeur sur le 1er élément trouvé ou NULL si l’élément est absent
element* list_search(element* head, int data);
element* list_search(element* head, int data){
element *runner = head;
while(runner->next != NULL){
if(runner->data == data)
return runner;
runner = runner->next;
}
return NULL;
}
//Supprime un élément de la liste sans libérer l’élément pointé. Renvoie un pointeur sur le 1er élément trouvé à supprimer et l’enlève de la liste, ou NULL si l’élément est absent
element* list_remove(element* head, int data);
element* list_remove(element* head, int data){
element *runner = head;
element *e = list_search(head, data);
while(runner->next != head){
if(runner->next->data == data && runner->next->next != NULL)
{
runner->next = runner->next->next;
}
else {
runner->next = NULL;
}
runner = runner->next;
}
return e;
}
//Supprime toute la liste en libérant chaque élément
void list_free(element* head);
void list_free(element* head){
element *runner = head;
element *first = head;
do
{
element *temp = runner;
runner = runner->next;
free(temp);
} while(runner!=first);
}
// Appel d’une fonction qui modifie tous les éléments de la liste grâce à la fonction action()
void list_process(element* head, int (*action)(int)){
if(!list_empty(head)){
element *runner = head;
while(head->next!=NULL)
{
runner->data =action(runner->data);
runner = runner->next;
}
}
}
\ No newline at end of file
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>
#include "circ_list.h"
void main(int argc, char ** argv)
{
if(argc==3)
{
int n = atoi(argv[1]);
int k = atoi(argv[2]);
// k %=n;
element* list = list_create();
//element* insert = list;
for(int i = 1; i<=n;i++){
list = list_insert_after(list, i);
}
list_free(list);
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment