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
JAVA ANSWER IN JAVA PLEASE.. ReadFile Create a linked list from an input file (input.txt) that...
JAVA ANSWER IN JAVA PLEASE.. ReadFile Create a linked list from an input file (input.txt) that contains an even number of first names. The number of items in the file is unknown. SplitMerge Create a split function that divides the newly created linked list into two equal sublists: myList1 and myList2. For example, originally you would point to (John, Jack, Jill, Jim). After the split, myList1 would point to john and jack and myList2 would point to jill and Jim....
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...
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...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read...
C++ pls finish code! Lab: Singly-Linked List (Student class) Review and finish the following files (read code and all comments carefully): Student.h StudentList.h StudentList.cpp main.cpp This program: Creates a sorted linked list (student name and gpa) . The list is sorted in ascending order by name. Displays the list Read and understand this program, then do the following: finish writing Student.h and other files fix errors in StudentList.cpp Student.h: #ifndef STUDENT_H #define STUDENT_H //using namespace std; //<==== This statement //...