Repository navigation
Expand file tree
/
Copy pathEntity.cs
More file actions
121 lines (110 loc) · 2.99 KB
/
Copy pathEntity.cs
File metadata and controls
121 lines (110 loc) · 2.99 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
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
113
114
115
116
117
118
119
120
121
namespace Algorithm.StackAndQueue
{
public class StackNode
{
public double Data;
public StackNode Previous;
public StackNode() { }
public StackNode(double data) { this.Data = data; }
}
/*
StackAndQueue.Stack s = new StackAndQueue.Stack();
s.push(1); s.push(2); s.push(3);
Console.WriteLine(s.pop() +" "+ s.pop() +" "+ s.pop()+" "+s.getSize());
*/
public class Stack
{
public StackNode head;
int size = 0;
public void push(double val)
{
StackNode sn = new StackNode(val);
sn.Previous = head;
head = sn;
size++;
}
public double pop()
{
if (head == null)
{
return -1;
}
double val = head.Data;
head = head.Previous;
size--;
return val;
}
public double peek()
{
if (head == null)
{
return -1;
}
return head.Data;
}
public int getSize()
{
return size;
}
public double popBottom()
{
StackNode sn = head;
while (sn.Previous != null)
{
sn = sn.Previous;
}
double val = sn.Data;
sn = null; size--;
return val;
}
public string toString()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
StackNode sn = head;
while (sn != null)
{
sb.Append(sn.Data).Append(" ");
sn = sn.Previous;
}
return sb.ToString();
}
}
/*
StackAndQueue.Queue q = new StackAndQueue.Queue();
q.enqueue(1); q.enqueue(2); q.enqueue(3);
Console.WriteLine(q.dequeue() + " " + q.peek() + " " + q.dequeue() + " " + q.dequeue());
*/
public class Queue
{
LinkedList.LinkedNode head = null;
LinkedList.LinkedNode tail = null;
public void enqueue(double val)
{
if (head == null)
{
head = new LinkedList.LinkedNode(val);
tail = head;
return;
}
LinkedList.LinkedNode ln = new LinkedList.LinkedNode(val);
ln.Next = head;
head = ln;
}
public double dequeue()
{
double val = tail.Data;
LinkedList.LinkedNode ln = head;
while (ln.Next != null && ln.Next.Next != null)
{
ln = ln.Next;
}
ln.Next = null;
tail = ln;
return val;
}
public double peek()
{
return tail.Data;
}
}
}