moitié tp 3
This commit is contained in:
19
prepa_CM3/ex3_A/makefile
Normal file
19
prepa_CM3/ex3_A/makefile
Normal file
@@ -0,0 +1,19 @@
|
||||
but : ex3
|
||||
|
||||
OBJS = stack_operations.o\
|
||||
stack.o
|
||||
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -ansi -pedantic
|
||||
|
||||
|
||||
stack_operations.o: stack.h
|
||||
|
||||
stack.o : stack.h
|
||||
|
||||
ex3: $(OBJS)
|
||||
$(CC) $(CFLAGS) -o ex3 $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) ex3
|
||||
60
prepa_CM3/ex3_A/stack.c
Normal file
60
prepa_CM3/ex3_A/stack.c
Normal file
@@ -0,0 +1,60 @@
|
||||
#include <stdlib.h>
|
||||
#include "stack.h"
|
||||
|
||||
struct s_link {
|
||||
unsigned value;
|
||||
struct s_link *next;
|
||||
};
|
||||
|
||||
typedef struct s_link link;
|
||||
|
||||
struct s_stack {
|
||||
link *first;
|
||||
};
|
||||
|
||||
|
||||
/* crée une pile vide */
|
||||
stack create_stack(void) {
|
||||
return (stack) calloc(1, sizeof(struct s_stack));
|
||||
}
|
||||
|
||||
/* ajoute un élément à la pile. Renvoie 1 en cas de succès */
|
||||
int push(stack the_stack, unsigned the_value) {
|
||||
link *new = (link*) malloc(sizeof(link));
|
||||
if (new == NULL) {
|
||||
return 0;
|
||||
}
|
||||
new->value = the_value;
|
||||
new->next = the_stack->first;
|
||||
the_stack->first = new;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* renvoie 1 si la pile est vide */
|
||||
int empty(stack the_stack) {
|
||||
return the_stack->first == NULL;
|
||||
}
|
||||
|
||||
/* retire un élément de la pile. Renvoie l'élément retiré, ou -1 en cas d'échec */
|
||||
long pop(stack the_stack) {
|
||||
if(the_stack->first == NULL) {
|
||||
return -1;
|
||||
}
|
||||
link l = *(the_stack->first);
|
||||
free(the_stack->first);
|
||||
the_stack->first = l.next;
|
||||
return l.value;
|
||||
}
|
||||
|
||||
/* détruit une pile en libérant les ressources associées */
|
||||
void destroy_stack(stack the_stack) {
|
||||
link *current, *saved;
|
||||
|
||||
current = the_stack->first;
|
||||
while(current != NULL) {
|
||||
saved = current->next;
|
||||
free(current);
|
||||
current = saved;
|
||||
}
|
||||
free(the_stack);
|
||||
}
|
||||
22
prepa_CM3/ex3_A/stack.h
Normal file
22
prepa_CM3/ex3_A/stack.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
/* le type stack représente une pile */
|
||||
typedef struct s_stack *stack;
|
||||
|
||||
/* crée une pile vide. Renvoie NULL en cas d'échec */
|
||||
stack create_stack(void);
|
||||
|
||||
/* ajoute un élément à la pile. Renvoie 0 en cas d'échec */
|
||||
int push(stack, unsigned);
|
||||
|
||||
/* renvoie 1 si la pile est vide */
|
||||
int empty(stack);
|
||||
|
||||
/* retire un élément de la pile. Renvoie l'élément retiré, ou -1 en cas d'échec */
|
||||
long pop(stack);
|
||||
|
||||
/* détruit une pile en libérant les ressources associées */
|
||||
void destroy_stack(stack);
|
||||
|
||||
#endif /* STACK_H */
|
||||
63
prepa_CM3/ex3_A/stack_operations.c
Normal file
63
prepa_CM3/ex3_A/stack_operations.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <stdio.h>
|
||||
#include "stack.h"
|
||||
|
||||
/* Retire de la pile tous les éléments valant 0 */
|
||||
void removeZeros(stack s) {
|
||||
stack tempStack = create_stack();
|
||||
|
||||
/* Transférer les éléments non nuls vers une pile temporaire*/
|
||||
while (!empty(s)) {
|
||||
long value = pop(s);
|
||||
if (value != 0) {
|
||||
push(tempStack, value);
|
||||
}
|
||||
}
|
||||
|
||||
/* Transférer les éléments depuis la pile temporaire vers la pile d'origine*/
|
||||
while (!empty(tempStack)) {
|
||||
push(s, pop(tempStack));
|
||||
}
|
||||
|
||||
/* Libérer la mémoire de la pile temporaire*/
|
||||
destroy_stack(tempStack);
|
||||
}
|
||||
|
||||
int main() {
|
||||
stack myStack = create_stack();
|
||||
|
||||
/* Ajouter des éléments à la pile (exemple)*/
|
||||
push(myStack, 0);
|
||||
push(myStack, 5);
|
||||
push(myStack, 0);
|
||||
push(myStack, 8);
|
||||
push(myStack, 0);
|
||||
push(myStack, 3);
|
||||
|
||||
/* Afficher la pile avant la suppression des zéros*/
|
||||
printf("Pile avant suppression des zéros : ");
|
||||
while (!empty(myStack)) {
|
||||
printf("%ld ", pop(myStack));
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
/* Remettre des éléments à la pile (exemple)*/
|
||||
push(myStack, 0);
|
||||
push(myStack, 9);
|
||||
push(myStack, 0);
|
||||
push(myStack, 7);
|
||||
|
||||
/* Appliquer la fonction pour retirer les zéros*/
|
||||
removeZeros(myStack);
|
||||
|
||||
/* Afficher la pile après la suppression des zéros*/
|
||||
printf("Pile après suppression des zéros : ");
|
||||
while (!empty(myStack)) {
|
||||
printf("%ld ", pop(myStack));
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
/* Libérer la mémoire de la pile*/
|
||||
destroy_stack(myStack);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user