-2

I typed 1234, but the list has 4,3,2,1 in it. I suspect the problem is getchar() itself, or a function in the class, but I have no way to find out.
The link class is responsible for some linked list operations, such as deletion, insertion, etc., while the node class is responsible for creating and assigning nodes.
The createlist class is responsible for the creation of the linked list, which is the main source of the problem. I wrote the debug statement in it, so you can run it and see the results for yourself

using namespace std;

class Node
{
public:
    int data;
    Node *next;
    Node()
    {
        next = nullptr;
    }
    Node(int data)
    {
        this->data = data;
    }
    Node(const Node &temp)
    {
        this->data = temp.data;
    }
};

class Link
{
public:
    Node *head;
    int length = 0;
    Link()
    {
        head = new Node();
    }
    ~Link()
    {   
        while (head != nullptr)
        {
            Node *p = head->next;
            free(head);
            head = p;
        }
    }
    void insert(const Node &cache)
    {
        Node *temp = new Node(cache);
        temp->next = head->next;
        head->next = temp;
        length++;
    }
};

void Creatlist(Link &link) 
{
    char cache;
    while (1)
    {
        cache = getchar();
        if (cache == '\n')
            break;
        link.insert(Node(cache - '0'));
        cout << cache << " ";
    }
    cout<<endl;
    Node *p = link.head->next;
    cout << "in the linklist:";
    while (p != nullptr)
    {
        cout << p->data << " ";
        p = p->next;
    }
}

int main()
{
    Link link;
    cout<<"inut numbers:"<<endl;
    Creatlist(link);
}```


  • Well, what did you observe when stepping through your code line by line with the debugger? – πάντα ῥεῖ Oct 16 '22 at 14:09
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason Oct 16 '22 at 14:11
  • @πάντα ῥεῖ First, the cache can tell me the order in which I typed, because the order in which the cache outputs must be the same as the order in which I typed, because getchar() reads from the cache from left to right. Second, at the end of the Creatlist class, I output the data from the beginning node to the end node of the linked list, but the final output results show that the two are different, because this program is relatively simple, so my debug statement only does these two simple things – Constantine Marx Oct 17 '22 at 15:16
  • @Jason Liam thk u,it help me a lot – Constantine Marx Oct 17 '22 at 15:18

1 Answers1

1

With the insert you inserted to the FRONT of the list. So you had "1", then "2->1" ... If you want to insert to the end, don't insert at the head, but hake a Node* tail in the class Link and an insert_end function as

//...
Node* temp;
void insert_end(const Node &cache){
    Node *temp = new Node(cache);
    tail->next=temp;
    tail=tail->next;
    length++;
}

Alsoin the constructor set tail=head

Botond Horváth
  • 929
  • 2
  • 15