foreach不能对变量进行操作,不能使用getenumerator,链表

时间:2015-03-27 21:00:15

标签: c# foreach linked-list ienumerable singly-linked-list

我有一个简单的程序,我在编程课程中忙于单链表,而我正在尝试计算不同类型对象的数量,我有一个类单链接列表,App,Onceoff:App,Distrubuted :App,Cell

我想要实现的是获取单个链接列表中名为Developed的每个项目的对象类型,并尝试使用“is”语句确定它的类型并递增所需的计数器,但foreach用下划线表示跟随错误 “foreach无法对'PRr05.SinglyLinkedList'类型的变量进行操作,因为'PRr05.SinglyLinkedList'不包含'GetEnumarator'的公共定义”,我不完全确定这意味着什么,但如果我是正确的,则Enumarator返回类型,我该如何解决这个问题,

下面的SinglyLinkedList类

(ps,是的,这是实用的,但我更进一步;))

FOREACH METHOD

public int countApps(char Type)
        {
            int countO = 0, countD = 0, countR = 0;
            foreach (App item in Developed)
            {
                if (item is Onceoff)
                {
                    countO++;
                }
                else
                    if (item is Distributed)
                    {
                        countD++;
                    }
                    else countR++;
            }

            if (Type == 'O')
            {
                return countO;
            }

            if (Type == 'D')
            {
                return countD;
            }

            if (Type == 'R')
            {
                return countR;
            }
        }

SinglyLinkedList类

使用System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Pr05
{
    public class SinglyLinkedList       // NO CHANGES PERMITTED TO EXISTING METHODS IN THIS CLASS


     {
            // Sentinel cell
            private int counter;
            private Cell head;   // pointer to first cell in the list
            public SinglyLinkedList() 
            {
                counter = 0;
                head = null;
            }
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { get {return Current;}}
            object System.Collections.IEnumerator.Current { get { return Current; } }
            public int Count()  
            {
                return counter;
            }
            public Cell getFirst() 
            {
                return head;
            }
            public void addFirst(Object newItem) // Section Adding Cells at the Beginning pp 59 - 60
            /* pre:  Have object to be added to calling singly linked list object, which may be empty.
             * post: newItem is the element of the FIRST cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering AFTER the new first cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. */
            {
                // ADD CODE FOR addFirst HERE
                Cell newCell = new Cell(newItem);
                newCell.setNext(head);
                head = newCell;
                counter++;
            }
            public void addLast(Object newItem) // Section Adding Cells at the End pp 60 - 61
            /* pre:  Have object to be added to calling singly linked list object, which may be empty.
             * post: newItem is the element of the LAST cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering BEFORE the new last cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. 
             * CAREFUL: C# has certain restrictions which do not allow direct implemention of the code as specified in the 
             *          prescribed text.  Find a way around the restriction. */
            {
                // ADD CODE FOR addLast HERE
                if (this.Count() == 0)
                {
                    this.addFirst(newItem);
                    return;
                }
                Cell newCell = new Cell(newItem);
                Cell cur = head;
                while (cur.next() != null)
                    cur = cur.next();
                cur.setNext(newCell);
                counter++;
            }
            public Cell removeFirst()
            /* pre:  Have at least one cell in calling singly linked list object.
             * post: Return the cell removed, which is the first cell in the list.
             *       The counter is modified to reflect the removal of the first cell from the singly linked list. */
            {
                // ADD CODE FOR removeFirst HERE
                Cell cur = head;
                head = cur.next();
                cur.setNext(null);
                counter--;
                return cur;

            }
            public Cell removeLast()
            /* pre:  Have at least one cell in calling singly linked list object.
             * post: Return the cell removed, which is the last cell in the list.
             *       The counter is modified to reflect the removal of the last cell from the singly linked list. 
             * CAREFUL: C# has certain restrictions - find a way around the restriction. */
            {
                // ADD CODE FOR removeLast HERE
                Cell cur;
                if (this.Count() == 1)
                {
                    cur = head;
                    this.Clear();
                    return cur;
                }
                cur = head;
                Cell prev = head;
                while (cur.next() != null)
                {
                    prev = cur;
                    cur = cur.next();
                }
                prev.setNext(null);
                counter--;
                return cur;
             }
            public void addBefore(Object newItem, Cell link)
            /* pre:  Have object to be added to calling singly linked list object, and a link in the singly linked list BEFORE
             *       which the newItem's cell must be added.
             * post: newItem is the element of the added cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering relevant to the position of the newly added cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. */
            {
                // ADD CODE FOR addBefore HERE
                if (link == null)  // list either empty or must be added at end of list
                {
                    this.addLast(newItem);
                    return;
                }
                Cell newCell = new Cell(newItem);
                Cell cur = head;
                if (cur == link)  // must be added as first cell
                {
                    this.addFirst(newItem);
                    return;
                }
                while (cur.next() != link)
                    cur = cur.next();
                cur.setNext(newCell);
                newCell.setNext(link);
                counter++;
            }
            public void addAfter(Object newItem, Cell link) // Section Inserting Cells After Other Cells pp 61 - 62
            /* pre:  Have object to be added to calling singly linked list object, and a link in the singly linked list AFTER
             *       which the newItem's cell must be added.
             * post: newItem is the element of the added cell of the singly linked list.  All other existing cells of the
             *       singly linked list retain their ordering relevant to the position of the newly added cell.
             *       The counter is modified to reflect the addition of a new cell to the singly linked list. */
            {
                // ADD CODE FOR addAfter HERE
                if (link == null)
                {
                    this.addLast(newItem);
                    return;
                }
                Cell newCell = new Cell(newItem);
                newCell.setNext(link.next());
                link.setNext(newCell);
                counter++;
            }
            public void Clear()
            /* pre:  Have calling singly linked list object, which could be empty.
             * post: EFFICIENTLY clear the singly linked list. */
            {
                // ADD CODE FOR Clear HERE
                counter = 0;
                head = null;
            }
            // OPTIONAL TASKS
            public Cell removeBefore(Cell link)
            /* pre:  Have at least one cell in calling singly linked list object. Have a link in the singly linked list.
             * post: Return the cell removed, which is the  cell BEFORE the given link.
             *       The counter is modified to reflect the removal of the cell from the singly linked list. */
            {
                // ADD CODE FOR removeBefore HERE
                if (link == head) // nothing to remove in front of link
                    return null;
                if (link == null) // then remove last cell
                    return removeLast();
                Cell cur = head;
                Cell prev = head;
                while (cur.next() != link)
                {
                    prev = cur;
                    cur = cur.next();
                }
                if (cur == head)
                    return removeFirst();
                prev.setNext(link);
                cur.setNext(null);
                counter--;
                return cur;
            }
            public Cell removeAfter(Cell link) // Section Deleting Cells pp 62 - 63
            /* pre:  Have at least one cell in calling singly linked list object. Have a link in the singly linked list.
             * post: Return the cell removed, which is the  cell AFTER the given link.
             *       The counter is modified to reflect the removal of the cell from the singly linked list. */
            {
                // ADD CODE FOR removeAfter HERE
                if (link == null)   // nothing after
                    return null;
                if (link.next() == null) // then nothing after link
                    return null;
                Cell cur = link.next();
                link.setNext(cur.next());
                cur.setNext(null);
                counter--;
                return cur;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您需要在代码的语法和概念方面进行一些必要的基本更改。例如,首先您应该知道foreach仅用于实现System.Collections.IEnumberableSystem.Collections.Generic.IEnumberable<T>的对象。这里有一个转折点;如果你想迭代你的Developed班(也许)成员,你应该使用反思;如果您想让Developed类与foreach语句一起使用,则必须从我提到的前两个BCL类之一派生。请考虑修改您的代码。