C++ 创建具有整数链接列表的n数组

C++ 创建具有整数链接列表的n数组,c++,pointers,tree,trie,C++,Pointers,Tree,Trie,我最近制作了一个数组,并试图模拟字典 我似乎不知道该怎么做。我尝试过传递一个int的linkedlist,而不是字符串。我当前的代码创建了26个节点(a-z),然后每个节点都有26个节点(a-z)。我想用int实现一种方法,比如(1-26)。这些int节点将表示项,我要传入的int的linkedlist将包含一组我希望在树中表示的类似于字符串的int 示例:传入集合{1,6,8},而不是像“hello”这样的字符串 #包括 使用名称空间std; N26类 { 私人: 结构N26节点 { 布尔伊森

我最近制作了一个数组,并试图模拟字典

我似乎不知道该怎么做。我尝试过传递一个int的linkedlist,而不是字符串。我当前的代码创建了26个节点(a-z),然后每个节点都有26个节点(a-z)。我想用int实现一种方法,比如(1-26)。这些int节点将表示项,我要传入的int的linkedlist将包含一组我希望在树中表示的类似于字符串的int

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

#包括
使用名称空间std;
N26类
{
私人:
结构N26节点
{
布尔伊森德;
结构N26节点*子节点[26];
}*头部;
公众:
N26();
~N26();
空白插入(字符串字);
布尔isExists(字符串字);
无效打印路径(字符搜索键);
};
N26::N26()
{
头部=新的N26节点();
head->isEnd=false;
}
N26::~N26()
{
}
void N26::插入(字符串字)
{
N26节点*电流=水头;
for(int i=0;i子项[字母]==NULL)
{
当前->子节点[字母]=新的N26节点();
}
当前=当前->子项[字母];
}
当前->isEnd=true;
}
/*Pre:搜索键
*Post:True表示在树中找到搜索键,否则为false
*目的:确定树中是否存在给定数据
******************************************************************************/
booln26::isExists(字符串字)
{
N26节点*电流=水头;
对于(int i=0;ichildren[((int)字[i]-(int)'a')]==NULL)
{
返回false;
}
当前=当前->子项[((int)字[i]-(int)'a');
}
返回当前->isEnd;
}
N26级
{
私人:
n26节点新节点(void);
N26节点*mRootNode;
...    
};
N26节点*新节点(无效)
{
n26节点*mRootNode=新的n26节点;
mRootNode=NULL;
mRootNode->mData=NULL;
对于(int i=0;i<26;i++)
mRootNode->mAlphabet[i]=NULL;
返回mRootNode;
}
啊!!我的眼睛

说真的,你尝试的东西太先进了。您的代码充满了bug,无法按预期工作。修补不会有帮助,您必须回到指针和链表的基础知识。学习基础知识,在理解上述代码的错误之前,不要尝试任何类似于链表链表的操作


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

我没有完全使用链表,但我使用数组成功地实现了它

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

}
/****作者:杰米·罗兰
*类别:CSI 281
*学院:尚普兰学院
*最后更新:2012年10月31日
*
*说明:
*此类用于实现n26 trie。这个
*操作
*可用于此实施的有:
*  
*  1.  插入
*  2.  栈空
*  3.  伊斯兰主义者
*  4.  去除
*  5.  显示顺序
*  6.  showPreOrder
*  7.  邮购
*
*真伪认证:
*本人证明此任务完全是本人的工作。
**********************************************************************/
#包括
使用名称空间std;
N26类
{
私人:
结构N26节点
{
布尔伊森德;
结构N26节点*子节点[26];
}*头部;
公众:
N26();
~N26();
空白插入(int-word[]);
bool-isExists(int-word[]);
无效打印路径(字符搜索键);
};
N26::N26()
{
头部=新的N26节点();
head->isEnd=false;
}
N26::~N26()
{
}
void N26::插入(int-word[])
{
int size=sizeof word/sizeof(int);
N26节点*电流=水头;
对于(int i=0;i子项[字母]==NULL)
{
当前->子节点[字母]=新的N26节点();
}
当前=当前->子项[字母];
}
当前->isEnd=true;
}
/*Pre:搜索键
*Post:True表示在树中找到搜索键,否则为false
*目的:确定树中是否存在给定数据
******************************************************************************/
bool N26::isExists(int-word[])
{
int size=sizeof word/sizeof(int);
N26节点*电流=水头;
for(int i=0;ichildren[(字[i]-1)]==NULL)
{
返回false;
}
当前=当前->子项[(字[i]-1)];
}
返回当前->isEnd;
}

从你的描述中,我不明白你想做什么。这个问题很难回答,因为强烈的诱惑是讨论实现解决方案的更好方法,而不是在这里尝试使用您的代码。如果你为解决方案设置了这个特定的表单,最好在你对你的代码有一个特定的问题之前一直胡乱处理它。这是我试图构建的结构的图像。基本上每个节点都有一个计数器。最顶层的节点跟踪每个int在数据集中出现的次数。下一层表示所有可能的2项集,第三层表示所有可能的3项集。节点旁边的蓝色数字表示输入数据集后的计数器。问题中的代码会泄漏它分配的所有内存,但这与当前的问题完全不同。@user1898442:为什么要跟踪每个int在数据集中出现的次数?我无法想象这样的东西有什么用处,除了。哦,天啊,哇,我上传错文件了!你介意再看一下吗?也许这次没那么糟。@user1898442:看起来没那么糟。它有用吗?它是如何失败的?
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;

}