谁能给个C#写的循环队列代码?

jsn6314113 2010-08-04 10:41:34
RT
...全文
368 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovienlyk 2012-05-31
  • 打赏
  • 举报
回复
感谢大家分享 刚好学到
wuyq11 2010-08-04
  • 打赏
  • 举报
回复
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>)>)。
兔子-顾问 2010-08-04
  • 打赏
  • 举报
回复
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体现的。
Peter200694013 2010-08-04
  • 打赏
  • 举报
回复

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());
}
Peter200694013 2010-08-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 peter200694013 的回复:]
C# code

public class CircularQueue
{
private Int32 length;
private Int32 font;
private Int32 rear;

private Int32[] elements;
……
[/Quote]
sorry about the mistakes, I will update it. pls wait
poloyzhang 2010-08-04
  • 打赏
  • 举报
回复


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;

//入列
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;
}
}
}
}


}
Peter200694013 2010-08-04
  • 打赏
  • 举报
回复

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;
}
}


简单写了下,仅供参考
tyg111 2010-08-04
  • 打赏
  • 举报
回复
帮 顶

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧