#region 棧的數(shù)據(jù)結構
/// summary>
/// 棧的數(shù)據(jù)結構
/// /summary>
public class SeqStackT>
{
public T[] data;
/// summary>
/// 棧頂指針
/// /summary>
public int top = -1;
public SeqStack(int lenth)
{
data = new T[lenth];
}
}
#endregion
#region 棧的初始化操作
/// summary>
/// 棧的初始化操作
/// /summary>
/// typeparam name="T">/typeparam>
/// returns>/returns>
public SeqStackT> SeqStackInitT>(int length)
{
SeqStackT> seqStack = new SeqStackT>(length);
seqStack.top = -1;
return seqStack;
}
#endregion
#region 入棧
/// summary>
/// 入棧
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// param name="data">/param>
public void SeqStackPushT>(SeqStackT> seqStack, T data)
{
if (SeqStackIsFull(seqStack))
throw new Exception("不好意思,棧溢出");
seqStack.data[++seqStack.top] = data;
}
#endregion
#region 出棧
/// summary>
/// 出棧
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// returns>/returns>
public T SeqStackPopT>(SeqStackT> seqStack)
{
if (SeqStackIsEmpty(seqStack))
throw new Exception("嗚嗚,棧已空");
seqStack.data[seqStack.top] = default(T);
return seqStack.data[--seqStack.top];
}
#endregion
#region 獲取棧頂
/// summary>
/// 獲取棧頂
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// returns>/returns>
public T SeqStackPeekT>(SeqStackT> seqStack)
{
if (SeqStackIsEmpty(seqStack))
throw new Exception("棧已空");
return seqStack.data[seqStack.top];
}
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SeqStack
{
class Program
{
static void Main(string[] args)
{
SeqStackClass stackManager = new SeqStackClass();
SeqStackStudent> seqStack = stackManager.SeqStackInitStudent>(10);
Console.WriteLine("******************** 壓入ID=1,ID=2,ID=3的元素 ***********************\n");
//壓入ID=1,ID=2,ID=3的元素
stackManager.SeqStackPush(seqStack, new Student() { ID = 1, Name = "一線碼農(nóng)", Age = 23 });
stackManager.SeqStackPush(seqStack, new Student() { ID = 2, Name = "huangxincheng520", Age = 23 });
stackManager.SeqStackPush(seqStack, new Student() { ID = 3, Name = "51cto", Age = 23 });
Console.WriteLine(".... 壓入成功,當前棧中元素有:" + stackManager.SeqStackLen(seqStack) + "個");
Console.WriteLine("\n****************** 查看棧頂元素 ********************");
var result = stackManager.SeqStackPeek(seqStack);
Console.WriteLine("棧頂元素為:ID=" + result.ID + ",Name=" + result.Name + ",Age=" + result.Age);
Console.WriteLine("\n******************** 彈出棧頂元素 ***********************");
stackManager.SeqStackPop(seqStack);
Console.WriteLine("\n****************** 查看棧中的元素 ********************");
for (int i = 0; i stackManager.SeqStackLen(seqStack); i++)
{
Console.WriteLine("棧頂元素為:ID=" + seqStack.data[i].ID + ",Name=" + seqStack.data[i].Name + ",Age=" + seqStack.data[i].Age);
}
Console.Read();
}
}
#region 學生數(shù)據(jù)實體
/// summary>
/// 學生數(shù)據(jù)實體
/// /summary>
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
#endregion
#region 棧的數(shù)據(jù)結構
/// summary>
/// 棧的數(shù)據(jù)結構
/// /summary>
public class SeqStackT>
{
public T[] data;
/// summary>
/// 棧頂指針
/// /summary>
public int top = -1;
public SeqStack(int lenth)
{
data = new T[lenth];
}
}
#endregion
public class SeqStackClass
{
#region 棧的初始化操作
/// summary>
/// 棧的初始化操作
/// /summary>
/// typeparam name="T">/typeparam>
/// returns>/returns>
public SeqStackT> SeqStackInitT>(int length)
{
SeqStackT> seqStack = new SeqStackT>(length);
seqStack.top = -1;
return seqStack;
}
#endregion
#region 判斷棧是否為空
/// summary>
/// 判斷棧是否為空
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// returns>/returns>
public bool SeqStackIsEmptyT>(SeqStackT> seqStack)
{
return seqStack.top == -1;
}
#endregion
#region 清空棧
/// summary>
/// 清空棧
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
public void SeqStackClearT>(SeqStackT> seqStack)
{
seqStack.top = -1;
}
#endregion
#region 棧是否已滿
/// summary>
/// 棧是否已滿
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
public bool SeqStackIsFullT>(SeqStackT> seqStack)
{
return seqStack.top == seqStack.data.Length;
}
#endregion
#region 入棧
/// summary>
/// 入棧
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// param name="data">/param>
public void SeqStackPushT>(SeqStackT> seqStack, T data)
{
if (SeqStackIsFull(seqStack))
throw new Exception("不好意思,棧溢出");
seqStack.data[++seqStack.top] = data;
}
#endregion
#region 出棧
/// summary>
/// 出棧
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// returns>/returns>
public T SeqStackPopT>(SeqStackT> seqStack)
{
if (SeqStackIsEmpty(seqStack))
throw new Exception("嗚嗚,棧已空");
seqStack.data[seqStack.top] = default(T);
return seqStack.data[--seqStack.top];
}
#endregion
#region 獲取棧頂
/// summary>
/// 獲取棧頂
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// returns>/returns>
public T SeqStackPeekT>(SeqStackT> seqStack)
{
if (SeqStackIsEmpty(seqStack))
throw new Exception("棧已空");
return seqStack.data[seqStack.top];
}
#endregion
#region 獲取棧中元素個數(shù)
/// summary>
/// 獲取棧中元素個數(shù)
/// /summary>
/// typeparam name="T">/typeparam>
/// param name="seqStack">/param>
/// returns>/returns>
public int SeqStackLenT>(SeqStackT> seqStack)
{
return seqStack.top + 1;
}
#endregion
}
}