C#实现队列
using System;
namespace QueueWithCSharp
{
/// <summary>
/// LinkList 的摘要说明。
/// </summary>
///
public class Node
{
public int data;
public Node prior, next;
public Node()
{
prior = null;
next = null;
data = 0;
}
}
public class Queue
{
Node head, rear;
int length;
public int Length
{
get
{
return length;
}
}
public Queue()
{
//
// TODO: 在此处添加构造函数逻辑
//
head = rear = null;
length = 0;
}
public void EnQueue(int data) // 追加
{
if (rear == null)
{
rear = new Node();
head = rear;
rear.data = data;
length ++;
}
else
{
rear.next = new Node();
rear.next.data = data;
length ++;
rear = rear.next;
}
}
public int DeQueue()
{
if (length <= 0)
{
rear = head = null;
Console.WriteLine("队列中没有元素");
return 0;
}
int data = head.data;
head = head.next;
length --;
return data;
}
public void Print()
{
string str = "";
Node current = head;
while (current != null)
{
if (current == head)
{
str += current.data.ToString();
}
else
{
str += " <- " + current.data.ToString();
}
current = current.next;
}// end while current
Console.WriteLine(str);
}
}
}
namespace QueueWithCSharp
{
/// <summary>
/// LinkList 的摘要说明。
/// </summary>
///
public class Node
{
public int data;
public Node prior, next;
public Node()
{
prior = null;
next = null;
data = 0;
}
}
public class Queue
{
Node head, rear;
int length;
public int Length
{
get
{
return length;
}
}
public Queue()
{
//
// TODO: 在此处添加构造函数逻辑
//
head = rear = null;
length = 0;
}
public void EnQueue(int data) // 追加
{
if (rear == null)
{
rear = new Node();
head = rear;
rear.data = data;
length ++;
}
else
{
rear.next = new Node();
rear.next.data = data;
length ++;
rear = rear.next;
}
}
public int DeQueue()
{
if (length <= 0)
{
rear = head = null;
Console.WriteLine("队列中没有元素");
return 0;
}
int data = head.data;
head = head.next;
length --;
return data;
}
public void Print()
{
string str = "";
Node current = head;
while (current != null)
{
if (current == head)
{
str += current.data.ToString();
}
else
{
str += " <- " + current.data.ToString();
}
current = current.next;
}// end while current
Console.WriteLine(str);
}
}
}