2 Replies - 1162 Views - Last Post: 28 October 2006 - 12:44 PM Rate Topic: -----

#1 jsenseman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 15-July 06

Need Help With Linked Lists for container

Posted 27 October 2006 - 08:10 PM

Hey everyone, I got my HW returned today and my teacher told me my container is inefeccient and I should used a linked list instead. Let me post my container.

class Employees{
  private:
	 Employee* allEmployees;
	 int EmployeeCount;
	 int EmployeeSize;
	 void qs_lname(int left, int right);
  public:
	 //defualt,w/initializer List
	 Employees(void) : allEmployees(NULL), EmployeeCount(0), EmployeeSize(0)
	 {allEmployees= new Employee[EmployeeSize];}
	 Employees(Employees& e) : allEmployees(NULL), EmployeeCount(e.EmployeeCount), EmployeeSize(e.EmployeeSize)
	 { 
	  allEmployees= new Employee[e.EmployeeSize];
	  for(int i=0;i<EmployeeSize;i++)
		 allEmployees[i]=e.allEmployees[i];
	 }
	 ~Employees(void){delete [] allEmployees;}
	 //change state
	 void add(const Employee& e);
	 void add(const char*last="",const char* first="",float pr=0.0,float hrs=0.0,float deff=0.0);
	 void remove(const Employee &e);
	 void remove(const char * last);
	 float search(const char* last);
	 float search(const Employee &e); // overloading
	 void quicksort(){qs_lname(0,EmployeeCount-1);}
	 void printPayrollReport(void);
	 //gets
	 const int getEmployeeCount(void)const{return EmployeeCount;}
};


The assignment said we should use
Employee * allEmployees[MAX];

But I tried arguing with my professor, saying isnt the whole point of a container to stores things dynamically and not a fixed size, well I think this made him angry and now he is expecting me to use a linked list, (which he hasnt covered in class yet), here is my add method

void Employees::add(const Employee &e)
{
  int found=search(e.getLastName());
  if (found >= 0)
	 allEmployees[found] = e;   // update the entry found
  else
  {   if (EmployeeCount < EmployeeSize)
		 allEmployees[EmployeeCount++] = e;
	  else
	  {
		 EmployeeSize += 32;//<--- this is what he is complaining about
		 Employee* all = new Employee[EmployeeSize];
		 for (int i = 0; i <  EmployeeCount; ++i)
			all[i] = allEmployees[i];
		 delete [] allEmployees;
		 allEmployees = all;
		 allEmployees[EmployeeCount++] = e;
   }
 }
}



He is saying it is ineffectient to reallocate , how can i use a linked list to solve this ?

thanks
JS

Is This A Good Question/Topic? 0
  • +

Replies To: Need Help With Linked Lists for container

#2 CBart21  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 28-October 06

Re: Need Help With Linked Lists for container

Posted 28 October 2006 - 12:07 PM

I don't understand your question. A container holds something, It doesn't HAVE to be dynamic, unless the objects your holding in your container IS NOT of fixed size. When you hold different things in a container you will want to use pointers to point to the objects so that "slicing" doesn't occur.

As far as linked lists go, I had to implement my own in a C++ class and they are far more hassle than worth. I guess you COULD use the STL list implementation, but I haven't used it to know if it would do any good.

If your question is help with creating a linked list from scratch I could help you out alittle bit.

CBart21
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Need Help With Linked Lists for container

Posted 28 October 2006 - 12:44 PM

Linked lists in C++ are very basic and extremely useful...take a look here for an introduction.

http://richardbowles...st/linklist.htm

As this is an assignment, you may not be allowed to use the STL, but if you are, that implementation would work fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1