forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_pop.c
57 lines (55 loc) · 935 Bytes
/
push_pop.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
#include <stdio>
#include <stdlib>
struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
void push(int x)
{
struct node *newnode = malloc(sizeof(struct node));
newnode->data = x;
newnode->next = top;
top = newnode;
}
void pop()
{
struct node *temp = top;
if (top == NULL)
printf("Stack is Underflow\n");
printf("\nPop element is %d \n", top->data);
top = temp->next;
free(temp);
}
void peek()
{
if (top == NULL)
printf("Stack is Empty\n");
ṇṇ
printf("Top element is %d", top->data);
}
void show()
{
struct node *temp = top;
if (top == NULL)
top = temp;
printf("Element are:");
while (temp != NULL)
{
printf(" %d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
push(10);
push(20);
push(30);
push(40);
show();
pop();
show();
return 0;
}