-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack2.c
66 lines (58 loc) · 1016 Bytes
/
stack2.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
59
60
61
62
63
64
65
66
//Making a stack through Linked List
#include <stdio.h>
#include<stdlib.h>
struct node {
int d;
struct node *next;
};
struct node *newNode(int d) {
struct node *new = malloc(sizeof(struct node));
new->d = d;
new->next = NULL;
return new;
}
void push(struct node** t, int d) {
struct node *new = newNode(d);
if(*t == NULL) {
//printf("Case 1 for push\n");
*t = new;
}
else {
//printf("Case 2 for push\n");
new->next = *t;
*t = new;
}
}
void printStack(struct node** t) {
struct node *temp = *t;
while(temp != NULL) {
printf("%d\n", temp->d);
temp = temp->next;
}
}
int pop(struct node **t) {
struct node *temp = *t;
struct node *temp2 = *t;
int d = 0;
if(temp == NULL) {
printf("Stack Undeflow");
return 0;
}
else {
d = temp->d;
temp = temp->next;
*t = temp;
free(temp2);
}
return d;
}
int main(void) {
struct node *top = NULL;
push(&top, 5);
push(&top, 99);
push(&top, 13);
pop(&top);
//printf("\n%d\n\n", pop(&top));
printStack(&top);
return 0;
}