-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstack.c
113 lines (113 loc) · 2.33 KB
/
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int x;
struct node *next;
}*top;
int size=0;
void push();
int pop();
int peek();
int count();
void displayStack();
int main()
{
int op,value;
printf("\nEnter the maximum size of the stack : ");
scanf("%d",&size);
do{
printf("\n...MENU...\n1)Push node\n2)Pop node\n3)Peek\nEnter 0 for EXIT\nEnter your option :");
scanf("%d",&op);
switch(op)
{
case 1:
push();
break;
case 2:
if(top==NULL)
printf("\nStack Underflow.");
else
{
value=pop();
printf("\n%d successfully popped from the stack.",value);
}
break;
case 3:
if(top==NULL)
printf("\nStack Underflow.");
else
{
value=peek();
printf("\nYou are peeking at : %d",value);
}
break;
case 0:
printf("\n...BYE BYE!...");
break;
default :
printf("\nINVALID option");
break;
}
displayStack();
}while(op!=0);
getch();
}
int count() //to count no. of elements in the stack
{
struct node *ptr=top;
int c;
if(top==NULL)
return 0;
while(ptr!=NULL)
{
c++;
ptr=ptr->next;
}
return c;
}
void push() //pushes one element to the beginning/top of the stack
{
if(count()>=size)
{
printf("\nStack Overflow");
return;
}
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
printf("\nEnter value to push : ");
scanf("%d",&ptr->x);
ptr->next=top;
top=ptr;
}
int pop() //returns the topmost element of the stack,and removes it from the stack
{
struct node *ptr;
int value;
ptr=top;
value=top->x;
top=top->next;
free(ptr);
return value;
}
int peek() //returns the topmost element of the stack,without modifying the stack
{
return top->x;
}
void displayStack() //to print the contents of the stack
{
struct node *ptr;
ptr=top;
if(top==NULL)
printf("\nStack is empty");
else
{
printf("\nElements of the Stack(From top to bottom):");
while(ptr!=NULL)
{
printf(" %d\t",ptr->x);
ptr=ptr->next;
}
}
}