-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingArray.csx
More file actions
69 lines (64 loc) · 1.73 KB
/
Copy pathStackUsingArray.csx
File metadata and controls
69 lines (64 loc) · 1.73 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
class Stack
{
const int MAX_SIZE = 100;
public int[] list { get; set; }
public int top { get; set; }
public Stack()
{
top = 0;
list = new int[MAX_SIZE];
}
public bool Push(int data)
{
if (top == MAX_SIZE)
return false;
list[top] = data;
top++;
return true;
}
public int Pop()
{
if (top == 0)
return -1;
top--;
return list[top];
}
}
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
int data;
Console.WriteLine("Welcome to stack using Array program");
int choice = -1;
while (choice != 0)
{
Console.WriteLine("0.Exit");
Console.WriteLine("1.Push");
Console.WriteLine("2.Pop");
Console.WriteLine("Please enter appropriate choice");
choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Please enter the element");
data = int.Parse(Console.ReadLine());
if (stack.Push(data))
Console.WriteLine("Element pushed successfully");
else
Console.WriteLine("Cant push element, Stack is full");
break;
case 2:
data = stack.Pop();
if (data == -1)
Console.WriteLine("Stack is already empty");
else
Console.WriteLine("Element: " + data);
break;
default:
break;
}
}
}
}