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!
Copy Constructor for Array of Linked Lists QuestionCopy Constructor Help
Page 1 of 1
4 Replies - 4356 Views - Last Post: 08 December 2008 - 07:45 PM
#1
Copy Constructor for Array of Linked Lists Question
Posted 07 December 2008 - 06:50 PM
Replies To: Copy Constructor for Array of Linked Lists Question
#2
Re: Copy Constructor for Array of Linked Lists Question
Posted 07 December 2008 - 07:34 PM
"Copy constructor" is a C++ concept
In Java we use the clone() method of the object
Have your class to implements Cloneable and Java will do it for you
In Java we use the clone() method of the object
Have your class to implements Cloneable and Java will do it for you
This post has been edited by pbl: 07 December 2008 - 07:35 PM
#3
Re: Copy Constructor for Array of Linked Lists Question
Posted 08 December 2008 - 03:57 AM
You seem to be in the wrong topic area - your code is C++
#4
Re: Copy Constructor for Array of Linked Lists Question
Posted 08 December 2008 - 12:03 PM
Hi Pbl,
Sorry about that. I posted under the wrong section initially. The same post is in the C++ forum now but still no responses. Maybe this isn't a very common copy constructor question.
At any rate, thanks for your response. I'll keep trying in the C++ forum.
Thanks!
Sorry about that. I posted under the wrong section initially. The same post is in the C++ forum now but still no responses. Maybe this isn't a very common copy constructor question.
At any rate, thanks for your response. I'll keep trying in the C++ forum.
Thanks!
#5
Re: Copy Constructor for Array of Linked Lists Question
Posted 08 December 2008 - 07:45 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|