Thread: Quick LinkList Help!!adding at the End

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Question 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

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    OK, what you could do is this.

    Firstly change the function to this:

    void Student::addStudent (char *prName,char *prID,char *prEmail,char *prPhone,char *prMajor,Student **root)
    Note the '**'

    Then change these lines

    ptrNewStudent->setNext(root);
    root = ptrNewStudent;

    to this

    ptrNewStudent->setNext(*root);
    *root = ptrNewStudent;

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Tried it Still same

    Same results

    ------------------
    Don't know what is wrong

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Question do you want me to attach student.dat

    do you want me to attach student.dat

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    ok, i'm not sure what you want to do.

    Is there addStudent function working? if not, what's it doing/not doing?

    Do you just want to add to the end of the list?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    compare both of this

    Here is my data file, and the result..

    You will see that the first record in the file is last in the LinkList i want it to apper first.

    Thanks
    [comment]

    Justin Case 123456789 [email protected] 317-222-1234 Biology
    Susie Creamcheese 987654321 [email protected] 317-999-9876 Computer Science
    Sue Ellen 234567890 [email protected] 777-878-8234 Chemistry
    Joe Murphy 231892323 [email protected] 365-213-9898 Chemistry
    Toof A Bone 888349876 [email protected] 317-234-3212 Biology
    Donalds Butts 984342912 [email protected] 317-909-1290 Computer Science
    Phil Hipps 783673434 [email protected] 317-934-9090 Physics
    John Spine 786349000 [email protected] 317-230-0909 Computer Science
    Lemme Goh 214889800 [email protected] 317-989-0999 Computer Science
    Douglas Pepper 317581090 [email protected] 317-889-1090 Computer Science

    ------------------------------------------------------------------------------------------------------------------------
    and here are results

    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

    [comment]

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> i want it to apper first.

    OK.

    What you have to do is this:

    The first node is simple enough. After assign the first node to the root, assign NULL to the first node's next member.

    To add another node, loop through the list until the pointer's next member is NULL.

    then assign the new node to that pointer and assign NULL to the node's next member, eg.
    Code:
    Student *temp;
    temp = *root;
    if(temp)
    {    
         while(temp->next != NULL)
              temp = ptrNewStudent->next;
         temp->next = ptrNewStudent;
    }
    else
         *root = ptrNewStudent;
    using the previous code

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    68
    I am having some problem fixing it in my code... Can you replace the code for me

    Thanks

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Change this,
    Code:
    if(root == NULL)
    {
    	root = ptrNewStudent;
    }	
    else
    {
    	ptrNewStudent->setNext(root);
    	root = ptrNewStudent;
    }
    to this
    Code:
    Student *temp;
    temp = *root;
    if(temp)
    {    
         while(temp->next != NULL)
              temp = ptrNewStudent->next;
         temp->next = ptrNewStudent;
         ptrNewStudent->next = NULL;
    }
    else
    {
         *root = ptrNewStudent;
         ptrNewStudent->next = NULL;
    }
    just declare temp in the beginning when you declare ptrNewStudent

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    68
    It is still giving me memory violation error......
    Can you please complie and attach the file

    Thanks
    Pratik

  11. #11
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> Here is my data file, and the result..

    Where?

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    68
    Her is my Data file

  13. #13
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I had to make this program far harder than what it could have been because of the way you've coded it.

    Anyway, here's what you wanted:

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    Thumbs up Thanks

    Thank You Very Much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  3. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM