队列的特别是先进先出,包含入队出队两个基本操作:
1 在入队过程中: 就是在队列的尾部添加数据,队列数据个数加一,尾指针后移。
2 在出队过程中:就是在队列的头部取的数据后,然后删除该数据,头指针后移
1 在入队过程中: 就是在队列的尾部添加数据,队列数据个数加一,尾指针后移。
2 在出队过程中:就是在队列的头部取的数据后,然后删除该数据,头指针后移
Code
using System;
using System.Collections;
namespace dotNet
{
public class Program
{
static void Main()
{
Queue queue = new Queue();
queue.DeQueue();
queue.EnQueue(new Item("一", null));
queue.EnQueue(new Item("二", null));
queue.EnQueue(new Item("三", null));
queue.EnQueue(new Item("四", null));
queue.DeQueue();
queue.DeQueue();
queue.ViewMember();
queue.EnQueue(new Item("五", null));
queue.EnQueue(new Item("六", null));
queue.ViewMember();
Console.Read();
}
}
class Item
{
public string name;
public Item next;
public Item(string sName, Item objNext)
{
this.name = sName;
this.next = objNext;
}
}
class Queue
{
Item head = null;
Item tail = null;
public void EnQueue(Item item)
{
if (head == null)
{
head = tail = item;
}
else
{
tail.next = item;
tail = item;
}
}
public void DeQueue()
{
if (head != null)
{
if (head.next != null)
head = head.next;
else
head = null;
}
}
public void ViewMember()
{
Item currNode =head;
while (currNode != null)
{
Console.WriteLine(currNode.name);
currNode = currNode.next;
}
}
}
}
using System;
using System.Collections;
namespace dotNet
{
public class Program
{
static void Main()
{
Queue queue = new Queue();
queue.DeQueue();
queue.EnQueue(new Item("一", null));
queue.EnQueue(new Item("二", null));
queue.EnQueue(new Item("三", null));
queue.EnQueue(new Item("四", null));
queue.DeQueue();
queue.DeQueue();
queue.ViewMember();
queue.EnQueue(new Item("五", null));
queue.EnQueue(new Item("六", null));
queue.ViewMember();
Console.Read();
}
}
class Item
{
public string name;
public Item next;
public Item(string sName, Item objNext)
{
this.name = sName;
this.next = objNext;
}
}
class Queue
{
Item head = null;
Item tail = null;
public void EnQueue(Item item)
{
if (head == null)
{
head = tail = item;
}
else
{
tail.next = item;
tail = item;
}
}
public void DeQueue()
{
if (head != null)
{
if (head.next != null)
head = head.next;
else
head = null;
}
}
public void ViewMember()
{
Item currNode =head;
while (currNode != null)
{
Console.WriteLine(currNode.name);
currNode = currNode.next;
}
}
}
}