显示功能不显示数据

display function not displaying data

本文关键字:显示 数据 功能      更新时间:2023-10-16

我正在制作树。它不显示。

我有一个字符串长度 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 //

这是与树中下一个节点的连接

使用指针到指针来确定数据结构中的下一个元素是没有意义的(据我从您的示例中可以看出),您必须小心内存分配。

希望这有帮助