Copy Constructor for Array of Linked Lists

Copy Constructor for Array of Linked Lists

Page 1 of 1

0 Replies - 2545 Views - Last Post: 07 December 2008 - 07:25 PM Rate Topic: -----

#1 stritone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 07-August 08

Copy Constructor for Array of Linked Lists

Post icon  Posted 07 December 2008 - 07:25 PM

Hello,

I was searching the web for information on coding a copy constructor for an array of linked lists type data structure. I've not coded one of these before but here's my attempt below. Can someone help me refine and correct this?

Here's the premise of what I'm working on:
Answer the following questions for a "hash table class" (implemented using an array of linear linked lists, where each node contains a char *; assume that there is a head private data member but no tail pointer to each linear linked list). Implement the code for the Copy Constructor - do not assume that functions exist except strcmp, strcpy, and strlen:

I'm seeing the node structure as the following:
struct node {
  char * name;
  node * next;
};



***************************
HashTable::HashTable(const HashTable & ht) {
   int i = 0; 
   int length = strlen(head);
   node * newArry[] = head[i];

   for(i = 0; i < length; ++i) {  /* NOT SURE WHAT TO DO HERE */
		 
		if (newArry[i] == NULL) {
		   cerr("Head was NULL.");
		   exit(1);
		}

	   node * newPtr = newArry[i];
	   node * oldPtr = head[i];
	   while(oldPtr->next != NULL){
			newPtr = new node;
			newPtr->name = new char[strlen(oldPtr->name) + 1];
			strcpy(newPtr->name, oldPtr->name);
			oldPtr = oldPtr->next;
			newPtr = newPtr->next;
	   }
   }



This question is part of a review for a final I have coming up and I don't feel confident in my understanding of it.

Thanks!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1