-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackUsingList.csx
More file actions
81 lines (76 loc) · 1.95 KB
/
Copy pathStackUsingList.csx
File metadata and controls
81 lines (76 loc) · 1.95 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
class Stack
{
class Node
{
public int Data { get; set; }
public Node Next { get; set; }
}
public Node Top { get; set; }
public Stack()
{
Top = null;
}
public bool Push(int data)
{
try
{
Node node = new Node() { Data = data, Next = null };
node.Next = Top;
Top = node;
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
}
}
public int Pop()
{
if (Top == null)
{
return -1;
}
int data = Top.Data;
Top = Top.Next;
return data;
}
}
class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
int data;
Console.WriteLine("Welcome to stack using Linked List 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 Empty");
else
Console.WriteLine("Element: " + data);
break;
default:
break;
}
}
}
}