C++ 显示功能不显示数据
我正在做一棵树。它没有显示 我有一个长度为4的字符串,现在每个字符SHAM有四个指针,并将NULL放入其中。但当我编译并运行display()函数时,它不起作用C++ 显示功能不显示数据,c++,C++,我正在做一棵树。它没有显示 我有一个长度为4的字符串,现在每个字符SHAM有四个指针,并将NULL放入其中。但当我编译并运行display()函数时,它不起作用 struct node { string info; struct node **next; } *front, *rear; void enqueue(string s) { node *p, *temp; p=new node[sizeof(node)]; stuff go
struct node
{
string info;
struct node **next;
} *front, *rear;
void enqueue(string s)
{
node *p, *temp;
p=new node[sizeof(node)];
stuff goes here...
}
}
void display()
{
int k = 0;
node *t, *temp;
t = front;
if(front == NULL || rear == NULL)
{
cout<<"\nQueue Empty!!!";
}
else
{
temp=t;
while(t!= NULL)
{
if(t->next[k] != NULL)
{
temp=t->next[k];
cout<<temp->info<<" ";
}
k++;
if(k==n.length())
{
k = 0;
t = t->next[k];
temp = t;
}
}
}
}
int main(int argc, char** argv)
{
int ch, len, x;
string string1;
rear = NULL;
front = NULL;
cout << "\n1. Insert\n2. Exit\n";
cout << "\nEnter Your Choice: ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\nEnter The String: ";
cin >> n;
len = n.length();
enqueue(n);
cout << " len " << len;
for(int p=1;p<=len;p++)
bnod+=pow(len,p);
cl = 0;
for (x = 0; x < len; x++)
{
string1=n.at(x);
enqueue(string1);
cl++;
}
display();
cout << "\n########################\n";
break;
case 2:
exit(0);
break;
default:
cout << "\nWrong Choice!!! Try Again.";
}
return 0;
}
struct节点
{
字符串信息;
结构节点**下一步;
}*前,*后;
无效排队(字符串s)
{
节点*p,*temp;
p=新节点[sizeof(节点)];
这里有东西。。。
}
}
无效显示()
{
int k=0;
节点*t,*temp;
t=前部;
如果(前==NULL | |后==NULL)
{
coutnext[k];
cout这是一种错误的方式来new
a类型:
p = new node[sizeof(node)]; // p = new node; is enough
你不需要那个[sizeof(node)]
部分。另一方面,我看不出你是如何初始化下一步的
因此,我认为这段代码不能正常工作。我认为您的启动方式是错误的:您的节点结构包含指向指针的指针
struct node
{
string info;
struct node **next;
};
这可能是代码没有意义的原因。您可以使用(通常您是这样做的)简单指针:
struct node
{
string info;
struct node *next;
};
这样,一切看起来都更易于管理:
node a;
a.info = "abc"; //this is your info
a.next = NULL //
这是到树中下一个节点的连接
使用指针对指针来确定数据结构中的下一个元素是毫无意义的(从您的示例中我可以看出),您必须小心内存分配
希望这对你有所帮助你在上一个问题中仍然有一些错误。首先修复这些错误。另外,“东西在这里…”是程序的一个重要部分。代码在哪里?哦,阅读错误和警告消息,他们可能非常清楚你必须做什么(如未声明的变量等)1.我已经做过了,现在是为长度N工作,不是为了修理length@Artemis这将有助于如果你更新你的代码。我们没有办法猜测你的代码中的众多问题中你已经固定的。先学习C++然后在论坛上……我在我的前一篇文章中纠正了这个问题。correctly@Artemis好吧,那就别担心t发布不是导致您出现问题的代码