Skip to content
Snippets Groups Projects
Commit 394c1de5 authored by narindra.rajohnso's avatar narindra.rajohnso
Browse files

Fin exercices

parent ff4a61ff
No related branches found
No related tags found
No related merge requests found
#include "stdio.h"
#include "stdlib.h"
#include <stdbool.h>
typedef struct element {
int data;
struct element *next;
} element;
int main(){
typedef element *stack;
stack stack_push(stack *s, int value) {
element *e = malloc(sizeof(*e));
e->data = value;
e->next = NULL;
e->next = *s;
*s = e;
return *s;
}
bool stack_is_empty(stack s) { return (s == NULL); }
stack stack_peek(stack s, int *value) {
// assert(!stack_is_empty(s));
if (stack_is_empty(s)) {
return NULL;
}
*value = s->data;
return s;
}
stack stack_pop(stack s, int *value) {
if (stack_peek(s, value) == NULL) {
return NULL;
}
element *tmp = s;
s = s->next;
free(tmp);
return s;
}
stack stack_destroy(stack s) {
int value;
while (!stack_is_empty(s)) {
s = stack_pop(s, &value);
}
return s;
}
int peek(stack *s, int position) {
stack test;
element *it = *s;
int count = 0;
if (position == 0) {
int value;
test = stack_peek(*s, &value);
return value;
}
while (count != position) {
it = it->next;
count++;
}
return it->data;
}
void insert(stack *s, int position, int value) {
element *it = *s;
element *tmp = it;
int count = 0;
if (position == 0) {
*s = stack_push(s, value);
return;
}
while (count != position) {
tmp = it;
it = it->next;
count++;
}
element *e = malloc(sizeof(*e));
e->data = value;
tmp->next = e;
e->next = it;
}
void printList(stack head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
void recurs(stack *liste, stack *listePrec, int nbLigne, int *i) {
if (*i < nbLigne) {
*liste = stack_push(liste, peek(liste, *i));
(*i)++;
}
}
int main() {
int numLigne;
scanf("%d", &numLigne);
stack listActuel = NULL;
stack listPrecedent;
listActuel = stack_push(&listActuel, 1);
int i=0;
recurs(&listActuel, &listPrecedent, numLigne, &i);
printList(listActuel);
stack_destroy(listActuel);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment