C# 堆栈实现
点击次数:21 次 发布日期:2008-11-26 23:11:31 作者:源代码网
|
源代码网推荐using System; 源代码网推荐public class Stack 源代码网推荐{ 源代码网推荐private Node first = null; 源代码网推荐private int count = 0; 源代码网推荐/***********************************************Property Procedures do not accept any parameters. Note the 源代码网推荐diff in the function definition (no parenthesis)************************************************/ 源代码网推荐public bool Empty 源代码网推荐{/*******************************************Property 源代码网推荐GetProcedure********************************************/ 源代码网推荐get 源代码网推荐{ 源代码网推荐return (first == null); 源代码网推荐} 源代码网推荐} 源代码网推荐public int Count 源代码网推荐{/*******************************************Property Get 源代码网推荐Procedure********************************************/ 源代码网推荐get 源代码网推荐{ return count; } } 源代码网推荐public object Pop() 源代码网推荐{ 源代码网推荐if (first == null) 源代码网推荐{ 源代码网推荐throw new InvalidOperationException ("Cant pop from an empty stack"); 源代码网推荐} 源代码网推荐else 源代码网推荐{ 源代码网推荐object temp = first.Value; 源代码网推荐first = first.Next; 源代码网推荐count--; 源代码网推荐return temp; 源代码网推荐} 源代码网推荐} 源代码网推荐public void Push(object o) 源代码网推荐{ 源代码网推荐first = new Node(o, first); 源代码网推荐count++; 源代码网推荐} 源代码网推荐class Node 源代码网推荐{ 源代码网推荐public Node Next; 源代码网推荐public object Value; 源代码网推荐public Node(object value) : 源代码网推荐this(value, null) {} 源代码网推荐public Node(object value, Node next) 源代码网推荐{ 源代码网推荐 源代码网推荐Next = next; 源代码网推荐Value = value; 源代码网推荐} 源代码网推荐}} 源代码网推荐class StackTest{ 源代码网推荐static void Main() 源代码网推荐{ Stack s = new Stack(); 源代码网推荐if (s.Empty) 源代码网推荐Console.WriteLine("Stack is Empty"); 源代码网推荐else 源代码网推荐Console.WriteLine("Stack is not Empty"); 源代码网推荐for (int i = 0; i < 5; i++) 源代码网推荐s.Push(i); 源代码网推荐 源代码网推荐Console.WriteLine("Items in Stack {0}", s.Count); 源代码网推荐 源代码网推荐for (int i = 0; i < 5; i++) 源代码网推荐Console.WriteLine("Popped Item is {0} and the count is {1}", s.Pop(), s.Count); 源代码网推荐s = null; 源代码网推荐} 源代码网推荐}}//*********END OF CODE 源代码网推荐 源代码网推荐//ASPHouse http://asphouse.yeah.net/ 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐原作者:不详 源代码网推荐来 源:不详 源代码网推荐 源代码网推荐 源代码网推荐 源代码网供稿. |
