CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2008
    Posts
    1

    Help with Linked List Project!

    I have to design and implement a new linked class called StudentList, to represent and manipulate the records of students enrolled in a course. The data field for each record in StudentList must contain:

    student name (string type)
    studentId (int) (from 0 to 10000)
    dept (string)
    major (string)

    In the program you should:
    1) add a student record to StudentList. Initially you should create an empty list and then add
    records to it. The records should be added in such a way that, they are placed serially, a student
    with lower studentId should be placed first in the list.

    2) Delete a student record from any location.

    3) Print out the entire StudentList as shown below. You should overloaded << operator for
    displaying the list.

    studentId Name Dept Major
    1234 Naveen ECE Computer Engineering
    2435 John Mechanical Aerospace Engineering
    10000 Annie Bio-Medical ---------

    4) Merge the records of two StudentList's into one list , again the records should be in increasing order of studentId.

    Your program should offer the menu to user:
    1. Add a student record to StudentList1.
    2. Delete a record from StudentList1.
    3. Print StudentList1.
    4. Add a student record to StudentList2.
    5. Delete a record from StudentList2.
    6. Print StudentList1.
    7. Merge StudentList1 and StudentList2 into a single list and display result.
    8. Exit.

    You should exit from the program only on choosing option 8, for all other options, after performing the task, user should be presented with the menu again. You should prompt user to select only from options 1 to 8, in case he presses anything other than that.


    I got everything to work except the merge function which just doesn't seem to work. If someone could please help me out with the merge part of it, I'd really appreciate it. Here's what I have so far. I commented out the merge portion of it. Thanks.


    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    
    
    class Student // Structure Student
    	{
    	public:
    		string sname; // Data Members
    	    string department;
    	    string major;
    	    int ID;
    		Student *nxt;
    	};
    class StudentList
    {
    	friend ostream &operator<<(ostream &, const StudentList&);
    public:
    	StudentList();//Default Constructor
    	void insert(int &, string , string , string );
    	void print();
    	void clearlist();
    	void removelist(int &);
    	void mergelist( Student, Student);
    	Student *start;
    	Student *current;
    	
    	
    };
    StudentList::StudentList()
    {
    	start = 0;
    	current = 0;
    }
    ostream &operator<<(ostream &output, const StudentList &a)
    {
    	Student*temp;
    	
    	temp = a.start;
         if (temp == 0)
            cout << "empty!" << endl;
         else
    		cout << "ID" << setw(9) << "sname" << setw(13) << "department" << setw(9) << "major" << endl;
           { while (temp != 0)
    
    		{  // Display decurrents for what temp points to
    			
              output << temp->ID << setw(9) << temp->sname << setw(13) << temp->department << setw(9) << temp->major << endl;    
    	      temp = temp->nxt;
    
    		}
    	   
    	   }
    	   return output;
    }
    void StudentList::insert(int &idx, string snamex, string departmentx, string majorx) // Add and Insert List
    {
    	Student *temp; Student *temp2 = 0; Student *temp3 = 0; // temp pointers
    	
    	temp = new Student;
    	(temp->sname)=snamex;
    	(temp->major)=majorx;
    	(temp->department)=departmentx;
    	(temp->ID) = idx;
    	(temp->nxt) = NULL;
    
    	if (start == 0 || start->ID>=temp->ID) 
    	{
    		temp->nxt = start; 
    		start = temp;
    		current = start;
    	}
    	else
    	{
    		temp2 = start;
    		while(temp2 != 0 && (temp2->ID < temp->ID))
    		{
    			temp3 = temp2;
    			temp2 = temp2->nxt;
    		}
    		//While ends with temp2 having an ID >= temp and temp3 having an ID < temp
    		if (temp3 != 0)
    		 temp3->nxt = temp;
    
    	if (temp2 != 0)
    		 temp->nxt = temp2;
    
    		
    	}
    }
    
    	void StudentList::removelist(int &) 
    	{
    		long int idx;
    		Student *temp;
    		Student *temp2 = 0;
    
    		cout<<"Enter the ID# that you want to get deleted: ";
    		cin>>idx;
    
    		temp = start;
    		while (temp->ID != idx)
    		{
    			temp2 = temp;
    			temp = temp->nxt;
    		}
    		if(temp != NULL)
    		{
    			if (temp == start)
    			{
    				start = start->nxt;
    				delete(temp);
    			}
    			else
    			{
    				temp2->nxt=temp->nxt;
    				delete(temp);
    			}
    		}
    	}
    
    	void StudentList::clearlist() // delete list
    	{
    		while(start != 0)
    			removelist(start->ID);
    	}
    
    
    
    /*void StudentList::mergelist(Student stud1,Student stud2) 
    	// (There seems to be syntex erros in this..)
    	{
    		Student *temp;
    		
    
    		for(temp = stud1->start; temp != 0 ; temp = temp->nxt)
    			insert(temp->ID,temp->sname,temp->department,temp->major);
    		for(temp = stud2->start; temp != 0 ; temp = temp->nxt)
    			insert(temp->ID,temp->sname,temp->department,temp->major);
    	}
    	
    */
    
    
    
    
    
    
    int main()
    {
    
    int choice;
    StudentList student;
    StudentList student1;
    StudentList student2;
    
    do
    {
         	cout
    		    <<	"1: Add a student to List1\n"
        		<<  "2: Delete student from StudentList1\n"
    			<<  "3: Print StudentList1\n" 
    			<<  "4: Add a student to StudentList2\n"
    			<<	"5: Delete a record from StudentList2\n"
    			<<  "6: Print StudentList2\n" 
    			<<  "7: Merge StudentList1 and StudentList2 into a single list and display result\n"
    			<<  "8: Exit\n"
    			<< endl;
    		cin >> choice;
    		cout << endl;
            
    				int idx;
    				string snamex, departmentx, majorx;
                switch (choice)
                {
    			case 1:
    				cout << "Input ID : ";
    				cin >> idx;
    				cout << "Input sname : ";
    				cin >> snamex;
    				cout << "Input department : ";
    				cin >> departmentx;
    				cout << "Input major : ";
    				cin >> majorx;
    				student.insert(idx, snamex, departmentx, majorx);
    				break;
    			case 2:
    				student.removelist(idx);
    				break;
    			case 3:
    				cout << student;
    				break;
    			case 4:
    				cout << "Input ID : ";
    				cin >> idx;
    				cout << "Input sname : ";
    				cin >> snamex;
    				cout << "Input department : ";
    				cin >> departmentx;
    				cout << "Input major : ";
    				cin >> majorx;
    				student1.insert(idx, snamex, departmentx, majorx);
    				break;
    			case 5:
    				student1.removelist(idx);
    				break;
    			case 6:
    				cout << student1;
    				break;
    			case 7:
    				//student2.clearlist();
    				//student2.mergelist(student,student1);
    			case 8:
    				break;
    			default:
    			   cout << "Wrong option!" << endl;
    				break;         
                }
           
    	}while(choice!=8);
    	return 0;
    		}

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with Linked List Project!

    Well, you seem to be using the wrong type for your parameters. Unless you're doing some freaky genetic experiments, merging two students doesn't seem to make sense.

    It's also worth noting that, *if* you're not trying to keep both the original lists intact, there's no need to do that much work to merge two lists. Just point the end of one at the start of the next one. Of course, in that case it would make more sense for your merge function to take only one argument (the list being appended) and modify the object being operated on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured