2 Replies - 674 Views - Last Post: 03 July 2008 - 07:33 AM Rate Topic: -----

#1 ac3this  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-July 08

Help With Linked List Project!

Posted 02 July 2008 - 04:28 PM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Help With Linked List Project!

#2 tortillaboy05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 08

Re: Help With Linked List Project!

Posted 02 July 2008 - 07:39 PM

What kind of error do you get? What is your compiler telling you is wrong?

Looks like your trying to insert a new node when your merging I don't think that is what the assignment is asking.
Use a loop to run through the first list, when it hits the last node make the next pointer point to the first node of the second list. Also, I think it would be easier to make a head pointer to each list, I didn't see one and pass it to the method.

*void StudentList::mergelist(Student *stud1,Student *stud2)
// (There seems to be syntex erros in this..)
{
Student *temp1 = stud1;

while(temp1->nxt !=null) //finds the end of the first list
temp1 = temp1 -> nxt;
temp1->nxt = stud2; //points the last node of the first list do the start of the second list


}

In main declare the head pointers.

Student *head1, *head2;

head1 = &student1;
head2 = &student2;

function call.

mergelist(head1, head2)

method definition

void mergelist(Student *, Student *);

hope this helps.


edit: I noticed what your problem is. This line:

insert(temp->ID,temp->sname,temp->department,temp->major);

you didn't specify which insert you want to use, should be:

stud1.insert(......)
stud2.insert(......) or whichever you meant to use.

This post has been edited by tortillaboy05: 02 July 2008 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#3 kapax  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 65
  • Joined: 02-July 08

Re: Help With Linked List Project!

Posted 03 July 2008 - 07:33 AM

I think it would have been better to make a Student class a friend of a StudentList class, instead of making all members in the Student class public. This way you would protect the members and still would get the access from the StudentList class.

Please, let me know if I am wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1