Ajouter ex4.c
This commit is contained in:
parent
31de6e7919
commit
2da2a8f7f5
85
ex4.c
Normal file
85
ex4.c
Normal file
@ -0,0 +1,85 @@
|
||||
#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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user