TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Implement Stack Operation In C#
Vijayaragavan S
Aug 24
2016
Code
825
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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#