-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cs
69 lines (66 loc) · 1.33 KB
/
Stack.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
using System;
public class StackItem{
private double value;
private StackItem next;
public StackItem(double value)
{
this.value=value;
}
public void SetNext(StackItem nextItem)
{
next=nextItem;
}
public StackItem GetNext()
{
return next;
}
public double GetValue()
{
return value;
}
}
public class Stack{
private StackItem top;
public Stack()
{
top=null;
}
public bool IsEmpty()
{
return top==null;
}
public void Insert(double value)
{
var newItem= new StackItem(value);
if (top != null)
newItem.SetNext(top);
top = newItem;
}
public double? Pop()
{
if (top==null)
return null;
var value=top.GetValue();
if(top.GetNext()!=null)
top=top.GetNext();
return value;
}
public StackItem Top()
{
return top;
}
public void Print()
{
Console.Write("STACK = {");
if(!IsEmpty())
{
var temp=top;
while(temp!=null)
{
Console.Write(string.Format(" {0}",temp.GetValue()));
temp=temp.GetNext();
}
}
Console.WriteLine("}");
}
}