-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackUsingLinked.c
More file actions
129 lines (120 loc) · 2.39 KB
/
stackUsingLinked.c
File metadata and controls
129 lines (120 loc) · 2.39 KB
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
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <stdlib.h>
struct Node
{
struct Node *next;
int data;
};
void traversal(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}
int isFull(struct Node *top)
{
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
if (p == NULL)
{
return 1;
}
else
{
return 0;
}
}
int isEmpty(struct Node *top)
{
if (top == NULL)
{
return 1;
}
else
{
return 0;
}
}
struct Node *push(struct Node *top, int data)
{
if (isFull(top))
{
printf("You cannot push the %d value ", data);
}
else
{
struct Node *ptr = (struct Node *)malloc(sizeof(struct Node));
ptr->data = data;
ptr->next = top;
top = ptr;
return top;
}
};
// This method is use for returning an integer value.
// In this function you can pass pointer of pointer and address pass in the main function
// If you don't need to pass a pointer of pointer so you can define global structure instead of structure of main function.
// Jo pointer nu pointer na banavu hoy to main function ma j structure 6 tena badle global ma structure banavu.
int popsecond(struct Node **top)
{
if (isEmpty(*top))
{
printf("Stack is Underflow..!!\n");
}
else
{
struct Node *ptr = *top;
*top = (*top)->next;
int x = ptr->data;
free(ptr);
return x;
}
}
struct Node *pop(struct Node *top)
{
if (isEmpty(top))
{
printf("Stack is Underflow..!!\n");
}
else
{
struct Node *ptr = top;
top = top->next;
int x = ptr->data;
free(ptr);
return top;
}
}
int peek(struct Node * top, int pos)
{
struct Node *ptr = top;
for (int i = 0; (i < pos - 1 && ptr != NULL); i++)
{
ptr = ptr->next;
}
if (ptr != NULL)
{
return ptr->data;
}
else
{
return -1;
}
}
int main()
{
struct Node *top = NULL;
top = push(top, 78);
top = push(top, 178);
top = push(top, 28);
top = push(top, 288);
// int element = popsecond(&top);
// printf("%d popped from stack..!!\n", element);
// top = pop(top);
// traversal(top);
for (int i = 1; i <= 4; i++)
{
printf("Value at position %d is: %d\n", i, peek(top, i));
}
return 0;
}