简体   繁体   English

链表显示功能不正常

[英]The display function in linked list is not working properly

When I call the display function, the first element of linked list is printed twice.当我调用 display 函数时,链表的第一个元素被打印两次。 I do not know what is wrong with the code.我不知道代码有什么问题。 Please help me to figure it out.请帮我弄清楚。 The code is as follows:代码如下:

#include <iostream>
using namespace std;
class node{
public:
    char data;
    node *link;
};

class linklist{
    private:
        node *start, *temp, *cur;
    public:
        linklist(){
            start = NULL;
        }
        void insert(char x){
            if (start == NULL){
                start = new node;
                start->data = x;
                start->link = NULL;
                cur = start;
            }
            else
            while (cur->link != NULL){
                cur = cur->link;
            }

                temp = new node;
                temp->data = x;
                temp->link = NULL;
                cur->link = temp;

        }
        void display(){
            cur = start;

            while (cur->link != NULL){  
                cout << "Value is: " << cur->data << endl;
                cur = cur->link;
            }
            cout << "Value is: " << cur->data << endl;
        }
};
int main(){
    linklist obj;
    obj.insert('e');
    obj.insert('t');
    obj.insert('r');
    obj.insert('w');
    obj.insert('l');
    obj.display();

    system("Pause");
}

the expected output is: etrwl .预期的输出是: etrwl
Actual output: eetrwl实际输出: eetrwl

You have two problems with your code.您的代码有两个问题。 The first is the missing {...} surrounding your else statement in insert() .第一个是在insert()围绕else语句丢失的{...} In order to have the needed code only apply to the else case, you need to surround the entire else case with braces, eg:为了让所需的代码仅适用于else情况,您需要用大括号将整个else情况括起来,例如:

        else {
            while (cur->link != NULL) {
                cur = cur->link;
            }
            temp = new node;
            temp->data = x;
            temp->link = NULL;
            cur->link = temp;
        }
    }

Secondly, your conditional used for display() is incorrect.其次,您用于display()条件不正确。 You only want to output the contents when cur != NULL not when cur->next != NULL (that is why you tried to tack an extra output statement following the end of the loop to catch your last value).您只想在cur != NULL时输出内容,而不是cur->next != NULL时输出内容(这就是为什么您试图在循环结束后添加额外的输出语句以捕获您的最后一个值)。 Don't do that, if you find yourself trying to do something like that -- your are probably doing it wrong.不要那样做,如果你发现自己试图做那样的事情——你可能做错了。

With that change, you simply have:通过这种变化,您只需:

    void display() {
        cur = start;
        while (cur != NULL) {  
            cout << "Value is: " << cur->data << endl;
            cur = cur->link;
        }
    }

Putting it altogether, you have:总而言之,你有:

#include <iostream>

using namespace std;

class node{
public:
    char data;
    node *link;
};

class linklist{
private:
    node *start, *temp, *cur;
public:
    linklist() {
        start = NULL;
    }

    void insert (char x) {
        if (start == NULL) {
            start = new node;
            start->data = x;
            start->link = NULL;
            cur = start;
        }
        else {
            while (cur->link != NULL) {
                cur = cur->link;
            }
            temp = new node;
            temp->data = x;
            temp->link = NULL;
            cur->link = temp;
        }
    }

    void display() {
        cur = start;
        while (cur != NULL) {  
            cout << "Value is: " << cur->data << endl;
            cur = cur->link;
        }
    }
};

int main (void) {

    linklist obj;

    obj.insert('e');
    obj.insert('t');
    obj.insert('r');
    obj.insert('w');
    obj.insert('l');
    obj.display();

    system("Pause");
}

Example Use/Output示例使用/输出

$ ./bin/llinsert
Value is: e
Value is: t
Value is: r
Value is: w
Value is: l

Look things over an let me know if you have further questions.如果您有其他问题,请仔细查看并告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM