-
Notifications
You must be signed in to change notification settings - Fork 0
/
29_stack_using_linkList.c
78 lines (67 loc) · 1.22 KB
/
29_stack_using_linkList.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
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node * next;
}node;
node* top = NULL;
void linkedListTraversal(node *ptr)
{
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
int isEmpty(node* top){
if (top==NULL){
return 1;
}
else{
return 0;
}
}
int isFull(node* top){
node* p = (node*)malloc(sizeof(node));
if(p==NULL){
return 1;
}
else{
return 0;
}
}
node* push(node* top, int x){
if(isFull(top)){
printf("Stack Overflow\n");
}
else{
node* n = (node*) malloc(sizeof(node));
n->data = x;
n->next = top;
top = n;
return top;
}
}
int pop(node* top){
if(isEmpty(top)){
printf("Stack Underflow\n");
}
else{
node* n = top;
top = (top)->next;
int x = n->data;
free(n);
return x;
}
}
int main(){
top = push(top, 78);
top = push(top, 7);
top = push(top, 8);
linkedListTraversal(top);
// int element = pop(top);
// printf("Popped element is %d\n", element);
// linkedListTraversal(top);
return 0;
}