Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <assert.h>
- #include "stack.h"
- static bool invrep(stack s){
- return s != NULL;
- }
- struct _s_stack{
- stack_elem elem;
- stack next;
- };
- stack stack_empty(){
- stack empty_stack = NULL;
- return empty_stack;
- }
- stack stack_push(stack s, stack_elem e){
- stack new_node = malloc(sizeof(stack));
- new_node->elem = e;
- new_node->next = s;
- assert(invrep(new_node));
- return new_node;
- }
- stack stack_pop(stack s){
- assert(invrep(s));
- stack s_without_head = s->next;
- free(s);
- return s_without_head;
- }
- unsigned int stack_size(stack s){
- unsigned int s_size = 0;
- while (s != NULL){
- s = s->next;
- s_size++;
- }
- return s_size;
- }
- stack_elem stack_top(stack s){
- assert(invrep(s));
- return s->elem;
- }
- bool stack_is_empty(stack s){
- return (s == NULL);
- }
- stack_elem *stack_to_array(stack s){
- printf("a");
- if (stack_is_empty(s)){
- return NULL;
- }
- printf("b");
- assert(invrep(s));
- stack aux = s;
- unsigned int len = stack_size(s);
- stack_elem *a = calloc(len, sizeof(stack_elem));
- unsigned int i = len;
- for(unsigned int i = len - 1u; i <= 0u ; i--){
- a[i] = aux->elem;
- aux = aux->next;
- }
- assert(invrep(s));
- return a;
- }
- stack destroy(stack s){
- assert(invrep(s));
- stack d;
- while (s->next != NULL){
- d = s->next;
- free(s);
- }
- free(d);
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement