Skip to content
Snippets Groups Projects
Commit 24bc2bdb authored by agnon.kurteshi's avatar agnon.kurteshi
Browse files

delete stack_stack

parent 135abc02
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include "stack.h"
typedef struct _stack_stack {
int size;
int top;
stack *data;
} stack_stack;
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "stack.h"
void stack_init(stack *s, int max_capacity)
{
s->size = max_capacity;
s->data = malloc(max_capacity * sizeof(int));
s->top = -1;
}
void print_stack(stack s)
{
if(stack_is_empty(s)){
printf("stack is empty\n");
}else {
for(int i = 0; i < s.top+1; i++){
printf("[%d] ", s.data[i]);
}
printf("\n");
}
}
void stack_push(stack *s, int value)
{
if(s->top == s->size-1)
{
printf("stack full\n");
return;
}
else
{
s->top++;
s->data[s->top] = value;
}
}
void stack_pop(stack *s, int *val) //échoue si vide
{
if(stack_is_empty(*s))
{
printf("empty stack\n");
return;
}
else
{
*val = s->data[s->top];
s->top--;
}
}
void stack_peek(stack s, int *val)//return the top of the stack without pooping
{ // error if stakc is empty or NULL
*val = s.data[s.top];
}
bool stack_is_empty(stack s)//check if stack is empty
{
return s.top < 0;
}
void stack_destroy(stack *s)
{
s->top = -1;
free(s->data);
}
void stack_to_stack(stack *stack_emet, stack *stack_dest)
{
if (stack_is_empty(*stack_emet)){
printf("stack empty");
return;
}
int valeur;
stack_pop(stack_emet, &valeur);
stack_push(stack_dest, valeur);
}
\ 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