-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.c
More file actions
49 lines (42 loc) · 996 Bytes
/
stack.c
File metadata and controls
49 lines (42 loc) · 996 Bytes
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
45
46
47
48
49
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include "constants.h"
#include "stack.h"
#include <string.h>
#include <stdlib.h>
void initStack(struct stack* s)
{
s->head = NULL;
}
void push(struct stack* s, char* data)
{
struct stackNode* newNode = (struct stackNode*)malloc(sizeof(struct stackNode));
strcpy(newNode->data, data);
newNode->previous = s->head;
s->head = newNode;
}
void pop(struct stack* s, char* dest)
{
strcpy(dest, s->head->data);
struct stackNode* newHead = s->head->previous;
free(s->head);
s->head = newHead;
}
void deleteStackNode(struct stackNode* node)
{
if(node->previous)
deleteStackNode(node->previous);
free(node);
}
void deleteStack(struct stack* s)
{
if(s->head)
deleteStackNode(s->head);
}
int isStackEmpty(struct stack* s)
{
return !s->head;
}