-
Notifications
You must be signed in to change notification settings - Fork 62
/
Stack Linked list.c
58 lines (49 loc) · 891 Bytes
/
Stack Linked list.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* pointer ;
};
typedef struct node node;
void push(int data, node** stack) {
node* tmp;
tmp = malloc(sizeof(node));
tmp->data = data;
tmp->pointer = *stack;
(*stack) = tmp;
}
void pop(node** stack) {
if (*stack != NULL) {
printf("Element Popped: %d", (*stack)->data);
*stack = (*stack)->pointer;
}
else {
printf("Stack Empty");
}
}
void printall(node* stack) {
if (stack != NULL) {
printf("Stack elements are :\n");
while (stack->pointer != NULL) {
printf("%d\n", stack->data);
stack = stack->pointer;
}
printf("%d\n", stack->data);
}
else
{
printf("Empty stack\n");
}
}
int main() {
node* root = malloc(sizeof(node));
root->data = 3;
root->pointer = NULL;
push(4, &root);
push(7, &root);
push(12, &root);
printall(root);
pop(&root);
printall(root);
return 0;
}