-
Notifications
You must be signed in to change notification settings - Fork 0
/
31_other_in_stack.c
118 lines (101 loc) · 2.25 KB
/
31_other_in_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<stdio.h>
#include<stdlib.h>
//27-07-21
struct Node{
int data;
struct Node *next;
};
struct Node *top = NULL;
void linkedlistTraversal(struct Node *ptr){
while (ptr != NULL)
{
printf("Element = %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node *top){
if (top == NULL)
{
// printf("!!Empty Linked list warning!!");
return 1;
}
return 0;
}
int isFull(struct Node *top){
struct Node *n = (struct Node *)malloc(sizeof(struct Node));
if (n == NULL)
{
// printf("!!Heap memory Exhaustion warning!!");
return 1;
}
return 0;
}
struct Node *push(struct Node *top, int x){
if (isFull(top))
{
printf("!!Heap memory Exhaustion warning!!\n");
}
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p->data = x;
p->next = top;
top = p;
return top;
}
//https://www.codewithharry.com/videos/data-structures-and-algorithms-in-hindi-30
int pop(struct Node **tp){
if (isEmpty(*tp))
{
printf("!!Heap memory Exhaustion warning!!\n");
}
struct Node *p = *tp;
*tp = (*tp)->next;
int x = p->data;
free(p);
return x;
}
int peek(int position){
struct Node *ptr = top;
for (int i = 0; (i < position - 1 && ptr != NULL); i++)
{
ptr = ptr->next;
}
if (ptr != NULL)
{
return ptr->data;
// printf("%d\n", ptr->data);
}
return -1;
}
int stackTop(){
return top->data;
}
int stackBottom(){
struct Node *ptr = top;
while (ptr->next != NULL)
{
// printf("Element = %d\n", ptr->data);
ptr = ptr->next;
}
return ptr->data;
}
int main(){
top = push(top, 54);
top = push(top, 55);
top = push(top, 56);
top = push(top, 57);
linkedlistTraversal(top);
top = push(top, 69);
linkedlistTraversal(top);
int element = pop(&top);
printf("\n popedElement = %d\n", element);
linkedlistTraversal(top);
printf("------------------27-07\n");
printf("%d\n",peek(2));
// for (int i = 1; i <= 4; i++)
// {
// printf("Value at position %d is : %d\n", i, peek(i));
// }
printf("%d\n", stackTop());
printf("%d\n",stackBottom());
return 0;
}