一、非泛型实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Stack s = new Stack(3);
s.Push(3);
s.Push(4);
s.Push(5);
for (int i = 0; i < s.array.Length; i++)
{
Console.WriteLine(s.array[i]);
}
Console.WriteLine(s.Peek());
Console.WriteLine(s.Pop());
}
}
/// <summary>
/// 栈的顺序结构基本实现(非泛型)
/// </summary>
public class Stack
{
/// <summary>
/// 存放元素的数组
/// </summary>
public object[] array;
/// <summary>
/// 元素的下标
/// </summary>
public int index;
/// <summary>
/// 默认的构造函数
/// </summary>
public Stack()
{
this.array = new object[10];
this.index = 0;
}
/// <summary>
/// 构造函数重载
/// </summary>
/// <param name="capcity"></param>
public Stack(int count)
{
this.array = new object[count];
this.index = 0;
}
/// <summary>
/// 扩大容器
/// </summary>
private void Dilatation()
{
//将数组个数扩大为原来的两倍
object[] newarray = new object[this.index * 2];
for (int i = 0; i < this.array.Length; i++)
{
newarray[i] = this.array[i];
}
this.array = newarray;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="obj"></param>
public void Push(object obj)
{
//判断是否为满,满的话扩容,最后就是添加元素
if (index==array.Length)
{
Dilatation();
}
this.array[index] = obj;
this.index++;
}
/// <summary>
/// 出栈
/// </summary>
public object Pop()
{
if (index!=0)
{
index--;
return array[index];
}
else
{
return "";
}
}
/// <summary>
/// 查找元素是否存在
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Contains(object obj)
{
bool b = false;
for (int i = 0; i < this.array.Length; i++)
{
if (this.array[i].Equals(obj))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 取栈顶
/// </summary>
public object Peek()
{
if (index!=0)
{
return array[index - 1];
}
else
{
return "";
}
}
}
}
二、泛型实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Stack<int> s = new Stack<int>(3);
s.Push(3);
s.Push(4);
s.Push(5);
for (int i = 0; i < s.array.Length; i++)
{
Console.WriteLine(s.array[i]);
}
Console.WriteLine(s.Peek());
Console.WriteLine(s.Pop());
}
}
/// <summary>
/// 栈的顺序结构基本实现(泛型)
/// </summary>
public class Stack<T>
{
/// <summary>
/// 存放元素的数组
/// </summary>
public T[] array;
/// <summary>
/// 元素的下标
/// </summary>
public int index;
/// <summary>
/// 默认的构造函数
/// </summary>
public Stack()
{
this.array = new T[10];
this.index = 0;
}
/// <summary>
/// 构造函数重载
/// </summary>
/// <param name="capcity"></param>
public Stack(int count)
{
this.array = new T[count];
this.index = 0;
}
/// <summary>
/// 扩大容器
/// </summary>
private void Dilatation()
{
//将数组个数扩大为原来的两倍
T[] newarray = new T[this.index * 2];
for (int i = 0; i < this.array.Length; i++)
{
newarray[i] = this.array[i];
}
this.array = newarray;
}
/// <summary>
/// 入栈
/// </summary>
/// <param name="obj"></param>
public void Push(T obj)
{
//判断是否为满,满的话扩容,最后就是添加元素
if (index==array.Length)
{
Dilatation();
}
this.array[index] = obj;
this.index++;
}
/// <summary>
/// 出栈
/// </summary>
public T Pop()
{
if (index == 0)
{
Console.WriteLine("元素不存在");
}
index--;
return array[index];
}
/// <summary>
/// 查找元素是否存在
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public bool Contains(T obj)
{
bool b = false;
for (int i = 0; i < this.array.Length; i++)
{
if (this.array[i].Equals(obj))
{
b = true;
break;
}
}
return b;
}
/// <summary>
/// 取栈顶
/// </summary>
public T Peek()
{
if (index==0)
{
Console.WriteLine("元素不存在");
}
return array[index - 1];
}
}
}