i have a generic linked list struct like this
template <typename E, typename F>
struct node
{
public:
E data;
node<E, F>* next;
node<F, F>* down;
};
and one class like this
class LetterList
{
private:
node <char, Dictionary> *head;
public:
LetterList(){head = NULL;};
void createLetterList();
void print();
};
the Dictionary in “node *head;” is another class;
what i would like to do is insert all the alphabets in to linked list using the LetterList class. Here is the code i have for that..
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;
}
}
after executing this code the head of the linked list is ‘Z’, but shouldn’t it be ‘A’? please tell me what i have done wrong in this.
You are only allocating one node. IF you want to have nodes ‘A’ to ‘Z’ in the list then you need to allocate a new node anytime to add it to the list.