php十天速成教程,算法系列15天速成 第十天 栈

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();

SeqStack seqStack = stackManager.SeqStackInit(10);

Console.WriteLine("********************  压入ID=1,ID=2,ID=3的元素  ***********************\n");

//压入ID=1,ID=2,ID=3的元素

stackManager.SeqStackPush(seqStack, new Student() { ID = 1, Name = "一线码农", 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 学生数据实体

///

/// 学生数据实体

///

public class Student

{

public int ID { get; set; }

public string Name { get; set; }

public int Age { get; set; }

}

#endregion

#region 栈的数据结构

///

/// 栈的数据结构

///

public class SeqStack

{

public T[] data;

///

/// 栈顶指针

///

public int top = -1;

public SeqStack(int lenth)

{

data = new T[lenth];

}

}

#endregion

public class SeqStackClass

{

#region 栈的初始化操作

///

/// 栈的初始化操作

///

///

///

public SeqStack SeqStackInit(int length)

{

SeqStack seqStack = new SeqStack(length);

seqStack.top = -1;

return seqStack;

}

#endregion

#region 判断栈是否为空

///

/// 判断栈是否为空

///

///

///

///

public bool SeqStackIsEmpty(SeqStack seqStack)

{

return seqStack.top == -1;

}

#endregion

#region 清空栈

///

/// 清空栈

///

///

///

public void SeqStackClear(SeqStack seqStack)

{

seqStack.top = -1;

}

#endregion

#region 栈是否已满

///

/// 栈是否已满

///

///

///

public bool SeqStackIsFull(SeqStack seqStack)

{

return seqStack.top == seqStack.data.Length;

}

#endregion

#region 入栈

///

/// 入栈

///

///

///

///

public void SeqStackPush(SeqStack seqStack, T data)

{

if (SeqStackIsFull(seqStack))

throw new Exception("不好意思,栈溢出");

seqStack.data[++seqStack.top] = data;

}

#endregion

#region 出栈

///

/// 出栈

///

///

///

///

public T SeqStackPop(SeqStack seqStack)

{

if (SeqStackIsEmpty(seqStack))

throw new Exception("呜呜,栈已空");

seqStack.data[seqStack.top] = default(T);

return seqStack.data[--seqStack.top];

}

#endregion

#region 获取栈顶

///

/// 获取栈顶

///

///

///

///

public T SeqStackPeek(SeqStack seqStack)

{

if (SeqStackIsEmpty(seqStack))

throw new Exception("栈已空");

return seqStack.data[seqStack.top];

}

#endregion

#region 获取栈中元素个数

///

/// 获取栈中元素个数

///

///

///

///

public int SeqStackLen(SeqStack seqStack)

{

return seqStack.top + 1;

}

#endregion

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值