4 Replies - 4829 Views - Last Post: 08 December 2008 - 07:45 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 Question

Post icon  Posted 07 December 2008 - 06:50 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
  • +

Replies To: Copy Constructor for Array of Linked Lists Question

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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

This post has been edited by pbl: 07 December 2008 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,173
  • Joined: 20-September 08

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++
Was This Post Helpful? 0
  • +
  • -

#4 stritone  Icon User is offline

  • New D.I.C Head

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

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!
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Copy Constructor for Array of Linked Lists Question

Posted 08 December 2008 - 07:45 PM

View Postg00se, on 8 Dec, 2008 - 02:57 AM, said:

You seem to be in the wrong topic area - your code is C++

Shame !!!! on me :v: :v: :v:

I spent my day writting C++ code and I didn't even notice the *
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1