-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
111 lines (69 loc) · 1.14 KB
/
Stack.cpp
File metadata and controls
111 lines (69 loc) · 1.14 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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;
void push(int data);
void pop();
void peep();
int main()
{ int data;
char c;
while(1)
{
printf("What do you want to do? \n a.Push \n b.Pop \n c.Peep \n d.Exit \n");
scanf(" %c",&c);
switch(c)
{
case 'a': printf("Enter the value you want to be pushed onto the stack \n");
scanf("%d",&data);
push(data);
printf("Value pushed \n");
break;
case 'b': pop();
break;
case 'c': peep();
break;
case 'd':exit(0);
default: printf("Invalid option. Try again \n");
}
}
}
void push(int data)
{
node *temp= (struct node*)malloc(sizeof(struct node));
temp->data=data;
temp->next=top;
top=temp;
}
void pop()
{
if(top==NULL)
{
printf("The stack is empty \n");
return;
}
printf("The value popped is : %d \n", top->data);
node *temp=top;
top=top->next;
free(temp);
}
void peep()
{
if(top==NULL)
{
printf("The stack is empty\n");
return;
}
node *ptr;
ptr=top;
while(ptr!=NULL)
{
printf("%d \n",ptr->data);
ptr=ptr->next;
}
}