-1

When I run this code and input data into the linked list like this :

23 45 55

The output will be:

45 55

The first data is lost!!!!

What's the problem? How can I change the code to get the correct output?

#include<iostream>
using namespace std;

struct node
{
    int data;
    node* next;
};

int main()
{
    char ch;

    node* n;
    node* t;
    node* h;

    n= new node();

    t=n;
    h=n;

    cout<<"Linked list is created.Now You can write data into it."<<endl;
    cout<<"Enter data into the linked list:";
    cin>>t->data;
    cout<<"do you want to enter more data into the linked list?(y/n):";
    cin>>ch;
    if( ch =='y'|| ch =='Y')
    do
    {
        n=new node();
        t->next=n;
        cout<<"Enter data into the linked list:";
        cin>>t->data;
        t=t->next;
        cout<<"do you want to enter more data into the linked list?(y/n):";
        cin>>ch;

    }while(ch=='Y'||ch=='y');
    
    t->next=NULL;
    cout<<endl;
    cout<<endl;
    cout<<endl;
    cout<<"================================="<<endl;
    cout<<endl;
    cout<<endl;
    cout<<endl;
    
    cout<<"Do you want to print data on the linked list?(y/n):";
    cin>>ch;
    if(ch=='y'||ch=='Y')
    {
        t=h;
        while(t->next != NULL)
        {
            cout<<t->data<<endl;
            t=t->next;
        }
    }
}
4
  • 3
    Move t=t->next; before cin>>t->data;. Then fix the printing loop accordingly. And use a debugger, please... Commented Sep 28, 2022 at 17:45
  • thnks.... im new to programming ..... thnsk for ur help. BTW what do u mean by use a debbugger. there was no compile time errors. Commented Sep 28, 2022 at 18:08
  • @MajeedI A debugger is for runtime errors and logic errors (like yours) Commented Sep 28, 2022 at 18:47
  • @MajeedI And that is exactly the situation in which you use a debugger. BTW, the delete statements are missing. Commented Sep 28, 2022 at 18:57

1 Answer 1

-2

The mistake is already pointed out in the comments. Let me just add that overly verbose code makes mistakes of this kind hard to spot. Linked list creation (without insertions or other complex operations) is a simple process that can (and should) be expressed in simple and concise code.

Using struct node from the question, this reads integers until the end of input and then prints them out:

int main() {
  int data;
  node *list;
  node **end{&list};

  // build the list
  while (std::cin >> data) end = &(*end = new node{.data{data}})->next;
  *end = nullptr;

  // print the list
  for (const node *n{list}; n; n = n->next) std::cout << n->data << '\n';

  // delete the list
  while (list) {
    const node *const previous{list};
    list = list->next;
    delete previous;
  }
}

It becomes even simpler when you don’t mind that the list has a reverse order. Then you don’t need to point at its end:

int main() {
  int data;
  node *list{};

  // build the list
  while (std::cin >> data) list = new node{.data{data}, .next{list}};

  // print the list
  for (const node *n{list}; n; n = n->next) std::cout << n->data << '\n';

  // delete the list
  while (list) {
    const node *const previous{list};
    list = list->next;
    delete previous;
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I can't agree with the "concise code" thing. This statement: while (std::cin >> data) end = &(*end = new node{.data{data}})->next; is practically an entry for an obfuscated C++ competition. It would be rejected by most code review practice and makes it impossible to catch allocation errors even if one would like to. Not having brackets around while statements calls for errors later, and squashing everything on one line makes it impossible to debug. I'd say that this is a major no no.
Thank you for your personal opinion, about which I could not care less. 😆 Given that the code in the question leaks memory (which is benign in main() but bad for code reusability) and (also) lacks error handling, the absence of std::bad_alloc handling in my snippet does not look concerning to me. As for more blocks and more variables instead of nested expressions — sure, to each their own. This was meant to be a short code snippet. Also, nothing makes it impossible to catch allocation errors. Add a try block, catch all you like — but that goes far beyond the scope of the question.
I am just introduced to programming. All this code is too much for me. I just needed a simple work around for now at my beginner stage. The comment give by @costantino was quite simple and precise for my current needs . Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.