using System using System Collections Generic using System Linq using

  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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public class CStack
{
private int[] M;
private int[] L;
private const int defaultCapacity = 10;
private int size;
private int head;
public CStack()
{
this.size = 0;
this.M = new int[defaultCapacity];
this.L = new int[defaultCapacity];
this.L[0] = -1;
this.head = 0;
}
public void Pop()
{
if (this.M[this.head] == this.M[this.size - 1])
{
this.size--;
int id = 0, max = this.M[0];
for (int i = 0; i < this.size; ++i)
{
if (max < this.M[i])
{
max = this.M[i];
id = i;
}
}
this.head = id;
return;
}
else
{
int id = 0, delta = 0;
for (int i = 0; i < this.size - 1; ++i)
{
if (this.M[i] - this.M[this.size - 1] >= 0)
{
id = i;
delta = this.M[i] - this.M[this.size - 1];
break;
}
}
for (int i = 0; i < this.size - 1; ++i)
{
if (this.M[i] >= this.M[this.size - 1] && this.M[i] - this.M[this.size - 1] < delta)
{
id = i;
delta = this.M[i] - this.M[this.size - 1];
}
}
this.L[id] = this.L[this.size - 1];
this.size--;
}
}
public void Push(int newElement)
{
if (this.size == 0)
{
this.M[this.size++] = newElement;
return;
}
if (this.M[this.head] < newElement)
{
this.L[this.size] = this.head;
this.M[this.size++] = newElement;
this.head = this.size - 1;
}
else
{
int id = 0, delta = 0;
for (int i = 0; i < this.size; ++i)
{
if (this.M[i] - newElement >= 0)
{
id = i;
delta = this.M[i] - newElement;
break;
}
}
for (int i = 0; i < this.size; ++i)
{
if (this.M[i] >= newElement && this.M[i] - newElement < delta)
{
id = i;
delta = this.M[i] - newElement;
}
}
this.L[this.size] = this.L[id];
this.L[id] = this.size;
this.M[this.size++] = newElement;
}
}
public void GetArray()
{
System.Console.WriteLine(this.M[this.head]);
int next = this.L[this.head];
while (this.L[next] != -1) {
System.Console.WriteLine(this.M[next]);
next = this.L[next];
}
System.Console.WriteLine(this.M[next]);
}
public void GetReverseArray()
{
int[] ret = new int[10];
int next = this.L[this.head];
ret[0] = this.M[this.head];
int id = 1;
while (this.L[next] != -1)
{
ret[id] = this.M[next];
next = this.L[next];
id++;
}
ret[id] = this.M[next];
for (int i = this.size - 1; i >= 0; --i)
{
System.Console.WriteLine(ret[i]);
}
}
}
static void Main(string[] args)
{
int[] input = { 9, 7, 11, 10, 5, 0 };
CStack M = new CStack();
for (int i = 0; i < 6; ++i)
{
M.Push(input[i]);
}
M.Pop();
M.Push(-5);
M.GetArray();
M.GetReverseArray();
}
}
}