here is the full code
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
struct Employee {
int UniqueID;
string FirstName,LastName, EmailAddress;
Employee* next;
Employee* previous;
};
struct Company {
Employee* head, * tail;
};
Company* InitializeDoubly() {
Company* c = new Company;
c->head = NULL;
c->tail = NULL;
return c;
}
bool isEmpty(Company *c){
return(c->head == NULL);
}
void InsertAtHeadDoubly(Company* c, Employee e) {
Employee* tmp = new Employee;
if (isEmpty(c)) {
tmp->UniqueID = e.UniqueID;
tmp->FirstName = e.FirstName;
tmp->LastName = e.LastName;
tmp->EmailAddress = e.EmailAddress;
tmp->next = NULL;
tmp->previous = NULL;
c->head = tmp;
c->tail = tmp;
}
else {
tmp->UniqueID = e.UniqueID;
tmp->FirstName = e.FirstName;
tmp->LastName = e.LastName;
tmp->EmailAddress = e.EmailAddress;
tmp->previous = NULL;
tmp->next = c->head;
c->head->previous = tmp;
c->head = tmp;
}
}
void DeleteFromDoublyHead(Company* c) {
if (isEmpty(c)) {
cout << "List is empty";
return;
}
Employee* cur = c->head->next;
delete c->head;
c->head = cur;
c->head->previous = NULL;
}
void tail2head(Company* c) {
Employee* cur = c->tail;
if (isEmpty(c)) {
cout << "LIST IS EMPTY";
return;
}
while (cur != NULL) {
cout << cur->UniqueID << " " << cur->FirstName <<" " << cur->LastName <<" " << cur->EmailAddress << endl;
cur = cur->previous;
}
}
void parsefile(fstream& f,Company *c) {
Employee efile;
while (f) {
getline(f, efile.FirstName,'\t');
getline(f, efile.LastName, '\t');
getline(f, efile.EmailAddress, '\t');
f >> efile.UniqueID;
InsertAtHeadDoubly(c, efile);
f.ignore(INT_MAX, '\n');
}
DeleteFromDoublyHead(c);
f.close();
}
void addemployee(fstream& f, Employee e1 ,Company* c) {
int id = 8;
f <<e1.FirstName << '\t' << e1.LastName << '\t' << e1.EmailAddress << '\t' << id;
f.close();
}
int main()
{
fstream f("file1.txt", ios::in | ios::out | ios::app);
Company* company1 = InitializeDoubly();
parsefile(f, company1);
tail2head(company1);
Employee e1;
e1.FirstName = 'ali';
e1.LastName = 'kilo';
e1.EmailAddress ="ebla@gmail.com";
addemployee(f, e1, company1);
tail2head(company1);
return 0;
}