c#实现队列和栈
2009-05-21 15:43
239 查看
//队列
using System;
namespace QueueExample
{
public class Queue<T>
{
T[] data;
int head;
int rear;
int count;
public Queue(int length)
{
data = new T[length];
head=rear=0;
count = 0;
}
public void EnQueue(T item)
{
if(!IsFull())
{
data[rear]=item;
rear= (rear+1)%data.Length;
count++;
}
else
{
throw new ApplicationException("????!");
}
}
public T DeQueue()
{
if(!IsEmpty())
{
T item = data[head];
head++;
count--;
return item;
}
else
{
throw new ApplicationException("????!");
}
}
public bool IsFull()
{
return count==data.Length;
}
public bool IsEmpty()
{
return count==0;
}
}
}
//////////
using System;
using System.Collections.Generic;
namespace QueueExample
{
class MainClass
{
public static void Main(string[] args)
{
Queue<string> q = new Queue<string>(4);
q.EnQueue("11");
q.EnQueue("22");
q.EnQueue("33");
q.EnQueue("44");
if(q.IsFull())
{
Console.WriteLine("已满");
}
else
{
Console.WriteLine("未满");
}
while(!q.IsEmpty())
{
Console.WriteLine(q.DeQueue());
}
Console.ReadKey();
}
}
}
//栈
using System;
using System.Collections.Generic;
namespace stacktest
{
class MainClass
{
public static void Main(string[] args)
{
Stack<int> stk=new Stack<int>(5);
//stk.Pop();
try{
stk.Push(1);
stk.Push(2);
stk.Push(21);
stk.Push(22);
Console.WriteLine(stk.Pop());
Console.WriteLine(stk.Pop());
Console.WriteLine(stk.Pop());
Console.WriteLine(stk.Pop());
}catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
public class Stack<T>{
T[] data;
int top;//战顶指针
//int bottom;//战地指针
int count;//元素个数
//int i=0;//元素位置
public Stack(int length){
data =new T[length];
}
//取站顶元素
public T Pop(){
if(isEmpty()==true){
throw new ApplicationException("栈为空!");
}
else{
T topTemp=data[top-1];
top--;
return topTemp;
// return data[top];
}
}
//压站
public void Push(T item){
if(isFull()==true){
throw new ApplicationException("栈已满!");
}
else{
data[top]=item;
count++;
top++;
}
}
//获取元素个数
public int getCount(){
return data.Length;
}
//判断是否为空
public bool isEmpty(){
return count==0;
}
//判断是否满
public bool isFull(){
return data.Length==count;
}
}
}
using System;
namespace QueueExample
{
public class Queue<T>
{
T[] data;
int head;
int rear;
int count;
public Queue(int length)
{
data = new T[length];
head=rear=0;
count = 0;
}
public void EnQueue(T item)
{
if(!IsFull())
{
data[rear]=item;
rear= (rear+1)%data.Length;
count++;
}
else
{
throw new ApplicationException("????!");
}
}
public T DeQueue()
{
if(!IsEmpty())
{
T item = data[head];
head++;
count--;
return item;
}
else
{
throw new ApplicationException("????!");
}
}
public bool IsFull()
{
return count==data.Length;
}
public bool IsEmpty()
{
return count==0;
}
}
}
//////////
using System;
using System.Collections.Generic;
namespace QueueExample
{
class MainClass
{
public static void Main(string[] args)
{
Queue<string> q = new Queue<string>(4);
q.EnQueue("11");
q.EnQueue("22");
q.EnQueue("33");
q.EnQueue("44");
if(q.IsFull())
{
Console.WriteLine("已满");
}
else
{
Console.WriteLine("未满");
}
while(!q.IsEmpty())
{
Console.WriteLine(q.DeQueue());
}
Console.ReadKey();
}
}
}
//栈
using System;
using System.Collections.Generic;
namespace stacktest
{
class MainClass
{
public static void Main(string[] args)
{
Stack<int> stk=new Stack<int>(5);
//stk.Pop();
try{
stk.Push(1);
stk.Push(2);
stk.Push(21);
stk.Push(22);
Console.WriteLine(stk.Pop());
Console.WriteLine(stk.Pop());
Console.WriteLine(stk.Pop());
Console.WriteLine(stk.Pop());
}catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}
public class Stack<T>{
T[] data;
int top;//战顶指针
//int bottom;//战地指针
int count;//元素个数
//int i=0;//元素位置
public Stack(int length){
data =new T[length];
}
//取站顶元素
public T Pop(){
if(isEmpty()==true){
throw new ApplicationException("栈为空!");
}
else{
T topTemp=data[top-1];
top--;
return topTemp;
// return data[top];
}
}
//压站
public void Push(T item){
if(isFull()==true){
throw new ApplicationException("栈已满!");
}
else{
data[top]=item;
count++;
top++;
}
}
//获取元素个数
public int getCount(){
return data.Length;
}
//判断是否为空
public bool isEmpty(){
return count==0;
}
//判断是否满
public bool isFull(){
return data.Length==count;
}
}
}
相关文章推荐
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现 收藏
- C#编写Windows服务程序 (服务端),客户端使用 消息队列 实现淘宝 订单全链路效果
- C# 【多线程+队列】【附源码】 实现右下角的消息提示框,支持同时显示多个提示框
- 基于无锁的C#并发队列实现(转载)
- 使用微软消息队列实现C#进程间通信(三)
- C#实现循环顺序队列(队列)
- 用 C# 实现的一个队列源代码
- 一个C#多线程的工作队列 ,c# 多线程排队队列实现的源码
- 用 C# 实现优先队列
- 使用C#实现顺序队列
- C#实现环形队列
- 使用微软消息队列实现C#进程间通信
- C#实现冒泡排序 堆栈 队列
- C# 数据结构基础-实现循环队列
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现(转载)
- c# 多线程排队队列实现的源码
- C#通过链表实现队列的方法
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- C#实现任务队列
- c# 多线程排队队列实现的源码