C#链接列表:实现队列 [英] C# Linked Lists: Implementing a Queue

查看:101
本文介绍了C#链接列表:实现队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想使用C#.net中的链表实现队列,但不想使用C#中提供的标准容器。此队列将包含 PdfDocument WordDocument 对象。



这个队列有两个函数: PUSH()这将把文件插入队列并且 POP()将从队列中检索文档。



如果有人有任何想法请与我分享。我们将非常感谢



谢谢。

Nabhendu

解决方案

As对您的问题发表评论的其他人已经说过我也建议您使用.NET中提供的标准类。最重要的是这样:





  1. Push和Pop是带有堆栈内涵的方法名称。将某些东西推到一个堆栈上并用Pop从中移除。堆栈是充当 LIFO 的数据结构,这意味着 L ast I n F irst O 的UT。最近推送的元素将在之前推送的任何元素之前弹出。

  2. 队列的正确方法名称为Enqueue,用于将元素放入队列和从队列中取出一个元素的Dequeue。这样做的方式称为 FIFO ,意思是 F irst I n F irst O 的UT。在元素B之前放置到队列中的元素A也将从元素B之前的队列中删除。(当谈论优先级队列时,存在细微差别,但您可以随时阅读它。





至于如何实现:



  1. 你的Queue类需要两个对QueueElement类实例的引用。



    1. 链接方向是从队列开始到队列尾部
    2. Head :指向要删除元素的队列开头(Dequeue)
    3. Tail :指向要添加元素的队列末尾(入队)



  2. 使Queue和QueueElement通用,以便您可以为各种类创建队列。

  3. 要将元素排入队列,请执行以下操作:



    1. 创建一个QueueElement实例
    2. 将内容元素存储到QueueElement中
    3. 使尾部指向的QueueElement的后继者指向刚刚创建的QueueElement。
    4. Make队列的尾部引用指向刚刚创建的QueueElement。
    5. 如果head为null,则make head也指向与tail相同的元素。



  4. 要使元素出列,请执行以下操作:



    1. 将头部引用存储在局部变量中。
    2. 将头部引用点指向头部QueueElement的后继元素。
    3. 链接列表末尾的元素始终为null它是继承者。
    4. 如果队列的头部为空,则将尾部设置为null
    5. 返回存储在局部变量中的引用。







这或多或少。



快乐编码!


但为什么你想要t o重新发明那个轮子? .Net框架已经拥有一个功能完备的队列类。您可以使用扩展方法扩展它,并获得经过充分测试的代码的所有好处,而无需实际测试基本队列功能。您要添加哪些功能尚未添加?您认为自己可以做得更好?


  class 节点{
< span class =code-keyword> private int data { get ; set ;}
public 节点之前的{ get ; set ;}
public 节点( int val)
{
data = val;
previous = null ;
}
}

class 队列{
私人节点正面,背面,上一个= null ;
public 队列()
{
正面=背面= null ;
}
public void Enque( int val)
{
节点n = 节点(val);
if (Prev == null
{
Front =后= n;
}
其他 如果(前==后方)
{
Rear.previous = n;
Front = Rear.previous;
}
else
{
Front.previous = n;
Front = Front.previous;
}
}
public int Deque()
{
int val = Rear.data;
Rear = Rear.previous;
return val;
}
}





我没有测试过这个解决方案,所以在使用前测试一下。我也是在很短的时间内完成的,所以如果你发现任何错误或任何其他故障,请告诉我。感谢。


Hi,

I want to implement a queue using a linked list in C#.net but don’t want to use a standard container which are available in C#. This queue will hold a PdfDocument and WordDocument object.

This queue has two functions: PUSH() which will insert documents into the queue and POP() will retrieve the documents from the queue.

If anybody has any ideas please share with me. It will be highly appreciated

Thanks.
Nabhendu

解决方案

As the others that commented on your question already said I too advise you to use the standard classes available in .NET. Most importantly though is this:


  1. Push and Pop are method names that carry the connotation of a stack. Something is "Pushed" onto a stack and removed from it with a "Pop". A stack is a datastructure that acts as a LIFO which means Last In First Out. The most recently pushed element will be popped before any element that was pushed before that.
  2. The correct method names for a queue are "Enqueue" for placing an element into the queue and "Dequeue" for taking an element from the queue. The manner in which this done is called FIFO, meaning First In First Out. An element A that was placed into a queue before element B will also be removed from the queue before element B. (When talking about priority queues there are subtle differences, but you can always read up on that later.



As to how to implement that:


  1. Your Queue class will need two references to instances of the QueueElement class.


    1. The linking direction is from start of queue to tail of queue
    2. Head: Points to the start of the queue where elements will be removed (Dequeue)
    3. Tail: Points to the end of the queue where elements will be added (Enqueue)


  2. Make Queue and QueueElement generic so that you'll have the possibility to create a queue for all kinds of classes.
  3. To enqueue an element do this:


    1. Create a QueueElement instance
    2. Store the content element into the QueueElement
    3. Make the successor of the QueueElement that is pointed to by tail point to the QueueElement just created.
    4. Make the tail reference of the queue point to the just created QueueElement.
    5. If head was null make head also point to the same element as tail.


  4. To dequeue an element do this:


    1. Store the head reference in a local variable.
    2. Make the head reference point to the successor element of the head QueueElement.
    3. The element at the end of the linked list always has null as it's successor.
    4. If the head of the queue is null set the tail also to null
    5. Return the reference stored in the local variable.




That's more or less it.

Happy coding!


But why do you "want" to reinvent that wheel? The .Net framework already has a perfectly capable queue class. You can extend it with extension methods, and get all the benefits of fully tested code without actually having to test the basic queue functionality. What functionality do you want to add that it doesn't already have? What do you think you could do better?


class Node {
  private int data {get; set;}
  public Node previous {get; set;}
  public Node (int val)
  {
    data = val;
    previous = null;
  }
}

class Queue {
  private Node Front, Rear, Prev = null;
  public Queue()
  {
    Front = Rear = null;
  }
  public void Enque(int val)
  {
    Node n = new Node(val);
    if (Prev == null)
    {
      Front = Rear = n;
    }
    else if (Front == Rear)
    {
      Rear.previous = n;
      Front = Rear.previous;
    }
    else
    {
      Front.previous = n;
      Front = Front.previous;
    }
  }
  public int Deque()
  {
    int val = Rear.data;
    Rear = Rear.previous;
    return val;
  }
}



I haven't tested this solution so test it before use. Also I did it in short amount of time so if you find any bugs or any other glitch, please let me know. Thanks.


这篇关于C#链接列表:实现队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆