链接列表方法帮助c#

时间:2014-11-04 14:37:22

标签: c#

从头开始我的链接列表程序。我已设法显示我的列表,但现在我想将列表中的项目数显示为int,在我的情况下将为3。

有人可以给我一些指示

主要:

  class Program
    {
        static void Main(string[] args)
        {
            LinkList testList = new LinkList();

            testList.AddItem(5);
            testList.AddItem(10);
            testList.AddItem(12);
            testList.DisplayItems();

            Console.WriteLine(testList.NumberOfItems()); /// Still to do


            Console.ReadKey();
        }
    }
}

LinkList的类:

  class LinkList
       {
        private Link list = null; //default value – empty list

        public void AddItem(int item) //add item to front of list
        {
            list = new Link(item, list);
        }

        public void DisplayItems() // Displays items in list
        {
            Link temp = list;
            while (temp != null)
            {
                Console.WriteLine(temp.Data);
                temp = temp.Next;
            }

        }

        public int NumberOfItems() // returns number of items in list
        {
            Link temp = list;
            while (temp != null) 
            {
             /// How can i display the number of items in list as an int?
            }    


            }


    }

}

链接类:

class Link
{
    private int data;
    private Link next;

    public Link(int item) //constructor with an item
    {
        data = item;
        next = null;
    }
    public Link(int item, Link list) //constructor with item and list
    {
        data = item;
        next = list;
    }

    public int Data //property for data
    {
        set { this.data = value; }
        get { return this.data; }
    }

    public Link Next //property for next
    {
        set { this.next = value; }
        get { return this.next; }
    }

   }
}

2 个答案:

答案 0 :(得分:1)

因此,声明一个私有变量的项目数,然后在Add方法上增加它。 最后,添加一个Property以将此私有变量作为Public Int返回。

这应该有效:

       public class LinkList
       {
          private Link list = null; //default value – empty list

          private numberOfItems = 0;

          public void AddItem(int item) //add item to front of list
          {
              list = new Link(item, list);
              numberOfItems++;
          }

          public void DisplayItems() // Displays items in list
          {
              Link temp = list;
              while (temp != null)
              {
                  Console.WriteLine(temp.Data);
                  temp = temp.Next;
              }

          }

          public int NumberOfItems
          {

              get {return numberOfItems; }

          }

      }

答案 1 :(得分:0)

您需要迭代列表(在Display内完成)

public void DisplayItems() // Displays items in list
    {
        Link temp = list;
        while (temp != null)
        {
            Console.WriteLine(temp.Data);
            temp = temp.Next;
        }

    }

    public int NumberOfItems() // returns number of items in list
    {
        Link temp = list;
        int i = 0;
        while (temp != null) 
        {
            temp = temp.Next;
            i++;
        }    
        return i;
    }