-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathStack_LL_Operations.c
More file actions
123 lines (108 loc) · 2.07 KB
/
Stack_LL_Operations.c
File metadata and controls
123 lines (108 loc) · 2.07 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
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *top=NULL; // DACLARING TOP AS GLOBAL VARIABLE SO AS TO CHANGE IT IN THE MAIN FUNC
void linked_list_traversal(struct Node *ptr)
{
while (ptr != NULL)
{
printf("Element : %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node*top)
{
if (top==NULL)
{
return 1;
}
else
{
return 0;
}
}
int isFull(struct Node*top)
{
struct Node *p=(struct Node*)malloc(sizeof(struct Node));
if (p==NULL)
{
return 1;
}
else
{
return 0;
}
}
struct Node* push(struct Node*top,int x)
{
if (isFull(top))
{
printf("Stack Overflow\n");
}
else
{
struct Node *n=(struct Node*)malloc(sizeof(struct Node));
n->data=x;
n->next=top;
top=n;
return top;
}
}
int pop(struct Node*tp)
{
if (isEmpty(tp))
{
printf("Stack Underflow\n");
}
else
{
struct Node*n=tp;
top=tp->next;
int x=n->data;
free(n);
return x;
}
}
int peek(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()
{
int y,z;
printf("Enter the element you want to push\n");
scanf("%d",&y);
printf("Enter the position you want to get the element\n");
scanf("%d",&z);
top=push(top,y);
top=push(top,78);
top=push(top,18);
top=push(top,28);
top=push(top,7);
int ele=pop(top);
printf("Popped element is %d\n",ele);
linked_list_traversal(top);
int seek_ele=peek(z);
printf("Number at position %d is %d\n",z,seek_ele);
for (int i = 1; i <=4; i++)
{
printf("Value at position %d is %d\n",i,peek(i));
}
return 0;
}