linked list

S

Shraddha

Can we access a node without traversing the list........

I mean if a list is having total 10 nodes..and I want to jump directly
on 5th node without traversing first four nodes can I do that?
The list is singly list...

I tried it using macros...I just thought of it....But I can not code
it..

This is what I thought..
To define a macro of arrow...
And using for loop we can attached that many of arrows to a string
variable....
Then we can access it...

I know this is a very vague idea of what I want to do...But I am not
getting a proper solution...
Please can anyone help me out...
 
K

keith

Can we access a node without traversing the [singly-linked] list........

Nope. If you need to access a specific entry, you probably want an
array or a vector (array of pointers) instead.
 
R

Richard Heathfield

Shraddha said:
Can we access a node without traversing the list........

If you need to do this, a linked list is the wrong data structure, or at
least is insufficient on its own.

This is what I thought..
To define a macro of arrow...
And using for loop we can attached that many of arrows to a string
variable....
Then we can access it...

No, that isn't going to work. (And even if it did, it would just be
another way of traversing the list.)
I know this is a very vague idea of what I want to do...But I am not
getting a proper solution...
Please can anyone help me out...

Get yourself an array of pointers, and point each element to one entry
in the list. That requires traversing the list once. Thereafter, you
have random access via the array - at least, until the list changes, at
which point you'll need to update your array appropriately.
 
M

mark_bluemel

Can we access a node without traversing the list........

Not unless you have some additional structure, such as an array of
pointers to list elements, in which case why would you bother with the
list?

[Snip]
I tried it using macros...I just thought of it....But I can not code
it..

This is what I thought..
To define a macro of arrow...
And using for loop we can attached that many of arrows to a string
variable....
Then we can access it...

I think what you're saying is that you want to have something like
this :-

struct node {
<type> data;
struct node *next;
};

struct node *head;

.... <code to populate the linked list, with the address of the first
entry stored in "head">...
head->next->next->next->next->data;

This is legal, but not hugely useful, IMHO.

What I then think you want to do is dynamically build the "head->next-
next..." string and use it as an expression.

You can't do that - C is a compiled, not a dynamic language (using the
terms loosely).
 
R

Richard Heathfield

CBFalconer said:
Richard Heathfield wrote:


If he is using a list he probably has an unknown number of items to
start with, and doesn't want an array.

I don't see why not. Dynamic, of course, so that if he runs out of space
he can always realloc. It's no big deal.
I suggest looking into use of hashlib

Why is that a better fit to his problem than a simple array?
 

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

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,100
Latest member
MelodeeFaj
Top