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.

#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;
		}

I would actually take the nxt data attribute out of the Student class and put it in the StudentList class. It gives you far more options and is generally the way things are done What if you want a student to be in two or more separate lists? The "next" student could be different in different lists. Currently you can't do that. If you change it and put nxt in StudentList instead of Student, you'll be able to do that.

This is a prime candidate for the Merge Sort. You're merging two ordered lists, so you'll need to write the second half of a Merge Sort (the "Merge" part of it). Since the two lists are already sorted when you merge them, you don't have to worry about the first part of the Merge Sort (splitting a list into two and then recursively calling the Merge Sort on the individual lists till THEY are sorted), so you won't need to use recursion. You just need to do the Merge part.

Your job is even easier since you've presumably already written "insert" and "delete" functions. So you have two linked lists. Create a third linked list, initially empty. Set "current" to the front of the two original lists. Compare the "current" elements of the original two linked lists. Pick the Student with the smaller studentID. Insert that student into the third linked list. If you are deleting the original linked lists when you merge, delete that "current" element from the original list. If not just change its "current" pointer to the next node in the list. Keep the "current" pointer of the list that had the LARGER studentID where it is. Continually do this until you've gotten to the end of one of the original lists. Then put the remaining lists at the end of the new third list(no need for any more comparisons after that).

[EDIT]
Just reread my post. Don't put nxt in StudentList. I was thinking of having a class called StudentNode. Sometimes I make a class called Student, then a class called StudentNode, where Student would have these attributes:

string sname; // Data Members
string department;
string major;
int ID;

and StudentNode would have these:

Student* student;
Student* next;

and then StudentList would have these attributes:

StudentNode* head;
StudentNode* current;

But I hereby withdraw my earlier advice to put nxt in StudentList. If you don't want to have a StudentNode class and just stick nxt in Student, that's cool too. I just like making them separate. That way I can use my Student class completely independently of any linked list if I want to and I can create any number of different options of having several different lists that use Student.
[/EDIT]

Revision 2 (sorry):

StudentNode would contain these attributes:

Student* student;
StudentNode* next; // not Student* next;

You could also make student type Student rather than type Student*. I've used both. Depends on your needs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.