473,408 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Linked lists and pointers

Hi,

I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,

struct linked_list {
int data;
struct linked_list next;
}

I thought of two arrays of pointers
struct linked_list **first;
struct linked_list **last;
in order to acces the linked_lists.

I have the following methods,

void
init_lists(const int N)
{
first = (struct linked_particle **)calloc(N, sizeof(struct
linked_particle *));
last = (struct linked_particle **)malloc(N * sizeof(struct
linked_particle *));

int i;
for (i = 0; i < N; i++) {
struct linked_particle *nova =
(struct linked_list *)malloc(sizeof(struct linked_list));
first[i] = last[i] = nova;
}
}

and

struct linked_list *
cells_get_cell_pointer (const int i)
{
struct linked_list *to_return = NULL;
to_return = first[i];
return to_return;
}

there is also a method for putting more elements in a given list,

void
add_element_to_list (const int i,
const int data)
{
struct linked_list *nova =
(struct linked_list *)malloc(sizeof(struct linked_particle));
nova->data = data;
struct linked_particle *last_actual = last[i];
if (!last_actual) printf("Error\n");
linked_particle_add(nova,&last_actual);

return;
}

where

void
linked_particle_add (struct linked_list * to_add,
struct linked_list ** last){
if(!*last) *last = to_add;
else (*last)->next = to_add;
to_add->next = NULL;
*last = to_add;

return;
}

The program seems to work when I add new items to a given list (I get
no errors), but when I try to get the first pointer of a given list in
order to list all the elements, I only get a NULL pointer.
I think I'm missing something, but I don't know what.

Thanks,

Nov 14 '05 #1
6 3972


paudirac wrote:
Hi,

I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,

struct linked_list {
int data;
struct linked_list next;
}
[...]
I think I'm missing something, but I don't know what.


Neither do I, since you haven't shown your source code.
(Yes, you showed *something*, but it obviously isn't your
actual code: the sample above, for example, will not compile.
Now, I know how to fix the two errors in those four lines,
but how many other inaccuracies lurk in the rest of what
you posted? Will I be debugging your code, or merely my
guesses about what your code looks like? If you prefer the
former to the latter, post actual code and not a paraphrase.)

--
Er*********@sun.com

Nov 14 '05 #2
paudirac wrote:

I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,


Whenever N is to be determined at runtime, a linked list is very
often the appropriate implementation. Thus you probably want a
linked list of headers to linked lists.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3
paudirac wrote on 16/03/05 :
I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,

struct linked_list {
int data;
struct linked_list next;
}

I think I'm missing something, but I don't know what.


a '*'

struct linked_list {
int data;
struct linked_list *next;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.

Nov 14 '05 #4
Sorry, actually my definitions is the appropiate one,
struct linked_list {
int data;
struct linked_list *next;
}

Excusme for that,

Emmanuel Delahaye wrote:
paudirac wrote on 16/03/05 :
I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,

struct linked_list {
int data;
struct linked_list next;
}

I think I'm missing something, but I don't know what.


a '*'

struct linked_list {
int data;
struct linked_list *next;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.


Nov 14 '05 #5
paudirac wrote:
...
I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,

struct linked_list {
int data;
struct linked_list next;
}
struct linked_list* next

This appears to be a type of a _single_ _entry_ of a list.
I thought of two arrays of pointers
struct linked_list **first;
struct linked_list **last;
in order to acces the linked_lists.
OK.
I have the following methods,

void
init_lists(const int N)
{
first = (struct linked_particle **)calloc(N, sizeof(struct
linked_particle *));
last = (struct linked_particle **)malloc(N * sizeof(struct
linked_particle *));
What is the importance of using 'calloc' for the first array and
'malloc' for the second? Below you explicitly assign initial values to
the elements of these arrays anyway...

Also, normally you don't have to cast the result of 'malloc' and 'calloc'.
int i;
for (i = 0; i < N; i++) {
struct linked_particle *nova =
(struct linked_list *)malloc(sizeof(struct linked_list));
first[i] = last[i] = nova;
}
}
Hmm... It appears that at initialization stage you are creating a
"dummy" element in each list. I don't exactly see the point in doing
this. But let's accept it for now...
and

struct linked_list *
cells_get_cell_pointer (const int i)
{
struct linked_list *to_return = NULL;
to_return = first[i];
return to_return;
}
Why don't just do

struct linked_list *
cells_get_cell_pointer (const int i)
{
return first[i];
}

?
there is also a method for putting more elements in a given list,

void
add_element_to_list (const int i,
const int data)
{
struct linked_list *nova =
(struct linked_list *)malloc(sizeof(struct linked_particle));
nova->data = data;
struct linked_particle *last_actual = last[i];
if (!last_actual) printf("Error\n");
linked_particle_add(nova,&last_actual);

return;
}
This is obviously wrong. Your 'linked_particle_add' function modifies
the pointer to the last element of the list through its 'last'
parameter. Here you create and pass a copy ('last_actual') of the
corresponding entry of the 'last' array ('last[i]'). 'last_actual' will
get modified, but 'last[i]' will remain unchanged. This doesn't make
sense and this is why your code doesn't work as expected. Why did you
decide to create a copy here? It is supposed to look more like

void
add_element_to_list (const int i,
const int data)
{
struct linked_list *nova = malloc(sizeof *nova);
nova->data = data;
linked_particle_add(nova, &last[i]);
}
where

void
linked_particle_add (struct linked_list * to_add,
struct linked_list ** last){
if(!*last) *last = to_add;
What's the point of this check??? Your lists start with one dummy
element already there. '*last' cannot be null here.
else (*last)->next = to_add;
to_add->next = NULL;
*last = to_add;

return;
}


If I were you, I'd get rid of the initial dummy elements in the lists
and also lifted the 'last' pointers to a higher level of indirection.
That would make the code much cleaner

struct linked_list **first;
struct linked_list ***last; /* Note 3 stars */

void init_lists(int N)
{
first = malloc(N * sizeof *first);
last = malloc(N * sizeof *last);

int i;
for (i = 0; i < N; i++) {
first[i] = NULL;
last[i] = &first[i];
/* Note what 'last' array contains now */
}
}

void add_element_to_list(int i, int data)
{
struct linked_list *nova = malloc(sizeof *nova);
nova->data = data;
linked_particle_add(nova, &last[i]);
}

void linked_particle_add(struct linked_list * to_add,
struct linked_list *** last)
{
to_add->next = NULL;
**last = to_add;
*last = &to_add->next;
/* Note how 'last' is used here */
}

That's it.

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #6
Thank you very much, I didn't realized that I was creating a copy
instead of using a pointer. Actually, I'm very new in C and I'm not so
much used to pointers and different levels of indirection yet. Now it
works fine.

Thanks,

Andrey Tarasevich wrote:
paudirac wrote:
...
I need to maintain N linked lists where N is determined at runtime.
The linked lists are defined as follows,

struct linked_list {
int data;
struct linked_list next;
}
struct linked_list* next

This appears to be a type of a _single_ _entry_ of a list.
I thought of two arrays of pointers
struct linked_list **first;
struct linked_list **last;
in order to acces the linked_lists.


OK.
I have the following methods,

void
init_lists(const int N)
{
first = (struct linked_particle **)calloc(N, sizeof(struct
linked_particle *));
last = (struct linked_particle **)malloc(N * sizeof(struct
linked_particle *));


What is the importance of using 'calloc' for the first array and
'malloc' for the second? Below you explicitly assign initial values

to the elements of these arrays anyway...

Also, normally you don't have to cast the result of 'malloc' and 'calloc'.
int i;
for (i = 0; i < N; i++) {
struct linked_particle *nova =
(struct linked_list *)malloc(sizeof(struct linked_list));
first[i] = last[i] = nova;
}
}
Hmm... It appears that at initialization stage you are creating a
"dummy" element in each list. I don't exactly see the point in doing
this. But let's accept it for now...
and

struct linked_list *
cells_get_cell_pointer (const int i)
{
struct linked_list *to_return = NULL;
to_return = first[i];
return to_return;
}


Why don't just do

struct linked_list *
cells_get_cell_pointer (const int i)
{
return first[i];
}

?
there is also a method for putting more elements in a given list,

void
add_element_to_list (const int i,
const int data)
{
struct linked_list *nova =
(struct linked_list *)malloc(sizeof(struct linked_particle));
nova->data = data;
struct linked_particle *last_actual = last[i];
if (!last_actual) printf("Error\n");
linked_particle_add(nova,&last_actual);

return;
}


This is obviously wrong. Your 'linked_particle_add' function modifies
the pointer to the last element of the list through its 'last'
parameter. Here you create and pass a copy ('last_actual') of the
corresponding entry of the 'last' array ('last[i]'). 'last_actual'

will get modified, but 'last[i]' will remain unchanged. This doesn't make
sense and this is why your code doesn't work as expected. Why did you
decide to create a copy here? It is supposed to look more like

void
add_element_to_list (const int i,
const int data)
{
struct linked_list *nova = malloc(sizeof *nova);
nova->data = data;
linked_particle_add(nova, &last[i]);
}
where

void
linked_particle_add (struct linked_list * to_add,
struct linked_list ** last){
if(!*last) *last = to_add;


What's the point of this check??? Your lists start with one dummy
element already there. '*last' cannot be null here.
else (*last)->next = to_add;
to_add->next = NULL;
*last = to_add;

return;
}


If I were you, I'd get rid of the initial dummy elements in the lists
and also lifted the 'last' pointers to a higher level of indirection.
That would make the code much cleaner

struct linked_list **first;
struct linked_list ***last; /* Note 3 stars */

void init_lists(int N)
{
first = malloc(N * sizeof *first);
last = malloc(N * sizeof *last);

int i;
for (i = 0; i < N; i++) {
first[i] = NULL;
last[i] = &first[i];
/* Note what 'last' array contains now */
}
}

void add_element_to_list(int i, int data)
{
struct linked_list *nova = malloc(sizeof *nova);
nova->data = data;
linked_particle_add(nova, &last[i]);
}

void linked_particle_add(struct linked_list * to_add,
struct linked_list *** last)
{
to_add->next = NULL;
**last = to_add;
*last = &to_add->next;
/* Note how 'last' is used here */
}

That's it.

--
Best regards,
Andrey Tarasevich


Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
7
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
2
by: Skywise | last post by:
I am fairly new to linked lists. I am trying to write a class using linked lists. It seems to work fine, but I need to know if I have any resource leaks in it because I plan on using this class...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
3
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.