栈的特别是后进先出(先进后出),包含出栈入栈两个基本操作:
Code
using System;
using System.Collections;
namespace dotNet
{
public class Program
{
static void Main()
{
Stack stack = new Stack();
stack.Pull();
stack.Push(new Item("一", null));
stack.Push(new Item("二", null));
stack.Push(new Item("三", null));
stack.Push(new Item("四", null));
stack.Pull();
stack.Pull();
stack.ViewMember();
stack.Push(new Item("五", null));
stack.Push(new Item("六", null));
stack.ViewMember();
Console.Read();
}
}
class Item
{
public string name;
public Item next;
public Item(string sName, Item objNext)
{
this.name = sName;
this.next = objNext;
}
}
class Stack
{
Item top = null;
public void Push(Item item)
{
if (top == null)
top = item;
else
{
item.next = top;
top = item;
}
}
public void Pull()
{
if (top != null)
{
if (top.next != null)
top = top.next;
else
top = null;
}
}
public void ViewMember()
{
Item currNode = top;
while (currNode != null)
{
Console.WriteLine(currNode.name);
currNode = currNode.next;
}
}
}
}
using System;
using System.Collections;
namespace dotNet
{
public class Program
{
static void Main()
{
Stack stack = new Stack();
stack.Pull();
stack.Push(new Item("一", null));
stack.Push(new Item("二", null));
stack.Push(new Item("三", null));
stack.Push(new Item("四", null));
stack.Pull();
stack.Pull();
stack.ViewMember();
stack.Push(new Item("五", null));
stack.Push(new Item("六", null));
stack.ViewMember();
Console.Read();
}
}
class Item
{
public string name;
public Item next;
public Item(string sName, Item objNext)
{
this.name = sName;
this.next = objNext;
}
}
class Stack
{
Item top = null;
public void Push(Item item)
{
if (top == null)
top = item;
else
{
item.next = top;
top = item;
}
}
public void Pull()
{
if (top != null)
{
if (top.next != null)
top = top.next;
else
top = null;
}
}
public void ViewMember()
{
Item currNode = top;
while (currNode != null)
{
Console.WriteLine(currNode.name);
currNode = currNode.next;
}
}
}
}