栈(C#数据结构学习二)
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Text;
5
6
namespace soloDataStruct
7
{
8
public class Mystack<T>
9
{
10
private T[] stackarray;
11
private int maxSize;
12
private int top;
13
public Mystack(int s)
14
{
15
maxSize = s;
16
stackarray=new T[maxSize];
17
top = -1;
18
}
19
public void Push(T data)
20
{
21
if (top == maxSize - 1)
22
Console.WriteLine("out of space!");
23
stackarray[++top] = data;
24
}
25
public T Pop()
26
{
27
if (top == -1)
28
Console.WriteLine("out of space!");
29
return stackarray[top--];
30
31
}
32
public T GetTop()
33
{
34
if (top == -1)
35
Console.WriteLine("Stack is empty ");
36
return stackarray[top];
37
}
38
public bool ifemptystack()
39
{
40
return top == -1;
41
42
}
43
44
}
45
46
class test
47
{
48
static void Main(string[] args)
49
{
50
Mystack<string> mystack=new Mystack<string>(5);
51
mystack.Push("a");
52
mystack.Push("b");
53
mystack.Push("c");
54
mystack.Push("d");
55
Console.WriteLine("Stack poped data is {0}", mystack.Pop());
56
Console.WriteLine("Stack poped data is {0}", mystack.Pop());
57
Console.ReadLine();
58
59
}
60
}
61
}
62
63
using System;2
using System.Collections;3
using System.Collections.Generic;4
using System.Text;5

6
namespace soloDataStruct7
{8
public class Mystack<T>9
{10
private T[] stackarray;11
private int maxSize;12
private int top;13
public Mystack(int s)14
{15
maxSize = s;16
stackarray=new T[maxSize];17
top = -1;18
}19
public void Push(T data)20
{21
if (top == maxSize - 1)22
Console.WriteLine("out of space!");23
stackarray[++top] = data;24
}25
public T Pop()26
{27
if (top == -1)28
Console.WriteLine("out of space!");29
return stackarray[top--];30

31
}32
public T GetTop()33
{34
if (top == -1)35
Console.WriteLine("Stack is empty ");36
return stackarray[top];37
}38
public bool ifemptystack()39
{40
return top == -1;41

42
}43

44
}45

46
class test47
{48
static void Main(string[] args)49
{50
Mystack<string> mystack=new Mystack<string>(5);51
mystack.Push("a");52
mystack.Push("b");53
mystack.Push("c");54
mystack.Push("d");55
Console.WriteLine("Stack poped data is {0}", mystack.Pop());56
Console.WriteLine("Stack poped data is {0}", mystack.Pop());57
Console.ReadLine();58

59
}60
}61
}62

63



浙公网安备 33010602011771号