Hi,
I want to implement a queue using linked list in c#.net. This queue will hold PdfDocument and WordDocument object. But I don't want to use standard container which are available in c#. I want to implement it by pure data structure. This queue should have two functions
1) PUSH() which will insert document in queue
and
2) POP() will retrive the document from the queue.
If any body any idea please share with me. It will be highly appreciated…
Thanks & Regards
Nabhendu
VulpesPosted Mar 22, 2011, 12:54 PM
Notice that linked lists are not very efficient in .NET (except possibly for inserting nodes at random positions) and so it won't work as quickly as the .NET implementation of Queue
using System;
using System.Collections.Generic;
class Test
{
static void Main()
{
Queue
q.Push("C#");
q.Push("Corner");
Console.WriteLine(q.Pop()); // C#
Console.WriteLine(q.Pop()); // Corner
Console.WriteLine(q.Pop()); // null
Console.ReadKey();
}
}
public class Queue
{
private LinkedList
public T Pop()
{
if (list.Count > 0)
{
LinkedListNode
list.RemoveFirst();
return first.Value;
}
else
{
return default(T); // or throw an exception
}
}
public void Push(T item)
{
list.AddLast(item); // EDIT: sorry had AddFirst here to start with!
}
}