Quick LinkList Help!!adding at the End
Hi everyone,
I am having a little problem figuring out how to add element at the end of the LinkList. Here is my program that adds element at front of the Linklist. The Function that add the Element to the front is addStudent() in Student class. Can any one fix these for me.....
Thanks
Code:
#include<iostream.h>
#include<cstring>
#include<cstdlib>
#include<fstream.h>
#include <iomanip>
#define Channel 200
#define Y true
#define N false
#define True 0 /* Allies for Zero*/
#define False 1 /* Allies for ONE*/
class Student;
class Person{
protected:
char *Name;
char *ID;
char *Email;
char *Phone;
public:
Person();
~Person();
};
class Student : public Person{
private:
char *Major;
Student *next;
public:
Student();
void setName(char *);
void setID(char *);
void setPhone(char *);
void setEmail(char *);
void setMajor(char *);
void setNext(Student *);
char* getName()const{return Name;}
char* getID()const{return ID;}
char* getPhone()const{return Phone;}
char* getEmail()const{return Email;}
char* getMajor()const{return Major;}
Student *getNext()const{return next;}
void addStudent (char *,char *,char *,char *,char *,Student *&);
~Student();
};
/*Opening all the file to run an make linklist after file processing*/
void funOpenFile(Student *&);
/*functions used for processing Student.dat file, and make a linklist*/
void StudentGetData (fstream &,Student *&);
void Sdatastore(int ,char *,Student *&);
void showStudent(Student *);
Person::Person()
{
Name = new char[30];
ID = new char[10];
Email = new char[30];
Phone = new char[15];
}
Person::~Person()
{
delete[] Name;
delete[] ID;
delete[] Email;
delete[] Phone;
}
Student::Student():Person()
{
Major = new char[30];
next = NULL;
}
Student::~Student()
{
delete[] Major;
}
int main()
{
Student *ptrStartStud = NULL;
funOpenFile(ptrStartStud);
showStudent(ptrStartStud);//Enable to check nothing is wrong while loading the LinkList
return 0;
}
void funOpenFile (Student *&rootStudent)
{
fstream StudentFileInPtr;
StudentFileInPtr.open("Student.dat",ios::in);
if(StudentFileInPtr.fail())
{
cerr<<"Error Opening The File"<<endl;
}
else
{
StudentGetData (StudentFileInPtr,rootStudent);
}
}
void StudentGetData (fstream &ReadFileInPtr,Student *&topStudent)
{
char *chtokenPtr,szPharseFile[Channel];
int ncounter = 0;
while(!(ReadFileInPtr.getline(szPharseFile,200,'\n')).eof())
{
chtokenPtr = strtok(szPharseFile,"\t");
while(chtokenPtr != NULL)
{
ncounter++;
if(ncounter == 6 )
{
ncounter = 1;
}
Sdatastore(ncounter,chtokenPtr,topStudent);
chtokenPtr = strtok(NULL,"\t\n");
}
}
}
void Sdatastore(int ncounter,char *prtokenPtr,Student *&topStudent)
{
static char szName[30],szID[9],szEmail[30],szPhone[13],szMajor[30];
enum Status{stage1 = 1,stage2,stage3,stage4,stage5};
Student NewPtrI;
switch(ncounter)
{
case stage1:
{
strcpy(szName,prtokenPtr);
break;
}
case stage2:
{
strcpy(szID,prtokenPtr);
break;
}
case stage3:
{
strcpy(szEmail,prtokenPtr);
break;
}
case stage4:
{
strcpy(szPhone,prtokenPtr);
break;
}
case stage5:
{
strcpy(szMajor,prtokenPtr);
NewPtrI.addStudent(szName,szID,szEmail,szPhone,szMajor,topStudent);
break;
}
}
}
void Student::setName(char *szName)
{
strcpy(Name,szName);
}
void Student::setID(char *szID)
{
strcpy(ID,szID);
}
void Student::setEmail(char *szEmail)
{
strcpy(Email,szEmail);
}
void Student::setPhone(char *szPhone)
{
strcpy(Phone,szPhone);
}
void Student::setMajor(char *szMajor)
{
strcpy(Major,szMajor);
}
void Student::setNext(Student *setnext)
{
next = setnext;
}
void Student::addStudent (char *prName,char *prID,char *prEmail,char *prPhone,char *prMajor,Student *&root)
{
Student *ptrNewStudent;
ptrNewStudent = new Student;
if(ptrNewStudent != NULL)
{
ptrNewStudent->Name = new char[strlen(prName)+1];
if(ptrNewStudent->Name != NULL)
{
ptrNewStudent->setName(prName);
}
ptrNewStudent->ID = new char[strlen(prID)+1];
if(ptrNewStudent->ID != NULL)
{
ptrNewStudent->setID(prID);
}
ptrNewStudent->Email = new char[strlen(prEmail)+1];
if(ptrNewStudent->Email != NULL)
{
ptrNewStudent->setEmail(prEmail);
}
ptrNewStudent->Phone = new char[strlen(prPhone)+1];
if(ptrNewStudent->Phone != NULL)
{
ptrNewStudent->setPhone(prPhone);
}
ptrNewStudent->Major = new char[strlen(prMajor)+1];
if(ptrNewStudent->Major != NULL)
{
ptrNewStudent->setMajor(prMajor);
}
if(root == NULL)
{
root = ptrNewStudent;
}
else
{
ptrNewStudent->setNext(root);
root = ptrNewStudent;
}
}
else
{
cerr<<"Memory Allocation Problem"<<endl;
}
}
void showStudent(Student *root)
{
Student *newStudentPtr = root;
while(newStudentPtr != NULL)
{
cout<<newStudentPtr->getName()<<"\t"<<newStudentPtr->getID()<<"\t"<<newStudentPtr->getEmail()<<"\t"<<newStudentPtr->getPhone()<<"\t"<<newStudentPtr->getMajor()<<endl;
newStudentPtr = newStudentPtr->getNext();
}
}
----------------------OutPut--------------------------
Douglas Pepper 317581090 [email protected] 317-889-1090 Computer
Science
Lemme Goh 214889800 [email protected] 317-989-0999 Computer Science
John Spine 786349000 [email protected] 317-230-0909 Computer
Science
Phil Hipps 783673434 [email protected] 317-934-9090 Physics
Donalds Butts 984342912 [email protected] 317-909-1290 Computer
Science
Toof A Bone 888349876 [email protected] 317-234-3212 Biology
Joe Murphy 231892323 [email protected] 365-213-9898 Chemistr
y
Sue Ellen 234567890 [email protected] 777-878-8234 Chemistry
Susie Creamcheese 987654321 [email protected] 317-999-9876
Computer Science
Justin Case 123456789 [email protected] 317-222-1234 Biology
do you want me to attach student.dat
do you want me to attach student.dat