Csharp/C Sharp/Collections Data Structure/Queue
Содержание
Add exception handling to the queue classes
/*
C# A Beginner"s Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/*
Project 10-1
Add exception handling to the queue classes.
*/
using System;
// A character queue interface.
public interface ICharQ {
// Put a characer into the queue.
void put(char ch);
// Get a character from the queue.
char get();
}
// An exception for queue-full errors.
class QueueFullException : ApplicationException {
public QueueFullException() : base() { }
public QueueFullException(string str) : base(str) { }
public override string ToString() {
return "\n" + Message;
}
}
// An exception for queue-empty errors.
class QueueEmptyException : ApplicationException {
public QueueEmptyException() : base() { }
public QueueEmptyException(string str) : base(str) { }
public override string ToString() {
return "\n" + Message;
}
}
// A fixed-size queue class for characters that uses exceptions.
class FixedQueue : ICharQ {
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public FixedQueue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void put(char ch) {
if(putloc==q.Length-1)
throw new QueueFullException("Max length is " +
(q.Length-1));
putloc++;
q[putloc] = ch;
}
// Get a character from the queue.
public char get() {
if(getloc == putloc)
throw new QueueEmptyException();
getloc++;
return q[getloc];
}
}
// Demonstrate the queue exceptions.
public class QExcDemo {
public static void Main() {
FixedQueue q = new FixedQueue(10);
char ch;
int i;
try {
// overrun the queue
for(i=0; i < 11; i++) {
Console.Write("Attempting to store : " +
(char) ("A" + i));
q.put((char) ("A" + i));
Console.WriteLine(" -- OK");
}
Console.WriteLine();
}
catch (QueueFullException exc) {
Console.WriteLine(exc);
}
Console.WriteLine();
try {
// over-empty the queue
for(i=0; i < 11; i++) {
Console.Write("Getting next char: ");
ch = q.get();
Console.WriteLine(ch);
}
}
catch (QueueEmptyException exc) {
Console.WriteLine(exc);
}
}
}
A queue class for characters
/*
C# A Beginner"s Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
// A queue class for characters.
using System;
class Queue {
// these are now private
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
// Construct an empty Queue given its size.
public Queue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Construct a Queue from a Queue.
public Queue(Queue ob) {
putloc = ob.putloc;
getloc = ob.getloc;
q = new char[ob.q.Length];
// copy elements
for(int i=getloc+1; i <= putloc; i++)
q[i] = ob.q[i];
}
// Construct a Queue with initial values.
public Queue(char[] a) {
putloc = 0;
getloc = 0;
q = new char[a.Length+1];
for(int i = 0; i < a.Length; i++) put(a[i]);
}
// Put a character into the queue.
public void put(char ch) {
if(putloc==q.Length-1) {
Console.WriteLine(" -- Queue is full.");
return;
}
putloc++;
q[putloc] = ch;
}
// Get a character from the queue.
public char get() {
if(getloc == putloc) {
Console.WriteLine(" -- Queue is empty.");
return (char) 0;
}
getloc++;
return q[getloc];
}
}
// Demonstrate the Queue class.
public class QDemo2 {
public static void Main() {
// construct 10-element empty queue
Queue q1 = new Queue(10);
char[] name = {"T", "o", "m"};
// construct queue from array
Queue q2 = new Queue(name);
char ch;
int i;
// put some characters into q1
for(i=0; i < 10; i++)
q1.put((char) ("A" + i));
// construct queue from another queue
Queue q3 = new Queue(q1);
// Show the queues.
Console.Write("Contents of q1: ");
for(i=0; i < 10; i++) {
ch = q1.get();
Console.Write(ch);
}
Console.WriteLine("\n");
Console.Write("Contents of q2: ");
for(i=0; i < 3; i++) {
ch = q2.get();
Console.Write(ch);
}
Console.WriteLine("\n");
Console.Write("Contents of q3: ");
for(i=0; i < 10; i++) {
ch = q3.get();
Console.Write(ch);
}
}
}
Demonstrate the Queue class
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the Queue class.
using System;
using System.Collections;
public class QueueDemo {
static void showEnq(Queue q, int a) {
q.Enqueue(a);
Console.WriteLine("Enqueue(" + a + ")");
Console.Write("queue: ");
foreach(int i in q)
Console.Write(i + " ");
Console.WriteLine();
}
static void showDeq(Queue q) {
Console.Write("Dequeue -> ");
int a = (int) q.Dequeue();
Console.WriteLine(a);
Console.Write("queue: ");
foreach(int i in q)
Console.Write(i + " ");
Console.WriteLine();
}
public static void Main() {
Queue q = new Queue();
foreach(int i in q)
Console.Write(i + " ");
Console.WriteLine();
showEnq(q, 22);
showEnq(q, 65);
showEnq(q, 91);
showDeq(q);
showDeq(q);
showDeq(q);
try {
showDeq(q);
} catch (InvalidOperationException) {
Console.WriteLine("Queue empty.");
}
}
}
illustrates the use of a Queue
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example11_10.cs illustrates the use of a Queue
*/
using System;
using System.Collections;
public class Example11_10
{
public static void Main()
{
// create a Queue object
Queue myQueue = new Queue();
// add elements to myQueue using the Enqueue() method
myQueue.Enqueue("This");
myQueue.Enqueue("is");
myQueue.Enqueue("a");
myQueue.Enqueue("test");
// display the elements in myQueue
foreach (string myString in myQueue)
{
Console.WriteLine("myString = " + myString);
}
// get the number of elements in myQueue using the
// Count property
int numElements = myQueue.Count;
for (int count = 0; count < numElements; count++)
{
// examine an element in myQueue using Peek()
Console.WriteLine("myQueue.Peek() = " +
myQueue.Peek());
// remove an element from myQueue using Dequeue()
Console.WriteLine("myQueue.Dequeue() = " +
myQueue.Dequeue());
}
}
}
Implements the queue data type using an array
using System;
public class Queue {
private int[] data;
private int size;
private int front = -1;
private int back = 0;
private int count = 0;
public Queue() {
size = 10;
data = new int[size];
}
public Queue(int size) {
this.size = size;
data = new int[size];
}
public bool IsEmpty() {
return count == 0;
}
public bool IsFull() {
return count == size;
}
public void Add(int i){
if (IsFull())
throw new ApplicationException("Queue full");
else {
count++;
data[back++ % size] = i;
}
}
public int Remove(){
if (IsEmpty())
throw new ApplicationException("Queue empty");
else {
count--;
return data[++front % size];
}
}
public int Head(){
if (IsEmpty()){
throw new ApplicationException("Queue empty");
}
else
return data[(front+1) % size];
}
public static void Main() {
try {
Queue q1 = new Queue();
q1.Add(4);
q1.Add(5);
Console.WriteLine("The front is now {0}", q1.Head());
q1.Add(6);
Console.WriteLine("Removing from q1 returns {0}", q1.Remove());
Console.WriteLine("Queue 1 has size {0}", q1.size);
Console.WriteLine("Queue 1 empty? {0}", q1.IsEmpty());
q1.Remove();
Console.WriteLine("Throws exception before we get here");
}catch(Exception e) {
Console.WriteLine(e);
}
}
}
Put elements into a queue
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
Queue alphabet = new Queue();
alphabet.Enqueue("A");
alphabet.Enqueue("B");
alphabet.Enqueue("C");
Console.Write("First Iteration: ");
foreach (string item in alphabet) {
Console.Write(item);
}
Console.WriteLine("\nItem pulled from collection: " +
alphabet.Dequeue().ToString());
Console.Write("Second iteration: ");
foreach (string item in alphabet) {
Console.Write(item);
}
}
}
Put user-defined objects to Queue collection
using System;
using System.Collections;
public class Starter {
public static void Main() {
Queue waiting = new Queue();
waiting.Enqueue(new Customer("B"));
waiting.Enqueue(new Customer("T"));
waiting.Enqueue(new Customer("K"));
waiting.Enqueue(new Customer("S"));
while (waiting.Count != 0) {
Customer cust =
(Customer)waiting.Dequeue();
Console.WriteLine(cust.Name);
}
}
public class Customer {
public Customer(string cName) {
propName = cName;
}
private string propName;
public string Name {
get {
return propName;
}
}
}
}
Queue test
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
using System.Collections;
namespace QueueDemo
{
public class TesterQueueDemo
{
public void Run()
{
Queue intQueue = new Queue();
// populate the array
for (int i = 0;i<5;i++)
{
intQueue.Enqueue(i*5);
}
// Display the Queue.
Console.Write( "intQueue values:\t" );
DisplayValues( intQueue );
// Remove an element from the Queue.
Console.WriteLine(
"\n(Dequeue)\t{0}", intQueue.Dequeue() );
// Display the Queue.
Console.Write( "intQueue values:\t" );
DisplayValues( intQueue );
// Remove another element from the queue.
Console.WriteLine(
"\n(Dequeue)\t{0}", intQueue.Dequeue() );
// Display the Queue.
Console.Write( "intQueue values:\t" );
DisplayValues( intQueue );
// View the first element in the
// Queue but do not remove.
Console.WriteLine(
"\n(Peek) \t{0}", intQueue.Peek() );
// Display the Queue.
Console.Write( "intQueue values:\t" );
DisplayValues( intQueue );
}
public static void DisplayValues( IEnumerable myCollection )
{
IEnumerator myEnumerator =
myCollection.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "{0} ",myEnumerator.Current );
Console.WriteLine();
}
[STAThread]
static void Main()
{
TesterQueueDemo t = new TesterQueueDemo();
t.Run();
}
}
}