Supprimer ex4.c

This commit is contained in:
Mazen BEN GHORBAL 2024-12-10 15:32:10 +01:00
parent 29a8c0fdaf
commit 50f6a47e7a

87
ex4.c
View File

@ -1,87 +0,0 @@
LE NOM DES VARIABLES SONT EN ANGLAIS TRADUIT LES
#include <stdio.h>
#include <stdlib.h>
typedef struct Point {
float x, y;
} Point;
typedef struct Node {
Point point;
struct Node *next;
} Node;
Node* createNode(float x, float y) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Erreur d'allocation mémoire\n");
exit(1);
}
newNode->point.x = x;
newNode->point.y = y;
newNode->next = NULL;
return newNode;
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("(%.2f, %.2f) -> ", current->point.x, current->point.y);
current = current->next;
}
printf("NULL\n");
}
void removeNegativePoints(Node** headRef) {
Node* current = *headRef;
Node* prev = NULL;
while (current != NULL) {
if (current->point.x < 0 && current->point.y < 0) {
Node* temp = current;
if (prev == NULL) {
*headRef = current->next;
} else {
prev->next = current->next;
}
current = current->next;
free(temp);
} else {
prev = current;
current = current->next;
}
}
}
int main() {
Node* head = createNode(1.0, 2.0);
head->next = createNode(-1.0, -3.0);
head->next->next = createNode(4.0, -2.0);
head->next->next->next = createNode(-2.0, -2.0);
head->next->next->next->next = createNode(5.0, 6.0);
printf("Liste avant la suppression des points négatifs :\n");
printList(head);
removeNegativePoints(&head);
printf("Liste après la suppression des points négatifs :\n");
printList(head);
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}