显示功能不显示数据
display function not displaying data
我正在制作树。它不显示。
我有一个字符串长度 4 说 SHAM 现在每个字符 S H A M 有四个指针并将 NULL 放入其中。但是当我编译并运行 display() 函数不起作用时。
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. Insertn2. Exitn";
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;
}
这是new
类型的错误方法:
p = new node[sizeof(node)]; // p = new node; is enough
你不需要那[sizeof(node)]
部分。另一方面,我看不出您如何初始化next
.
所以,我相信这段代码无法正常工作。
我认为你以错误的方式开始:你的节点结构包含指向指针的指针。
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 //
这是与树中下一个节点的连接
使用指针到指针来确定数据结构中的下一个元素是没有意义的(据我从您的示例中可以看出),您必须小心内存分配。
希望这有帮助
相关文章:
- 访问和打印元组中的数据,并使用 C++14 使用模板函数显示数据
- 它不显示数据,只显示永无止境的循环
- 在网格视图中显示数据 - 基于 MFC c++ 对话框的应用程序
- 使用 insertion(<<) 运算符和 "write" 函数在 C++ 中显示数据有什么区别?
- 使用 ARDUINO 和处理在 8x8 LED 矩阵上显示数据时出现问题
- 列表视图最初不显示数据
- C++服务器未显示数据包
- 模板堆栈类:堆栈中没有显示数据
- 使用选择排序对名称进行排序并显示数据
- 'top'程序用于在屏幕上重新显示数据的技术是什么?
- 以两列c++显示数据
- QTableView 显示列标题,但不显示数据
- 显示功能不显示数据
- 从变体VT_ARRAY读取和显示数据 |VT_BSTR
- 线程不刷新数据,在屏幕中显示数据时出错
- 使用C++与3000Hz的线阵扫描相机接口,并处理/显示数据
- QtableWidget不显示数据
- 不按我的要求显示数据输入表单
- 无法在 MySql 数据库中的 C++ QTableView 中显示数据 QSqlQueryModel
- 使用SDI应用程序(MFC)从文本文件读取数据并显示数据