Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:17:28+00:00 2026-06-17T21:17:28+00:00

Please consider this C++ code for creation and traversing a linked list (number of

  • 0

Please consider this C++ code for creation and traversing a linked list (number of nodes to be decided by user, not programmer)

#include <iostream>
using namespace std;
class Node
{ 
 public:
 int data; Node* next;
 Node(int d, Node* j): data(d),next(j) {cout << "Constructor\n";}
};

int main()
{int n; Node* p; Node* q = 0;

while(cin >> n) 
{ p = new Node(n,q);
  q = p;}

for(;p->next; p=p->next)
    cout << p->data << "->";
    cout << p-> data << "->*\n";
return 0;}

The code above works perfectly, and can be terminated by the user using Ctrl+D followed by Enter. However if we replace while(cin >> n) with while (true) using cin >> n; inside the loop, as shown here

    while(true) 
    { cin >> n;
    p = new Node(n,q); q = p;}

then upon the user attempting to terminate, the loop just goes on automatically creating new Nodes!! Why??
Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:17:29+00:00Added an answer on June 17, 2026 at 9:17 pm

    It keeps going because the loop condition is true, making it an infinite loop. If you want to break out of the infinite loop, you can use a break statement, e.g.

    while (true) {
        if (!(cin >> n)) {
            break; // exits the loop
        }
        p = new Node(n,q);
        q = p;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Please consider this code. Is it using Circular Reference? If not why am I
please consider this code : 1-... 2-{ 3-... 4-SqlConnection cn=new SqlConnection(); 5-... 6-} if
please consider this code : 1)public static class MyClass 2){ 3) public static DateTime
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
sorry if this is a newB question, please conider the following code: #include <boost/multi_index_container.hpp>
please consider this table: PK_Id Number Year Month Value ------------------------------------------------------------------------- 1 1 2000 5
Please consider this site . I'm having trouble stretching the submenu list items in
please consider this code: import xml.etree.ElementTree as ET import urllib XML_response = urllib.urlopen('http://www.navlost.eu/aero/metar/?icao=LWSK&dt0=2011-05-03+12%3A00%3A00&c=1&rt=metar').read() tree
Consider this code: #include <iostream> int main() { int iTemp = 0; iTemp +=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.