-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLLStack.cs
112 lines (109 loc) · 2.81 KB
/
SLLStack.cs
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
namespace DSA
{
public class SLLStack
{
private static int count = 0;
private SLLNode? head;
private SLLNode? top;
public SLLNode? Top { get => top; set => top = value; }
public SLLNode? Head { get => head; set => head = value; }
public static int Count { get => count; set => count = value; }
public SLLStack()
{
Head = null;
Top = null;
}
public void Push(int data)
{
if (Head == null)
{
Head = new SLLNode(data);
Top = Head;
++Count;
}
else
{
Top.Next = new SLLNode(data);
Top = Top.Next;
++Count;
}
}
public void PrintStack()
{
if (Head != null)
{
int tempCount = Count;
string indicator = "";
SLLNode? iterator;
for (int i = 0; i < Count; ++i)
{
iterator = Head;
for (int j = tempCount; j > 1; --j)
{
iterator = iterator.Next;
}
if (tempCount == Count)
{
indicator = "<--- Top";
}
Console.WriteLine($"|{iterator.Data,6}| {indicator}");
Console.WriteLine("--------");
indicator = "";
--tempCount;
}
Console.WriteLine();
}
else
{
Console.WriteLine("Stack Is Empty");
}
}
public void Pop()
{
if (Count > 0)
{
SLLNode temp = Head;
if (Count == 1)
{
Head = null;
Top = null;
--Count;
return;
}
for (int i = 0; i < Count - 2; ++i)
{
temp = temp.Next;
}
temp.Next = null;
--Count;
Top = temp;
}
else
{
Console.WriteLine("Stack Is Empty");
}
}
public int Size()
{
return Count;
}
public string Peek()
{
if (Count > 0)
return Top.Data.ToString();
else
return "[Stack Is Empty]";
}
public string IsEmpty()
{
if (Count == 0)
{
return "Yes";
}
else
{
return "No";
}
}
}
}