Jump to content

Hey guys, I was given a problem on my midterm to copy the last two nodes of a Doubly Linked List

and I honestly drew a blank on how to do it.  I thought that I could possibly do it recursively but then

I realized I still had no idea what to do.  Could someone possibly give an example as I can't find anything

good online.  I realized that i would have to do something like:
 

node * temp = new node;

temp -> data = current -> data;

 

which would be making a temp node and setting its data but then I had no idea what to doto get there and

what to do from there.

 

I had another one where i was supposed to remove the last node if it hadn't appeared anywhere in the doubly

linked list but I don't even have start on that.

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/588688-copy-last-two-nodes-c/
Share on other sites

Link to post
Share on other sites

I would do it iteratively rather than recursively. 

void copyList (node * curr){ 
	if(curr == NULL) 
		return NULL;
	
	while(curr != NULL){ 
		node *temp = new node; 
		temp->data = curr->data; 
		temp->next = curr->next; 
		if(curr->next != NULL) 
			curr->next->prev = temp; 
		curr = curr->next; 
	}
	return; 
}

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/588688-copy-last-two-nodes-c/#findComment-7666485
Share on other sites

Link to post
Share on other sites

9 minutes ago, djdwosk97 said:

I would do it iteratively rather than recursively. 


void copyList (node * curr){ 
	if(curr == NULL) 
		return NULL;
	
	while(curr != NULL){ 
		node *temp = new node; 
		temp->data = curr->data; 
		temp->next = curr->next; 
		if(curr->next != NULL) 
			curr->next->prev = temp; 
		curr = curr->next; 
	}
	return; 
}

 

Wouldn't that copy the whole list?

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/588688-copy-last-two-nodes-c/#findComment-7666526
Share on other sites

Link to post
Share on other sites

13 minutes ago, CJPowell27 said:

Wouldn't that copy the whole list?

Oh, whoops, my bad, I somehow didn't notice you just wanted the last two. 

 

Here's an idea, although it can be done in a more elegant manner. (This also doesn't make sure the list is at least two nodes long)

void copyLastTwo (node * curr){ 
	if(curr == NULL) 
		return NULL;
	
	while(curr != NULL){
                if(curr->next == NULL) 
		        break; 
	        curr = curr->next; 
	}

	for(int i=0; i<2; i++){
		node * temp = new node; 
		temp->data = curr->data; 
		if(i < 1){
			temp->prev = curr->prev;
		} else {
			temp->prev = NULL; 
		}
		temp->next = curr->next; 
		curr = curr->prev; 
	}

	return; 
}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/588688-copy-last-two-nodes-c/#findComment-7666576
Share on other sites

Link to post
Share on other sites

13 minutes ago, djdwosk97 said:

Oh, whoops, my bad, I somehow didn't notice you just wanted the last two. 

 

Here's an idea, although it can be done in a more elegant manner. (This also doesn't make sure the list is at least two nodes long)


void copyLastTwo (node * curr){ 
	if(curr == NULL) 
		return NULL;
	
	while(curr != NULL){
                if(curr->next == NULL) 
		        break; 
	        curr = curr->next; 
	}

	for(int i=0; i<2; i++){
		node * temp = new node; 
		temp->data = curr->data; 
		if(i < 1){
			temp->prev = curr->prev;
		} else {
			temp->prev = NULL; 
		}
		temp->next = curr->next; 
		curr = curr->prev; 
	}

	return; 
}

Oh hey that actually makes sense, there's probably a more elegant solution but that works thank you so much!

Now to figure out the other one :dry:

i5 4670k| Asrock H81M-ITX| EVGA Nex 650g| WD Black 500Gb| H100 with SP120s| ASUS Matrix 7970 Platinum (just sold)| Patriot Venom 1600Mhz 8Gb| Bitfenix Prodigy. Build log in progress 

Build Log here: http://linustechtips.com/main/topic/119926-yin-yang-prodigy-update-2-26-14/

Link to comment
https://linustechtips.com/topic/588688-copy-last-two-nodes-c/#findComment-7666646
Share on other sites

Link to post
Share on other sites

1 minute ago, CJPowell27 said:

Oh hey that actually makes sense, there's probably a more elegant solution but that works thank you so much!

Now to figure out the other one :dry:

Well, since it's doubly-linked, there should be both head and tail sentinels. So, you can just copy tail and tail->prev (assuming tail->prev isn't null). But without knowing an exact implementation and how it would be called, getting something really elegant is a bit tricky. But, since elegance usually isn't tested for, simple is best imo since there's less things you're likely to overlook. 

 

Something like this is a bit simpler (imo), but it lacks the expandability of the above, as if you wanted to copy the last ten nodes, in the first solution I posted you would just change the 2 to a 10 and the 1 to a 9, whereas this would require you to check if curr's->next->next->.....->next is null. 

 
	void copyLastTwo (node *curr){
	       //check for at least one node
	       //if there's only one node, copy one node
	       
	        //at least two nodes by this point
	        while(curr-&gt;next-&gt;next != NULL) 
	               curr = curr-&gt;next; 
	 
	        //copy curr and curr-&gt;next 
	       return; 
	}
	

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/588688-copy-last-two-nodes-c/#findComment-7666695
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×