我有一个像这样的通用链表结构
template <typename E, typename F>
struct node
{
public:
E data;
node<E, F>* next;
node<F, F>* down;
};
还有一个这样的类
class LetterList
{
private:
node <char, Dictionary> *head;
public:
LetterList(){head = NULL;};
void createLetterList();
void print();
};
“node *head;”中的字典是另一个类; 我想做的是使用 LetterList 类将所有字母表插入到链表中。这是我的代码..
node <char, Dictionary> *p = new node <char, Dictionary>;
p->data = 'A';
char ch;
if (head == NULL)
{
p->next = NULL;
head = p;
}
node <char, Dictionary> *q = head;
while (true)
{
for (int i=66; i<91;i++)
{
ch = char (i);
p->data = ch;
q ->next = p;
if (i == 90)
{
q->next = NULL;
}
else
q = q->next;
}
break;
}
}
执行这段代码后,链表的头部是'Z',但它不应该是'A'吗?请告诉我我做错了什么。
最佳答案
您只分配一个节点。如果您希望列表中有节点“A”到“Z”,那么您需要随时分配一个新节点以将其添加到列表中。
关于c++ - 插入到链表后,value head value 是最后输入的值。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114350/