TP Algo

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include <stdio.

h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Node {


char data;
struct Node* next;
} Node;

void trierListe(Node** head) {


int swapped;
Node *ptr1;
Node *lptr = NULL;

if (*head == NULL)
return;

do {
swapped = 0;
ptr1 = *head;

while (ptr1->next != lptr) {


if (ptr1->data > ptr1->next->data) {
char temp = ptr1->data;
ptr1->data = ptr1->next->data;
ptr1->next->data = temp;
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
} while (swapped);
}
void ajouterTrie(Node** head, char value) {
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = value;
new_node->next = NULL;

if (*head == NULL || (*head)->data >= value) {


new_node->next = *head;
*head = new_node;
} else {
Node* current = *head;
while (current->next != NULL && current->next->data < value) {
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
void inverserListe(Node** head) {
Node *prev = NULL;
Node *current = *head;
Node *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
int* compterOccurrences(Node* head) {
int* occurrences = (int*)calloc(26, sizeof(int)); // 26 lettres de l’alphabet

while (head != NULL) {


if (isalpha(head->data)) {
int index = tolower(head->data) - ’a’;
occurrences[index]++;
}
head = head->next;
}

return occurrences;
}

void supprimerDoublons(Node** head, char value) {


Node* current = *head;
Node* temp = NULL;

while (current != NULL && current->data == value) {


temp = current;
current = current->next;
free(temp);
}

*head = current;

while (current != NULL && current->next != NULL) {


if (current->next->data == value) {
temp = current->next;
current->next = temp->next;
free(temp);
} else {
current = current->next;
}
}
}

int main() {
Node* myList = NULL;
ajouterTrie(&myList, ’d’);
ajouterTrie(&myList, ’a’);
ajouterTrie(&myList, ’c’);
ajouterTrie(&myList, ’b’);
ajouterTrie(&myList, ’a’);
ajouterTrie(&myList, ’d’);
printf("Liste initiale : ");
Node* current = myList;
while (current != NULL) {
printf("%c ", current->data);
current = current->next;
}
printf("\n");
trierListe(&myList);
printf("Liste trie : ");
current = myList;
while (current != NULL) {
printf("%c ", current->data);
current = current->next;
}
printf("\n");
inverserListe(&myList);
printf("Liste inverse : ");
current = myList;
while (current != NULL) {
printf("%c ", current->data);
current = current->next;
}
printf("\n");
int* occurrences = compterOccurrences(myList);
printf("Occurrences de chaque caractre : ");
for (int i = 0; i < 26; i++) {
if (occurrences[i] > 0) {
printf("%c:%d ", ’a’ + i, occurrences[i]);
}
}
printf("\n");
supprimerDoublons(&myList, ’a’);
printf("Liste aprs suppression des doublons de ’a’ : ");
current = myList;
while (current != NULL) {
printf("%c ", current->data);
current = current->next;
}
printf("\n");

free(myList);
free(occurrences);

return 0;
}

You might also like