-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cs
More file actions
85 lines (77 loc) · 1.08 KB
/
Copy pathStack.cs
File metadata and controls
85 lines (77 loc) · 1.08 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
82
83
84
85
using System;
public class Program
{
public static void Main()
{
PStack<string> ps = new PStack<string>();
ps.Push("A");
ps.Push("B");
ps.Push("C");
ps.PrintPStack();
}
}
public class PStack<TItem>
{
private static readonly int MAX = 1000;
private int top = -1;
private TItem[] stack = new TItem[MAX];
public PStack()
{
top = -1;
}
public bool IsEmpty()
{
return (top < 0);
}
public bool Push(TItem data)
{
if (top >= MAX)
{
//overflow
return false;
}
else
{
stack[++top] = data;
return true;
}
}
public TItem Pop()
{
if (top < 0)
{
//no items in stack
return default(TItem);
}
else
{
//lifo
TItem data = stack[top--];
return data;
}
}
public void Peek()
{
if (top < 0)
{
Console.WriteLine("no elements in stack");
return;
}
else
{
Console.WriteLine("top most element in stack is " + stack[top]);
}
}
public void PrintPStack()
{
if (top < 0)
{
Console.WriteLine("stack is empty");
return;
}
for (int i = top; i >= 0; i--)
{
Console.WriteLine(stack[i]); //lifo
}
}
}