[英]LinkedLists: Variable automatically updating without re-assignmenet
因此,发生的事情是,链表的头部在每次迭代中都得到了更新,这本不该发生,并且我无法对其进行调试。 任何帮助将不胜感激。
// Given arbitrarily big string with alphabet [ Implement using singly or doubly linked list ]
// Store the each alphabet into linked list
// Example
// Input:
// SSSKKBBBBHHHJJJJKKKK
// Store
这是问题(上)
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class ListNode
{ //class for a node of the linked list
public:
ListNode *next = NULL; //pointer to next node
ListNode *prev = NULL; //pointer to previous node
char data; //the alphabet present in this node
int frequency; //number of times this alphabet is present in the list
ListNode(char data)
{
this->data = data;
frequency = 1;
}
};
class LinkedList
{ //class for the entire linked list
public:
ListNode **Head = NULL;
ListNode **Tail = NULL;
void printlist()
{
ListNode *temp = *Head;
while (temp != NULL)
{
cout << temp->data << temp->frequency << " ";
temp = temp->next;
}
return;
}
void insertNodeFromEnd(char c)
{
ListNode *newNode = new ListNode(c);
cout << "input character: " << c << endl;
if(Head != NULL){cout << "head = " << (*Head)->data << endl;}
if (Head == NULL) //if the list is empty, point the head and tail to the node pointer of newly created node.
{
Head = &newNode;
Tail = Head;
cout << "empty condition: ";
cout << "tail = " << (*Tail)->data << " ";
cout << "head = " << (*Head)->data << endl;
}
else //else point the tail to pointer of newly created node.
{
{
(*Tail)->next = newNode;
Tail = &newNode;
cout << "regular condition: ";
cout << "tail = " << (*Tail)->data << " ";
cout << "head = " << (*Head)->data << endl;
}
}
return;
}
};
这是主要功能
int main(int argc, char const *argv[])
{
string s;
cin >> s;
int T;
cin >> T;
if (s.length() == 0)
{
cout << "Error! \n enter atleast one letter \n exiting...";
return 0;
}
else
{
LinkedList listofalphabet;
for (char &c : s)
{
listofalphabet.insertNodeFromEnd(c);
}
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.