using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 使用Object类实现后进先出队列
{
class Stack
{
private Object[] _items;
public Object[] Items
{
get { return this._items; }
set { this._items = value; }
}
//将对象压入
public void Push(Object obj)
{
//第一次压入时,进行初始化,长度为1
if (this._items == null)
{
this._items = new Object[1];
this._items[0] = obj;
}
else
{
int count = this._items.Length;
Object[] objTemp = this._items;
this._items = new Object[count + 1];
int i = 0;
foreach (Object o in objTemp)
{
this._items[i++] = o;
}
this._items[i] = obj;
}
}
//按后入先出取出
public Object Pop()
{
//为初始化或长度为0时,无法取出任何元素
if (this._items == null||this._items.Length == 0)
return null;
else
{
Object obj = this._items[this._items.Length - 1];
//删除最后一个元素
this.DeleteLastObj();
return obj;
}
}
private void DeleteLastObj()
{
Object[] objTemp = new Object[this._items.Length - 1];
for (int i = 0; i < this._items.Length - 1; i++)
{
objTemp[i] = this._items[i];
}
this._items = objTemp;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 使用Object类实现后进先出队列
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Stack stack = new Stack();
private Stack<string> stackGeneric= new Stack<string>();
private void button1_Click(object sender, EventArgs e)
{
stack.Push(this.textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Object[] objs = stack.Items;
foreach(Object o in objs)
{
Console.WriteLine(o.ToString());
}
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
Console.WriteLine(this.stack.Pop().ToString());
}
catch
{
Console.WriteLine("null");
}
}
private void button3_Click(object sender, EventArgs e)
{
this.stackGeneric.Push(this.textBox2.Text);
}
private void button4_Click(object sender, EventArgs e)
{
try
{
Console.WriteLine(this.stackGeneric.Pop());
}
catch (InvalidOperationException)
{
Console.WriteLine("null");
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有