将字符串中的每个单词添加并计算到链表



我首先要说的是,我还没有完全理解OOP。

我需要一个例程来迭代字符串中的每个单词,检查它是否在我的链表中,如果不是,则将其添加为节点,或者如果它在列表中,则增加现有节点的计数。

这是我所拥有的:

private void CountWords(string cleanString)
        {
            WordNode nextNode, prevNode;
            WordNode addNode;
            foreach (string stringWord in cleanString.Split(' '))
            {
                if (head == null)
                {
                    // No items in list, add to the beginning
                    addNode = new WordNode(stringWord);
                    head = addNode;
                }
                else
                {
                    if (String.Compare(stringWord, head.Word) < 0)
                    {
                        // If stringWord belongs at the beginning of the list, put it there
                        addNode = new WordNode(stringWord);
                        addNode.NextWord = head;
                        head = addNode;
                    }
                    else if (String.Compare(stringWord, head.Word) == 0)
                    {
                        // If stringWord is equal to head.Word, increase count
                        addNode.Count += 1;
                    }
                    else
                    {
                        prevNode = head;
                        nextNode = head.NextWord;
                        // If it doesn't belong at the beginning, cycle through the list until you find where it does belong
                        while ((nextNode != null) && (String.Compare(nextNode.Word, addNode.Word) < 0))
                        {
                            prevNode = nextNode;
                            nextNode = nextNode.NextWord;
                        }
                        if (nextNode == null)
                        {
                            prevNode.NextWord = addNode;
                        }
                        else
                        {
                            prevNode.NextWord = addNode;
                            addNode.NextWord = nextNode;
                        }
                    }
                }
            }
        }

在此之前,我尝试 addNode = new WordNode(stringWord); 在每次迭代开始时通过"for string 中的每个单词"循环,但这将重新定义类并将计数重置为 1。 现在,目前,我无法增加计数,因为addNode.Count += 1;未定义。 我希望我可以检查字符串是否在链表中,如果是,请将stringWord.count增加一,但这会引发错误。

现在看到这个,我认为addNode.Count += 1;属于它下面的几行的while循环......

这是我的WordNode类:

class WordNode
    {
        // constants
        // variables
        private string data;            // this is our only data, so also key
        private int count;
        private WordNode next;          // this is reference to next Node
        // constructors
        public WordNode(string newValue)
        {
            Word = newValue;
            count = 1;
            NextWord = null;
        }
        // methods
        public string Word
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }
        public int Count
        {
            get
            {
                return count;
            }
            set
            {
                count = value;
            }
        }
        public WordNode NextWord
        {
            get
            {
                return next;
            }
            set
            {
                next = value;
            }
        }
    }

试试这个:

private void CountWords(string cleanString)
{
    foreach (string stringWord in cleanString.Split(' '))
    {
        if (head == null)
        {
            head = new WordNode(stringWord);
        }
        else
        {
            var last = (WordNode)null;
            var current = head;
            do
            {
                if (current.Word == stringWord)
                {
                    break;
                }
                last = current;
                current = current.NextWord;
            } while (current != null);
            if (current != null)
            {
                current.Count++;
            }
            else
            {
                last.NextWord = new WordNode(stringWord);
            }
        }
    }
}

另一种方法是使用 linq:

var query =
    cleanString
        .Split(' ')
        .ToLookup(x => x)
        .Select(x => new
        {
            Word = x.Key,
            Count = x.Count(),
        });

听起来你只是在尝试练习制作链表,所以这可能没有帮助,但更简单的解决方案是像字典一样使用键/值对。

Dictionary<string, int> Words = new Dictionary<string, int>();
string wordsList = "a list of words for testing a list of words for testing";
foreach (string word in wordsList.Split(' '))
{
   if (Words[word] == null)
      Words[word] = 1;
   else
      Words[word] += 1;
}
System.Console.WriteLine("testing: {0}", Words["testing"]); //result- testing: 2

按字符串索引单词字典的结果将返回单词数。

这看起来像字典或这里的工作

Dictionary<string,int> myDict = new Dictionary<string,int>();
foreach(string str in listOfWords)
{
   myDict.add(str,0);
}
foreach(string x in cleanText.split(' '))
{
if(myDict.ContainsKey(x))
   myDict[x]+=1;
}

运行完foreach myDict后,将包含"单词袋"中每个单词的计数。

编辑

if(myDict == null)
   myDict = new Dictionary<string,int>(); //assuming running tally at higher scope.

foreach(string x in cleanText.split(' '))
{
if(myDict.ContainsKey(x))
   myDict[x]+=1;
else
   myDict.add(x,1);
}

最新更新