C++ Question. how to store the following record information in a linked list . E
ID: 3756294 • Letter: C
Question
C++ Question.
how to store the following record information in a linked list . Each part in a node.
28931 Edwin Porter 12 24 1999 1 42931 Secretary
id first last MM DD YYYY YearsWorked Salary Position.
There will not be that much of space between them in the text file. So the text file will contain information as such:
18513 Lorene Cox 01 11 1978 25 148200 Manager
28931 Edwin Porter 12 24 1999 1 42931 Secretary
98753 Van Goodman 03 15 1998 2 50181 Janitor
** My question is how can i store each part in a linked list and make sure the the date is correctly stored as MM DD YYYY with the spaces from input file.
Explanation / Answer
Please find a sample code below.
CODE
======================
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include <fstream>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int id;
string firstName, lastName;
int mm, dd, yyyy, yearsWorked;
double Salary;
string Position;
struct node *next;
}*start;
/*
* Class Declaration
*/
class single_llist
{
public:
node* create_node(int, string, string, int, int, int, int, double, string);
void insert_begin(int, string, string, int, int, int, int, double, string);
void insert_pos(int, string, string, int, int, int, int, double, string);
void insert_last(int, string, string, int, int, int, int, double, string);
void display();
single_llist()
{
start = NULL;
}
};
/*
* Creating Node
*/
node *single_llist::create_node(int ID, string fn, string ln, int d, int m, int y, int years, double sa, string pos)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->id = ID;
temp->firstName = fn;
temp->lastName = ln;
temp->dd = d;
temp->mm = m;
temp->yyyy = y;
temp->yearsWorked = years;
temp->Salary = sa;
temp->Position = pos;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::insert_begin(int ID, string fn, string ln, int d, int m, int y, int years, double s, string pos)
{
struct node *temp, *p;
temp = create_node(ID, fn, ln, d, m, y, years, s, pos);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at last
*/
void single_llist::insert_last(int ID, string fn, string ln, int d, int m, int y, int years, double sa, string pos)
{
struct node *temp, *s;
temp = create_node(ID, fn, ln, d, m, y, years, sa, pos);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}
/*
* Insertion of node at a given position
*/
void single_llist::insert_pos(int ID, string fn, string ln, int d, int m, int y, int years, double sa, string p)
{
struct node *temp, *s, *ptr;
int counter = 0;
int pos;
temp = create_node(ID, fn, ln, d, m, y, years, sa, p);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
/*
* Display Elements of a link list
*/
void single_llist::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout << temp->id << endl;
cout << temp->firstName << endl;
cout << temp->lastName << endl;
cout << temp->dd << "/" << temp->mm << "/" << temp->yyyy << endl;
cout << temp->yearsWorked << endl;
cout << temp->Salary << endl;
cout << temp->Position << endl;
temp = temp->next;
}
cout<<"NULL"<<endl;
}
int main() {
int ID;
string fn, ln;
int d, m, y, years;
double s;
string pos;
ifstream inFile;
single_llist list;
inFile.open("input.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> ID >>fn >> ln >> d >> m >> y >> years >> s >> pos) {
list.insert_begin(ID, fn, ln, d, m, y, years, s, pos);
}
inFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.