使用int的链接列表创建n数组

时间:2012-12-12 16:31:24

标签: c++ pointers tree trie

我最近做了一个26array并尝试模拟字典。

我似乎无法弄清楚如何做到这一点。我尝试过传入一个int而不是字符串的链表。我当前的代码创建了26个节点(a-z),然后每个节点都有26个节点(a-z)。我想用int来实现一种方法,比如说(1-26)。这些int节点将表示项目,我想要传入的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;

}

2 个答案:

答案 0 :(得分:1)

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;
}

啊!我的眼睛!

说真的,你正在尝试一些太先进的东西。您的代码充满了错误,无法按预期工作。修补无济于事,你必须回到指针和链表的基础知识。研究基础知识,不要尝试任何类似链接列表的链接,直到您了解上述代码的错误为止。

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

答案 1 :(得分:0)

我没有使用链表,但我设法使用数组。

/* ***  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;

}