FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Linked Lists





akshar
You aree given a linked list of unkown number of nodes. You are not given any pointer to the first node but you know the memory address of nth node. You have to enter a new node at n-1 position how will you do that.
AaronMoore
If it's C or C++, use pointer arithmetic like:

Code:

type *new = /*Something */

type *ptr = n-(2*sizeof(type))

type *oldNLess1 = ptr->next

//insert into n-1
new->next = oldNLess1
ptr->next = new


Hope that helps
(Using a diagram will help you understanding of linked lists...)
akshar
you have given the correct answer
AaronMoore
What??? I thought you needed help! I didn't know this was a contest!

But thanks, that's nice to know

I'll try and find one in the next few days. Wink
AhmedSalmanJamal
AaronMoore wrote:
If it's C or C++, use pointer arithmetic like:

Code:

type *new = /*Something */

type *ptr = n-(2*sizeof(type))

type *oldNLess1 = ptr->next

//insert into n-1
new->next = oldNLess1
ptr->next = new


Hope that helps
(Using a diagram will help you understanding of linked lists...)


Your answer is definitely correct...however there is a slight error in your code... the syntax "new" is known as a function and hence strictly speaking you are not allowed to have a variable with the name "new"... Smile
akshar
see the concept here is more important than the function. Syntax correction is a donkey's job but developing an algorithm is certainly a goob brainy job.
Related topics
which Programing launguage to learn
data structures
Data structures - small question
2, 3 Trees (data structures)
Machine Problem No.2 [Pt. 1]
"PHP Vulnerability May Halt Millions of Servers"
Tutorial: PHP Installed Modules Dynamic Reference Tool
Stack Implementation by Linked List
Lists of Modding site..
Windows File System Explained
Programming links, info, and tutorials
Justification for War in Iraq
iPods Good or Bad?
Any Martial Arts movie buffs?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.