创建一个带有整数链表的 n 数组
Creating an n array with a linked list of ints
我最近做了一个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;
}
相关文章:
- 反向给定链表中的K节点
- 如果没有malloc,链表实现将失败
- 文本文件中的单词链表
- 努力将整数转换为链表。不知道我在这里做错了什么
- 链表,反向函数,数据结构
- 使用std::list创建循环链表
- 链表的泛型函数remove()与成员函数remove)
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 我们可以删除链表中静态内存中的节点吗
- 如何实现容纳整数和无效指针的双向链表?
- C++ 从链表中删除给定整数的倍数
- 如何将自己的链表实现从存储整数更改为存储个人数据
- 如何将唯一的随机整数插入链表中
- 创建一个带有整数链表的 n 数组
- 从文本文件将整数读入简单的链表中.然后对整数列表进行气泡排序并读出到另一个文件
- 将整数按升序插入链表
- 使用C++当我同时具有整数和字符数组时,如何将.txt文件中的数据作为链表的一部分访问
- C++使用链表、模板和堆栈设计了一个大整数加减法类
- 删除链表中的元素时出现随机整数
- 有效地获取C++链表中最大的3个整数(未排序)