I'm stuck on creating a linked queue class. I am not sure on the properties of the class. Would I need a head, top, and rear property or just a top and rear property? How would I code it to do the Enqueue and Dequeue? Thanks.
Code:using System; using System.Windows.Forms; using System.Collections.Generic; using System.Text; namespace LinkedQueueLib { public class QueueNode { private object data; private QueueNode next; public QueueNode(object dataValue) // Initializing Constructor { this.data = dataValue; this.next = null; } public object Data // Read-only Data Property { get { return this.data; } set { this.data = value; } } public QueueNode Next // Read/Write Next property { get { return this.next; } set { this.next = value; } } } // End class QueueNode //Begin class LinkedQueue public class LinkedQueue { private string name; private int size; private QueueNode front; private QueueNode rear; private QueueNode head; public LinkedQueue() // Default constructor { this.name = "Linked Queue"; this.size = 0; this.head = new QueueNode(null); this.rear = this.head; this.front = this.head; } public LinkedQueue(string nameValue) //Initializing constructor { this.name = nameValue; this.size = 0; this.head = new QueueNode(null); this.rear = this.head; this.front = this.head; } public string Name // Define read/write Name property { get { return this.name; } set { this.name = value; } } public int Size // Define read-only Size property { get { return this.size; } } public bool Empty // Return true (false) if queue is (not) empty { get { return (this.size == 0); } } public void Purge() // Remove all queue datas { while (!this.Empty) this.Dequeue(); } public void Enqueue(object data) // Implement the enqueue operation { QueueNode queueNode = new QueueNode(data); this.rear.Next = queueNode; this.front.Next = this.rear.Next; this.rear = queueNode; this.size++; } public object Dequeue() // Implement the dequeue operation { object data = null; data = this.head.Next.Data; this.head.Next = this.front.Next; this.size--; return data; } } }
Last edited by WingedPanther; 04-13-2009 at 05:37 PM. Reason: add code tags (the # button)
Here's a linked-list based queue in C++, it may help: ADTs: Stacks and Queues Part 2: Using Lists
So your class would have behaviour of both LinkedList and Queue? First of all, would it be better to make it generic? My guess is yes.
Here are the generic classes:
LinkedList(T) Class (System.Collections.Generic)
Queue(T) Class (System.Collections.Generic)
I could offer making the whole class for you, if you really need it. I just do not know how to make it from your prototype. Let me know then.
Regards,
Arek Bulski
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
I needed that too, Thanks Arek.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks