Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Implement Stack Operation In C#
WhatsApp
Vijayaragavan S
Aug 24
2016
874
0
0
using
System;
class
Program {
static
void
Main(
string
[] args) {
int
top = -1;
int
[] s =
new
int
[10];
Console.WriteLine(
"Enter The Size of The Stack"
);
int
MAX = Convert.ToInt16(Console.ReadLine());
while
(
true
) {
Console.WriteLine(
"1.Push"
);
Console.WriteLine(
"2.Pop"
);
Console.WriteLine(
"3.Display"
);
Console.WriteLine(
"4.Exit"
);
Console.WriteLine(
"Enter your choice :"
);
int
ch = Convert.ToInt16(Console.ReadLine());
switch
(ch) {
case
1:
if
(top > MAX - 1) Console.WriteLine(
"... Stack Overflow ..."
);
else
{
Console.WriteLine(
"Enter the item :"
);
int
n =
int
.Parse(Console.ReadLine());
s[++top] = n;
}
break
;
case
2:
if
(top == -1) Console.WriteLine(
" ... Stack Underflow ..."
);
else
{
Console.WriteLine(
"Popped item :"
+ s[top--]);
}
break
;
case
3:
if
(top == -1) Console.WriteLine(
"... Stack underflow ..."
);
else
{
Console.WriteLine(
"Elements in the stack"
);
for
(
int
i = top; i >= 0; i--) Console.WriteLine(s[i]);
}
break
;
case
4:
return
;
default
:
Console.WriteLine(
"Wrong Choice"
);
break
;
}
}
}
}
Implement Stack Operation
C#
Up Next
Implement Stack Operation In C#