简体   繁体   中英

Create a linked list from space separated input

I am trying to make a linked list from space separated integer input.

Input:

  1. Number of nodes
  2. Space separated input

int main()
{
    int n;
    cout<<"Enter number of nodes";
    cin>>n;
    cout<<"\nEnter data"<<endl;
    int temp;
    lNode *head = NULL;
    while(cin>>temp)
    {
        CreateLinkedList(&head,temp);
    }
    PrintLinkedList(head);

    return 0;
}

Here I am not getting how to limit the user input to the number of nodes which he has given as first input. Is there any other way to get user input?

You can ask the input as a string:

string line;
getline(cin, line);

Then you can separate the entered numbers in the line using stringstream so you should include the sstream library (eg. #include <sstream> ):

stringstream ss(line);
int number;

while(ss >> number) {
    ... do whatever you want to do with the number here ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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