//
you're reading...
C++, Data Structures, Source Code

B2B::NodeNFromEnd

Second post in the series Back to Basic

Display the Nth Node from the End

void findNfromEnd(int nodeNum){
	node *ptr1, *ptr2;
	ptr1 = ptr2 = head;
	for(int i=0; i< (nodeNum-1) ; i++){
		ptr1 = ptr1->next;
	}
	while(ptr1->next != NULL){
		ptr1= ptr1->next;
		ptr2 = ptr2->next;
	}
	cout << "Nth Element from End: " << ptr2->data << endl; 
}
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: