Linked list

S

surender

Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.
Any body Pls help me.

Thanks in advance
 
K

Karl Heinz Buchegger

surender said:
Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.
Any body Pls help me.

You can't.
You need at least the pointer to the first node (usually called
the 'head of the list')
 
P

petermcmillan_uk

Unless it was doubly linked with a 'previous' pointer. If he's
implementing the list himself, then he could possibly add a 'previous'
pointer.
 
U

Ulrich Achleitner

Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.

reverse traversing is not possible with a single linked list (because
every node has only pointer to its successor).
you need to use a doubly linked list to traverse from the e.g. 50th list
to the 49th

ulrich
 
M

msalters

surender said:
Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.
Any body Pls help me.

Thanks in advance

A circular single linked list can go from the last to the first
element. In that case, and only in that case, can you from
50->99->0->49.

Regards,
Michiel Salters
 
J

John Carson

surender said:
Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address

What do you have?
 
K

KiLVaiDeN

surender said:
Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.
Any body Pls help me.

Thanks in advance

If the list was built at once, there is chance that by some statistics on
memory adresses, you can find the 49th node, if you know the position in
memory of the 50th 51th 52nd 53rd.. But it's very unlikely ! It could be
undefined data..

K
 
C

Chris Gordon-Smith

surender said:
Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.
Any body Pls help me.

Thanks in advance

Erm - Just out of curiosity, what stops you from using a doubly linked list?

Chris Gordon-Smith
London
http://graffiti.virgin.net/c.gordon-smith/
 
C

chris

surender said:
Hi,
Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.
Any body Pls help me.
If this is a programming question, your programming is poor! If this is
a homework question, then either you've read it wrong, or it's badly
worded :)

One possiblility is that the linked list is circular (the last node is
attached to the first one). If this is the case you could go all the way
around and get to it from the start :)

Chris
 
T

Taran

Simple. No you can't.
In a singly linked list given any node address you can traverse only
forward, cos the node only gives you the _next node address.

You can traverse backwards only if its a doubly linked list.
Or else if you have a circular linked list then you can traverse the
list counting the nodes and print the 99th node.

You can do this way, but I suggest not.
First: There's low probability of you hitting the right address.
Second: Second whatever address you land at, you have no clue whether
the data there is valid or not in terms of context.
HTH.

Regards,
Taran
 
R

Robert B. Clark

Let us think that we have 100 nodes in the single linked list.
I have only 50th node address and i don't have the first node address
but i want to print the 49th node data. So how can we print the 49th
node data.

You don't (unless this is a circular singly-linked list, which you did
not stipulate).

The usual practice is to always maintain a pointer to the head node of a
singly-linked list, or to use a doubly-linked list (nodes include
pointers to n-1 as well as n+1).
 
F

Flash Gordon

Unless it was doubly linked with a 'previous' pointer. If he's
implementing the list himself, then he could possibly add a 'previous'
pointer.

Please include enough of the message you are replying to for people too
*see* what you are replying to. There is absolutely *no* guarantee that
people reading your message will have seen the message you are replying to.

However, in answer to your point (since I happen to have the message
handy), what part of "Let us think that we have 100 nodes in the single
linked list." do you think allows it to be a doubly linked list. The
last time I checked 1 and 2 were different numbers.
 
I

infobahn

The
last time I checked 1 and 2 were different numbers.

Let a = b.

Then multiply each side by b, so: ab = a^2
Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
Factorise: b(a - b) = (a + b)(a - b)
Divide each side by common term: b = a + b
Substitute a (because a = b): b = 2b
Divide by b: 1 = 2

QED.

;-)
 
G

Gary Labowitz

infobahn said:
Let a = b.

Then multiply each side by b, so: ab = a^2
Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
Factorise: b(a - b) = (a + b)(a - b)
Divide each side by common term: b = a + b
Substitute a (because a = b): b = 2b
Divide by b: 1 = 2

QED.

;-)
Boy, talk about undefined behavior!!! ::))
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Flash Gordon wrote:




Let a = b.

Then multiply each side by b, so: ab = a^2
Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
Factorise: b(a - b) = (a + b)(a - b)
Divide each side by common term:

A quick aside...
Assuming

a = b (1)

then, subtracting b from both sides gives
a - b = b - b (2)

solving the right hand side gives

a - b = 0 (3)

Back to your exposition

Divide each side by the common term (a - b)
is the same as
Divide each side by 0 (from (3) above)

So, your solution requires division by zero, which invokes "undefined behaviour"
in the 'real world' language said:
b = a + b
Substitute a (because a = b): b = 2b
Divide by b: 1 = 2

QED.

;-)


- --
Lew Pitcher
IT Consultant, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB3Z32agVFX4UWr64RAjtOAKCHWSrbqBPTgC8gAlP1PHQ7Tatx8wCeKNTt
nHHELXK7cIsg+iPeq/xKtEU=
=F/rF
-----END PGP SIGNATURE-----
 
H

Howard

infobahn said:
Let a = b.

Then multiply each side by b, so: ab = a^2
Now subtract b^2 from each side: ab - b^2 = a^2 - b^2
Factorise: b(a - b) = (a + b)(a - b)
Divide each side by common term: b = a + b
Substitute a (because a = b): b = 2b
Divide by b: 1 = 2

QED.

;-)

I like it! :)

Of course, there are some flaws in your proof (but well-hidden, which is
what makes this fun!)...

Your conclusion itself is incorrect. Given the formula b = 2b, there are
two "potential" solutions. Either 1=2, or b=0. Since no value can be
substituted for 1 which makes it equal 2 (as it is a constant, and will
accept only the value 1), the only answer available is that b equals zero.

So, does that prove that if a=b, then a=b=0? :)

Obviously, the real culprit is that you can't divide by zero. The operation
where you divide both sides by some value (a-b, in this case), is only valid
iff that value is non-zero. As I recall, the form that line should take
would be

b = a + b { a-b != 0 }

(Which is a fallacy, since we already said a=b, which implies a-b=0.)

But it's still great fun! I'd like to give it to an early Algebra class and
see what they have to say about it... :-0

-Howard
 
K

Keith Thompson

Howard said:
I like it! :)

Of course, there are some flaws in your proof (but well-hidden, which is
what makes this fun!)...

Your conclusion itself is incorrect. Given the formula b = 2b, there are
two "potential" solutions. Either 1=2, or b=0. Since no value can be
substituted for 1 which makes it equal 2 (as it is a constant, and will
accept only the value 1), the only answer available is that b equals zero.

That's easily corrected by assuming that a and b have some
(unspecified) non-zero value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top