-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
44 lines (38 loc) · 807 Bytes
/
stack.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Created by Freedom Coder on 05.10.2021.
//
#include <stdlib.h>
#include "stack.h"
void init(struct MStack *stack)
{
stack->top = NULL;
}
void push(struct MStack *stack, int item)
{
struct MStackElement *top = stack->top;
struct MStackElement *nptr = malloc(sizeof(struct MStackElement));
nptr->data = item;
nptr->next = top;
stack->top = nptr;
}
int pop(struct MStack *stack)
{
struct MStackElement *top = stack->top;
if (top != NULL) {
struct MStackElement *temp;
temp = top;
stack->top = top->next;
free(temp);
return top->data;
}
return -1;
}
int peek(struct MStack *stack)
{
if (stack->top != NULL) return stack->top->data;
return -1;
}
int empty(struct MStack *stack)
{
return stack->top == NULL;
}