您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验 第三单元 队列操作

2015-12-18 12:22 447 查看
namespace Queue
{
internal class LinkedQueue
{
class Node
{
public  object data;
public  Node next;
}
//他们均指向节点,不存数据
Node front, rear;
int length = 0;
public LinkedQueue()
{
Initial();
}
public LinkedQueue(IEnumerable source)
{
Initial();
foreach (var ele in source)
{
Push(ele);
}
}
/// <summary>
/// 初始化队列
/// </summary>
public void Initial()
{
front = new Node();
rear = new Node();
front.data = null;
front.next = null;
rear.data = null;
rear.next = null;
length = 0;
}

/// <summary>
/// 置队空
/// </summary>
public void  Clear()
{
front.next = null;
length = 0;
}
/// <summary>
/// 判队空
/// </summary>
/// <returns></returns>
public Boolean Empty()
{
return front.next == null;
}
/// <summary>
/// 入队
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public Boolean Push(object obj)
{
Node node = new Node();
node.data = obj;
node.next = null;
if (Empty())
{
front.next = node;
rear.next = node;
}
else
{
Node last = rear.next;
last.next = node;
rear.next = node;
}
length++;
return true;
}
/// <summary>
/// 出队
/// </summary>
/// <returns></returns>
public object Pop()
{
Node first = front.next;
front.next = first.next;
if (object.ReferenceEquals(first,rear.next))
{
//最后一个元素了
rear.next = null;
}
length--;
return first.data;
}
/// <summary>
/// 取队头元素
/// </summary>
/// <returns></returns>
public object Front()
{
return front.data;
}
/// <summary>
/// 取队尾元素
/// </summary>
/// <returns></returns>
public object Rear()
{
return rear.data;
}

public List<object> ToList()
{
List<object> list = new List<object>();
Node node = front.next;
while (node != null)
{
list.Add(node.data);
node = node.next;
}
return list;
}

public int Length {
get { return length; }
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: