public class CircularQueue { private Int32 length; private Int32 font; private Int32 rear; private Int32[] elements; public CircularQueue() { elements = new Int32[16]; length = 0; font = 0; rear = 0; } public Int32 Lenght { get { return length; } } public Boolean EnQueue(Int32 i) { if (length == 16) return false; if (length == 0) { elements[font] = i; length++; } else { rear++; length++; elements[rear] = i; } return true; } public Int32 DeQueue() { length--; Int32 i = elements[rear]; rear--; return i; } public Boolean IsEmpty() { if (length == 0) return true; else return false; } public Boolean IsFull() { if (length == 16) return true; else return false; } } 简单写了下,仅供参考
using System; using System.Collections.Generic; using System.Linq; using System.Text; /* * 队列是这样一种数据结构,数据项的插入在一端(队列尾),而数据项的取得或删除则在另一端(队列头)。 * 因为第一个插入的数据项也是第一个取得或删除的数据项,开发者普遍地将队列称为FIFO数据结构。 开发者经常使用到两种队列:线性队列和循环队列。在两种队列中,数据项都是在队列尾插入, * 并从队列头删除或获取,即先进先出 * 下面实现一个循环队列 */ namespace 队列 { class Student { string name; public string Name { get { return name; } set { name = value; } } public Student(string name) { this.name = name; }
public Student Next; //指示下一个队列 } class Program { static void Main(string[] args) { //创建一个空队列 Student Node = null;
//出列 static void Delete(Student Node) { if (Node != null) { //如果队列中仅剩一个结点 if (Node.Next == Node) { Node = null; } else { Student Index = Node.Next; Node.Next = Node.Next.Next; Index = null; } } } }
}
sorry about the mistakes, I will update it. pls wait
public class CircularQueue { private Int32 count ; private Int32 length; private Int32 font; private Int32 rear; private Int32[] elements; public CircularQueue() { elements = new Int32[16]; length = 0; font = 0; rear = 0; count = 16; } public CircularQueue(Int32 c) { elements = new Int32[c]; length = 0; font = 0; rear = 0; count = c; } public Int32 Lenght { get { return length; } } public Boolean EnQueue(Int32 i) { if (length == count) return false; if (length == 0) { elements[font] = i; length++; } else { rear++; length++; if (rear == count) rear = 0; elements[rear] = i; } return true; } public Int32 DeQueue() { length--; Int32 i = elements[rear]; rear--; if (rear == -1) rear = count - 1; return i; } public Boolean IsEmpty() { if (length == 0) return true; else return false; } public Boolean IsFull() { if (length == count) return true; else return false; } } static void Main(string[] args) { CircularQueue cq = new CircularQueue(); for (Int32 i = 0; i < 16; i++) { if(!cq.IsFull()) cq.EnQueue(i); } cq.DeQueue(); if (!cq.IsFull()) cq.EnQueue(16); cq.DeQueue(); if (!cq.IsFull()) cq.EnQueue(17); while(!cq.IsEmpty()) Console.WriteLine(cq.DeQueue()); }
public class MyList<T> { T[] arr = new T[0]; public MyList(int size){arr=new T[size];} int last = 0; public void Add(T item) { last = (last + 1) % arr.Length; arr[last] = item; } }其他方法你自己加吧。循环就是这个Add体现的。
class List<T>{ public List(){this.next=new Node();} class Node{public T Tp{get;set;} public Node Next{get;set;}} Node next; public void Add(T t){ Node n=new Node(){Tp=t}; n.Next=this.next; this.next=n; } }System.Collections.Generic.Queue<(Of <(T>)>)。
public class CircularQueue
{
private Int32 length;
private Int32 font;
private Int32 rear; private Int32[] elements;
public CircularQueue()
{
elements = new Int32[16];
length = 0;
font = 0;
rear = 0;
} public Int32 Lenght
{
get
{
return length;
}
} public Boolean EnQueue(Int32 i)
{
if (length == 16)
return false; if (length == 0)
{
elements[font] = i;
length++;
}
else
{
rear++;
length++;
elements[rear] = i;
} return true;
} public Int32 DeQueue()
{
length--;
Int32 i = elements[rear];
rear--;
return i;
} public Boolean IsEmpty()
{
if (length == 0)
return true;
else
return false;
} public Boolean IsFull()
{
if (length == 16)
return true;
else
return false;
}
}
简单写了下,仅供参考
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
* 队列是这样一种数据结构,数据项的插入在一端(队列尾),而数据项的取得或删除则在另一端(队列头)。
* 因为第一个插入的数据项也是第一个取得或删除的数据项,开发者普遍地将队列称为FIFO数据结构。
开发者经常使用到两种队列:线性队列和循环队列。在两种队列中,数据项都是在队列尾插入,
* 并从队列头删除或获取,即先进先出
* 下面实现一个循环队列
*/
namespace 队列
{
class Student
{
string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public Student(string name)
{
this.name = name;
}
public Student Next; //指示下一个队列
}
class Program
{
static void Main(string[] args)
{
//创建一个空队列
Student Node = null;
//入列
Student student1 = new Student("小李");
Node = student1;
Node.Next = Node;
//入列
Student student2 = new Student("小丽");
student2.Next = Node.Next;
Node.Next = student2;
Node = student2;
//入列
Student student3 = new Student("小马");
student3.Next = Node.Next;
Node.Next = student3;
Node = student3;
//入列
Student student4 = new Student("小龙");
student4.Next = Node.Next;
Node.Next = student4;
Node = student4;
//输出队列的数据验证队列是否成功
Write(Node);
//出列
Delete(Node);
Delete(Node);
Console.WriteLine();
Write(Node);
}
//输出队列
static void Write(Student Node)
{
if (Node != null)
{
Student Index = Node.Next;
do
{
Console.WriteLine(Index.Name);
Index = Index.Next;
} while (Index != Node.Next);
}
else
{
Console.WriteLine("队列中已没有结点");
}
}
//出列
static void Delete(Student Node)
{
if (Node != null)
{
//如果队列中仅剩一个结点
if (Node.Next == Node)
{
Node = null;
}
else
{
Student Index = Node.Next;
Node.Next = Node.Next.Next;
Index = null;
}
}
}
}
}
public class CircularQueue
{
private Int32 count ;
private Int32 length;
private Int32 font;
private Int32 rear; private Int32[] elements;
public CircularQueue()
{
elements = new Int32[16];
length = 0;
font = 0;
rear = 0;
count = 16;
} public CircularQueue(Int32 c)
{
elements = new Int32[c];
length = 0;
font = 0;
rear = 0;
count = c;
} public Int32 Lenght
{
get
{
return length;
}
} public Boolean EnQueue(Int32 i)
{
if (length == count)
return false; if (length == 0)
{
elements[font] = i;
length++;
}
else
{
rear++;
length++;
if (rear == count)
rear = 0; elements[rear] = i;
} return true;
} public Int32 DeQueue()
{
length--;
Int32 i = elements[rear];
rear--;
if (rear == -1)
rear = count - 1; return i;
} public Boolean IsEmpty()
{
if (length == 0)
return true;
else
return false;
} public Boolean IsFull()
{
if (length == count)
return true;
else
return false;
}
}
static void Main(string[] args)
{ CircularQueue cq = new CircularQueue(); for (Int32 i = 0; i < 16; i++)
{
if(!cq.IsFull())
cq.EnQueue(i);
} cq.DeQueue(); if (!cq.IsFull())
cq.EnQueue(16); cq.DeQueue(); if (!cq.IsFull())
cq.EnQueue(17); while(!cq.IsEmpty())
Console.WriteLine(cq.DeQueue());
}
{
T[] arr = new T[0];
public MyList(int size){arr=new T[size];}
int last = 0;
public void Add(T item)
{
last = (last + 1) % arr.Length;
arr[last] = item;
}
}其他方法你自己加吧。循环就是这个Add体现的。
public List(){this.next=new Node();}
class Node{public T Tp{get;set;} public Node Next{get;set;}}
Node next;
public void Add(T t){
Node n=new Node(){Tp=t};
n.Next=this.next;
this.next=n;
}
}System.Collections.Generic.Queue<(Of <(T>)>)。