summaryrefslogtreecommitdiff
path: root/src/02/05/stack.c
blob: 18a344a919c203cea04b476178fdc160b7748fce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stack.h"
#include <stdlib.h>

Node *node_init(int data) {
  Node *node = malloc(sizeof(Node));
  node->next = NULL;
  node->data = data;
  return node;
}

Stack *stack_init(int data) {
  Stack *stack = malloc(sizeof(Stack));
  stack->head = node_init(data);
  return stack;
}

int stack_size(Stack *self) {
  if (!self || !self->head)
    return 0;

  int count;
  Node *current = self->head;
  while (current) {
    ++count;
    current = current->next;
  }

  return count;
}