-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintStack-ll.c
More file actions
60 lines (51 loc) · 1.1 KB
/
intStack-ll.c
File metadata and controls
60 lines (51 loc) · 1.1 KB
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
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} node;
typedef struct Stack {
int size; //number of nodes
node *firstNode; //first node
} stack;
void initialise(stack *head) {
head->size=0;
head->firstNode=NULL;
}
void push(stack *head, int value) {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->data = value;
temp->next = head->firstNode;
head->firstNode = temp;
head->size++;
}
void pop(stack *head) {
if (head->size == 0) {
printf("Warning: Trying to pop from an empty stack!\n");
return;
}
node *temp;
temp = head->firstNode;
head->firstNode = head->firstNode->next;
free(temp);
head->size--;
}
int top(stack *head) {
if (head->size == 0) {
printf("Warning: Stack empty!\n");
return INT32_MIN;
}
return head->firstNode->data;
}
int isEmpty(stack *head) {
return head->size ? 0 : 1;
}
int size(stack *head) {
return head->size;
}
void freeStack(stack *head) {
for(int i = head->size; i > 0; i--) {
pop(head);
}
}