forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
55 lines (49 loc) · 981 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
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *link;
};
void push(struct node **top, int newData){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = newData;
if(*top == NULL){
*top = newNode;
newNode->link = NULL;
}
else{
newNode->link = *top;
*top = newNode;
}
}
void pop(struct node **top){
if(*top == NULL)
printf("\nstack underflow");
else if((*top)->link == NULL){
*top = NULL;
printf("\nstack is now empty");
}
else
*top = (*top)->link;
}
void print(struct node *top){
while(top != NULL){
printf("%d ", top->data);
top = top->link;
}
}
int main(){
struct node *top = NULL;
push(&top, 5);
push(&top, 4);
push(&top, 3);
print(top);
pop(&top);
pop(&top);
pop(&top);
pop(&top);
pop(&top);
printf("\n");
print(top);
return 0;
}