using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*定义List接口:*/
namespace StackRealize
{
public interface list
{
bool IsEmpty();
void Push(Object obj);
Object Pop();
bool Contain(Object obj);
void Delete(Object obj);
void PrintAll();
Object getHead();
Object getTail();
void Clear();
Object Shift();
void Unshift(Object obj);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*单向链表的实现*/
namespace StackRealize
{
public class SNode
{
public Object value;
public SNode next;
public SNode(object value, SNode next)
{
this.value = value;
this.next = next;
}
public SNode(Object value):this(value,null)
{
}
}
public class SList:list
{
private SNode head, tail;
public SList()
{
this.head = this.tail = null;
}
public bool IsEmpty()
{
return head == null;
}
public void Unshift(Object obj)
{
head = new SNode(obj, head);
if (tail == null)
tail = head;
}
public Object Shift()
{
if (head == null)
throw new NullReferenceException();
Object value = head.value;
if (head == tail)
head = tail = null;
else
head = head.next;
return value;
}
public void Push(Object obj)
{
if (!IsEmpty())
{
tail.next = new SNode(obj);
tail = tail.next;
}
else
head = tail = new SNode(obj);
}
public Object Pop()
{
if (head == null)
throw new NullReferenceException();
Object obj = tail.value;
if (head == tail)
head = tail = null;
else
{
//查找前驱节点
SNode temp = head;
for (; temp.next != null && !temp.next.Equals(tail); temp = temp.next);
tail = temp;
tail.next = null;
}
return obj;
}
public void PrintAll()
{
string result = "";
for (SNode temp = head; temp != null; temp = temp.next)
result += " " + temp.value.ToString();
Console.WriteLine(result);
}
public bool Contain(Object obj)
{
if (head == null)
return false;
else
{
for (SNode temp = head; temp != null; temp = temp.next)
{
if (temp.value.Equals(obj))
return true;
}
}
return false;
}
public void Delete(Object obj)
{
if (!IsEmpty())
{
if (head == tail && head.value.Equals(obj))
head = tail = null;
else if (head.value.Equals(obj))
head = head.next;
else
{
//temp_prev为删除值的前驱节点
for (SNode temp_prev = head, temp = head.next; temp != null; temp_prev = temp_prev.next, temp = temp.next)
{
if (temp.value.Equals(obj))
{
temp_prev.next = temp.next; //设置前驱节点的next为下个节点
if (temp == tail)
tail = temp_prev;
temp = null;
break;
}
}
}
}
}
public Object getHead()
{
return this.head.value;
}
public Object getTail()
{
return this.tail.value;
}
public void Clear()
{
do
{
Delete(head.value);
} while (!IsEmpty());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StackRealize
{
public class Stack:StackExercise
{
private SList list;
public Stack()
{
list = new SList();
}
public bool IsEmpty()
{
return list.IsEmpty();
}
public void Push(Object obj)
{
list.Push(obj);
}
public object Pop()
{
return list.Pop();
}
public object Top()
{
return list.getTail();
}
public void Clear()
{
list.Clear();
}
public void PrintAll()
{
list.PrintAll();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StackRealize
{
public interface StackExercise
{
Object Pop();
void Push(Object item);
Object Top();
bool IsEmpty();
void Clear();
void PrintAll();
}
}
NUNit单元测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using StackRealize;
/*测试栈操作*/
namespace TestStack
{
[TestFixture]
public class TestStack
{
[TestFixtureSetUp]
public void FinalizeClass()
{
Console.WriteLine("执行栈的检查开始");
}
[Test]
public void NewStack()
{
Stack s1 = new Stack();
Assert.IsTrue(s1.IsEmpty(), "stack is not empty!");
}
[Test, ExpectedException(typeof(NullReferenceException)), Category("NULLA")]
public void TopStack()
{
Stack temp = new Stack();
temp.Top();
Console.WriteLine("代码不应该执行到这里");
}
[Test, ExpectedException(typeof(NullReferenceException)), Category("NULLA")]
public void PopStack()
{
Stack temp = new Stack();
temp.Pop();
Console.WriteLine("代码不应该执行到这里");
}
[Test, Category("stack1"),ExpectedException(typeof(NullReferenceException))]
public void PushStack()
{
Stack temp = new Stack();
string str="I am liuli!";
temp.Push(str);
Assert.AreSame(str, temp.Pop(), "not same!");
Assert.IsTrue(temp.IsEmpty(), "stack is not empty!");
temp.Pop();
Console.WriteLine("代码不应该到这里");
}
[Test, Category("Order")]
public void StackOrder()
{
Stack temp = new Stack();
temp.Push("a");
temp.Push("b");
temp.Push("c");
Assert.AreSame("c", temp.Pop(), "not same!");
Assert.AreSame("b", temp.Pop(), "not same!");
Assert.AreSame("a", temp.Pop(), "not same!");
}
[Test, Category("TestNull")]
public void PopNull()
{
Stack temp = new Stack();
temp.Push(null);
Assert.AreSame(null, temp.Pop(), "not null");
}
[Test, Category("AfterExcception")]
public void AfterExcepiton()
{
Stack temp = new Stack();
try
{
temp.Pop();
}
catch (NullReferenceException)
{
}
finally
{
temp.Push("after exception!");
Console.WriteLine(temp.Pop());
}
}
[TestFixtureTearDown]
public void FinalizeMethod()
{
Console.WriteLine("执行栈的检查结束");
}
}
}