创建一个带有整数链表的 n 数组

Creating an n array with a linked list of ints

本文关键字:链表 整数 数组 一个 创建      更新时间:2023-10-16

我最近做了一个26数组,并试图模拟字典。

我似乎不知道如何做到这一点。我尝试传递整数的链接列表而不是字符串。我当前的代码创建 26 个节点(a-z),然后每个节点都有 26 个节点(a-z)。我想实现一种使用 ints 执行此操作的方法,例如 (1-26)。这些 int 节点将表示项目,而我要传入的整数链接列表将包含一组我想在树中表示的整数,类似于字符串。

示例:传入集合 {1, 6, 8},而不是像"hello"这样的字符串

   #include <iostream>
using namespace std;
class N26
{
   private:
       struct N26Node 
        {
          bool isEnd;
          struct N26Node *children[26];
        }*head;
   public:
      N26();
      ~N26();
      void insert(string word);
      bool isExists(string word);
      void printPath(char searchKey);
};
N26::N26()
{
    head = new N26Node();
    head->isEnd = false;
}
N26::~N26()
{
}
void N26::insert(string word)
{
   N26Node *current = head;
   for(int i = 0; i < word.length(); i++)
   {
       int letter = (int)word[i] - (int)'a';
       if(current->children[letter] == NULL)
       {
           current->children[letter] = new N26Node();
       }
       current = current->children[letter];
   }
   current->isEnd = true;
}
/*      Pre:  A search key
 *     Post:  True is the search key is found in the tree, otherwise false
 *  Purpose:  To determine if a give data exists in the tree or not
 ******************************************************************************/
bool N26::isExists(string word)
{
    N26Node *current = head;
    for(int i=0; i<word.length(); i++)
    {
        if(current->children[((int)word[i]-(int)'a')] == NULL)
        {
            return false;
        }
        current = current->children[((int)word[i]-(int)'a')];
    }
    return current->isEnd;
}
class N26
{
  private:
    N26Node newNode(void);
    N26Node *mRootNode;
  ...    
};
N26Node *newNode(void)
{
  N26Node *mRootNode = new N26Node;
  mRootNode = NULL;
  mRootNode->mData = NULL;
  for ( int i = 0; i < 26; i++ )
    mRootNode->mAlphabet[i] = NULL;
  return mRootNode;
}

啊!我的眼睛!

说真的,你正在尝试一些太高级的事情。您的代码充满了错误,无法按预期工作。修补无济于事,您必须回到指针和链表的基础知识。学习基础知识,在了解上述代码的问题之前,不要尝试任何类似链表链表的东西

我会给你一些提示:"内存泄漏"、"悬空指针"、"类型不匹配"、"未定义的行为"。

我没有完全使用链表,但我设法使用数组让它工作。

/* ***  Author:       Jamie Roland
     *  Class:        CSI 281
     *  Institute:    Champlain College
     *  Last Update:  October 31, 2012
     *
     *  Description:
     *      This class is to implement an n26 trie.  The 
     *  operations
     *  available for this impementation are:
     *  
      *  1.  insert
     *  2.  isEmpty
      *  3.  isExists
     *  4.  remove
     *  5.  showInOrder
     *  6.  showPreOrder
     *  7.  showPostOrder
     *
     *  Certification of Authenticity:  
     *     I certify that this assignment is entirely my own work.
     **********************************************************************/
#include <iostream>
using namespace std;
class N26
{
   private:
       struct N26Node 
        {
          bool isEnd;
          struct N26Node *children[26];
        }*head;
   public:
      N26();
      ~N26();
      void insert(int word[]);
      bool isExists(int word[]);
      void printPath(char searchKey);
};
N26::N26()
{
    head = new N26Node();
    head->isEnd = false;
}
N26::~N26()
{
}
void N26::insert(int word[])
{
    int size = sizeof word/sizeof(int);
   N26Node *current = head;
   for(int i = 0; i < size; i++)
   {
       int letter = word[i] - 1;
       if(current->children[letter] == NULL)
       {
           current->children[letter] = new N26Node();
       }
       current = current->children[letter];
   }
   current->isEnd = true;
}
/*      Pre:  A search key
 *     Post:  True is the search key is found in the tree, otherwise false
 *  Purpose:  To determine if a give data exists in the tree or not
 ******************************************************************************/
bool N26::isExists(int word[])
{
    int size = sizeof word/sizeof(int);
    N26Node *current = head;
    for(int i=0; i<size; i++)
    {
        if(current->children[(word[i]-1)] == NULL)
        {
            return false;
        }
        current = current->children[(word[i]-1)];
    }
    return current->isEnd;
}