Question

I am having some trouble trying to create a linked list from a text file input...

I am having some trouble trying to create a linked list from a text file input from user

The three lines
respectively represent a long integer ID of a person, the name of the person, and an
integer ranging from 0 to 5 indicating the threat level posed by a person.

389114
Paul Bunion
5
399012
John Doe
0
685015
Johnny Appleseed
3
179318
Tom Sawyer
2
284139
Ebenezer Scrooge
5

The full connection is: Paul Bunion -> John Doe ->Johnny Appleseed -> Tom Sawyer -> Ebenezer Scrooge.

The task is to read the file content, construct a singly linked list to store the records in a

sequence they appear in the file, and allow flexibility so that users can

perform different operations.

Homework Answers

Answer #1

C ++ CODE:

#include <bits/stdc++.h>
using namespace std;

class node
{
public:
   string name = "";
   long int ID;
   int t_level;
   node* next = NULL;
};

void insert(long int i, int t_level, string name, node* head)
{
   node* tmp = head;
   if(tmp->name == "")
   {
       tmp->ID = i;tmp->t_level = t_level; tmp->name = name; tmp->next = NULL;
   }
   else
   {
       while(tmp->next != NULL)
       {
           tmp = tmp->next;
       }
       node * newnode = new node();
       newnode->ID = i;
       newnode->t_level = t_level;
       newnode->name = name;
       newnode->next = NULL;
       tmp->next = newnode;
   }
}

void display(node* head)
{
   node* tmp = head;
   if(tmp == NULL)
   {
       cout << "Empty list!" << endl;
   }
   while(tmp->next != NULL)
   {
       cout << "( " << tmp->name << ", ID = " << tmp->ID << " )" << " --> ";
       tmp = tmp->next;
   }
   cout << "( " << tmp->name << ", ID = " << tmp->ID << " )" << endl;
}

int main()
{
   string line;
   node* head = new node();
   ifstream myfile ("input2.txt");
   if (myfile.is_open())
   {
   while ( getline (myfile,line) )
   {
       long int ID = atol(line.c_str());
       getline (myfile,line);
       string name = line;
       getline (myfile,line);
       int t_level = atoi(line.c_str());
       insert(ID,t_level,name,head);
   }
   myfile.close();
   }
   else
   {
   cout << "Unable to open input file" << endl;
   exit(1);
   }      
   display(head);
return 0;
}  

input2.txt

389114
Paul Bunion
5
399012
John Doe
0
685015
Johnny Appleseed
3
179318
Tom Sawyer
2
284139
Ebenezer Scrooge
5

Sample Output:

( Paul Bunion, ID = 389114 ) --> ( John Doe, ID = 399012 ) --> ( Johnny Appleseed, ID = 685015 ) --> ( Tom Sawyer, ID = 179318 ) --> ( Ebenezer Scrooge, ID = 284139 )

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
Please linked both files. For this assignment you need to create a ToDo list using Javascript,...
Please linked both files. For this assignment you need to create a ToDo list using Javascript, along with HTML and CSS. Begin by creating a HTML page called todo.html. Then create a Javascript file called todo.js and link it in to the HTML page using a script tag. All Javascript for the assignment must be in the separate file. (For CSS, feel free to include styles in a style block at the top of the HTML page, or to link...
I have trouble in my code with - Create and append the fancySheet link element to...
I have trouble in my code with - Create and append the fancySheet link element to the document head - Create and append figBox element to element with id box - Populate the figure box with preview images of the five fancy style sheets I don't understand what went wrong. New Perspectives HMTL5, CSS3, and JavaScript T12 Case Problem 1: New Accents Photography JavaScript File Event Listener Go to the na_styler.js file in your editor. Add an event listener that...