-1

我的问题是这个。我有一个学校作业,要求我将一个新方法添加到给定的链接链接类中,并且我不允许对我当前的链接列表类进行任何更改

iii. RemoveParcelAtPosition(int n):此方法将删除链表中位置 n 处的节点。假设链表的第一个节点的位置编号为 1,第二个节点的位置编号为 2,依此类推。

class LinkedList
{
    private Node head;  // first node in the linked list
    private int count;

    public int Count
    {
        get { return count; }
        set { count = value; }
    }

    public Node Head
    {
        get { return head; }
    }
    public LinkedList()
    {
        head = null;    // creates an empty linked list
        count = 0;
    }

public void AddFront(int n)
{
        Node newNode = new Node(n);
        newNode.Link = head;
        head = newNode;
        count++;

}

    public void DeleteFront()
    {
        if (count > 0)
        {
            Node temp = head;
            head = temp.Link;
            temp = null;
            count--;
        }
    }


}
4

2 回答 2

3

使用继承。使用您所需的方法创建一个新类。继承你的类中的Linkedlist类。

于 2013-06-29T06:43:30.360 回答
2

可能你应该使用扩展方法

像这样的东西

namespace LinkedListExtension
{
    public static class MyExtensions
    {
        public static void RemoveParcelAtPosition(this LinkedList, int n)
        {
            // remove here
        }
    }   
}

这个方法调用看起来像这样:

_yourLinkedList.RemoveParcelAtPosition(position);
于 2013-06-29T06:41:34.890 回答