Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > calling a singly-linked list

Reply
Thread Tools

calling a singly-linked list

 
 
Phred Phungus
Guest
Posts: n/a
 
      02-08-2010
In the following program:

dan@dan-desktop:~/source/unleashed/ch11$ gcc -D_GNU_SOURCE -Wall
-Wextra ac1.c -o out
dan@dan-desktop:~/source/unleashed/ch11$ ./out
7
345
23
0
dan@dan-desktop:~/source/unleashed/ch11$ cat ac1.c
#include <stdio.h>
#include <stdlib.h>

struct list {
struct list *next;
int n;
};

static int *read_numbers(struct list *list)
{
char buf[100];
int val, i, *ret;
struct list node, *p;
if(fgets(buf, sizeof buf, stdin)) {
if(sscanf(buf, "%d", &val)!=1) {
fprintf(stderr, "That's not a number\n");
exit(EXIT_FAILURE);
}
if(val!=0) {
node.n = val;
node.next = list;
return read_numbers(&node);
}
} else {
if(ferror(stdin)) {
perror("standard input");
exit(EXIT_FAILURE);
}
/* Naughty user sent EOF; pretend it was a 0 but with a warning */
fprintf(stderr, "Didn't get 0 terminator. List may be incomplete\n");
}

for(i=0,p=list;p;++i,p=p->next)
/*nothing*/;
ret = malloc(i * sizeof *ret);
if(!ret) {
perror("malloc");
exit(EXIT_FAILURE);
}
for(--i,p=list;p;--i,p=p->next)
ret[i] = p->n;
return ret;
}

int main(void)
{
int *numbers;
numbers = read_numbers(0);
/* do stuff with numbers[] I guess */
return 0;
}


// gcc -D_GNU_SOURCE -Wall -Wextra ac1.c -o out
dan@dan-desktop:~/source/unleashed/ch11$

, am I correct to think that the list is visible to entire program? My
question is how main would correctly output what I input.

Cheers,
--
fred
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      02-08-2010
On Feb 8, 5:26*am, Phred Phungus <Ph...@example.invalid> wrote:
> , am I correct to think that the list is visible to entire program? *My
> question is how main would correctly output what I input.


There is no variable named "list" in the entire program. There is a
struct named 'list' which is visible to the entire unit.

Tom
 
Reply With Quote
 
 
 
 
bartc
Guest
Posts: n/a
 
      02-08-2010
"Phred Phungus" <> wrote in message
news:...
> In the following program:


> static int *read_numbers(struct list *list)


> int main(void)
> {
> int *numbers;
> numbers = read_numbers(0);
> /* do stuff with numbers[] I guess */
> return 0;
> }


> , am I correct to think that the list is visible to entire program? My
> question is how main would correctly output what I input.


read_numbers() returns a pointer to an array of ints, which is stored in
numbers. At this point it's only visible to main().

This example tries to print the results:

int main(void)
{
int *numbers,*p;
numbers = read_numbers(0);
/* do stuff with numbers[] I guess */
p=numbers;
while (*p) printf("%d\n",*p++);
return 0;
}

But it doesn't quite work; I don't know what read_numbers does exactly: it
seems to read one integer per line, following by 0 on a line by itself (and
does this recursively, into an internal linked-list). Then returns an array
of the numbers, however without a terminator, so main() doesn't know how
many numbers there are.

--
Bartc

 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      02-08-2010
In article <>,
Phred Phungus <> wrote:
|#include <stdio.h>
|#include <stdlib.h>
|
|struct list {
| struct list *next;
| int n;
|};
|
|static int *read_numbers(struct list *list)
[...]

This code, which I posted in another thread, should not be used. It was a
response to a "challenge" and should be considered mainly as a joke. It does
technically do everything the challenge requested, while being useless for
any practical purpose.

In the very same article in which I posted it, I listed 3 reasons why the
challenge (i.e. make a growable list/array-ish data structure without
realloc) was stupid. And I also explained the particular shortcoming of my
code which I was too lazy to fix: it provides no way for the caller to know
how big the list is.

Yet somehow you missed all of that, and decided to hold on to that code and
treat it as if it was worthy of further study, and start a new thread
dedicated to analyzing it. Woosh!

--
Alan Curry
 
Reply With Quote
 
Phred Phungus
Guest
Posts: n/a
 
      02-09-2010
Alan Curry wrote:
> In article <>,
> Phred Phungus <> wrote:
> |#include <stdio.h>
> |#include <stdlib.h>
> |
> |struct list {
> | struct list *next;
> | int n;
> |};
> |
> |static int *read_numbers(struct list *list)
> [...]
>
> This code, which I posted in another thread, should not be used. It was a
> response to a "challenge" and should be considered mainly as a joke. It does
> technically do everything the challenge requested, while being useless for
> any practical purpose.
>
> In the very same article in which I posted it, I listed 3 reasons why the
> challenge (i.e. make a growable list/array-ish data structure without
> realloc) was stupid. And I also explained the particular shortcoming of my
> code which I was too lazy to fix: it provides no way for the caller to know
> how big the list is.


You mentioned passing a counter to a recursive function.

I didn't see a better posting than yours, and now it's Feb. 9 east of
Chicago.
>
> Yet somehow you missed all of that, and decided to hold on to that code and
> treat it as if it was worthy of further study, and start a new thread
> dedicated to analyzing it. Woosh!
>


I'm having trouble hooking up data structures with a platform and
extensions that are somewhat new to me. So I look for simpler toy
programs that do something similar.

It's not hard to mistake differing forms of lists. I was hoping to do
something with this from Unleashed:

//c11_022.c - naive single linked list, using an array.
#include <stdio.h>

typedef struct ITEM
{
char Title[30];
char Author[30];
int Next;
} ITEM;

int main(void)
{
ITEM List[] =
{
{"UNIX Unleashed", "Burk and Horvath", 2},
{"Algorithms in C", "Sedgewick", 9},
{"Builder Unleashed", "Calvert", 10},
{"C++ Unleashed", "Liberty", 12},
{"Linux Unleashed", "Husain and Parker", 8},
{"Teach Yourself BCB", "Reisdorph", 1},
{"Data Structures & Algorithms", "Lafore", 3},
{"DOS Programmers Reference", "Dettmann & Johnson", 11},
{"C Programming Language", "Kernighan & Ritchie", 6},
{"C++ Programming Language", "Stroustrup", 13},
{"C: How to Program", "Deitel & Deitel", 7},
{"C : A Reference Manual", "Harbison & Steele", 15},
{"The Standard C Library", "Plauger", 5},
{"C Programming FAQs", "Summit", 14},
{"Expert C Programming", "van der Linden", -1},
{"C Unleashed", "Heathfield & Kirby", 4}
};

int Current = 0;

while(Current != -1)
{
printf("Read %s, by %s.\n",
List[Current].Title,
List[Current].Author);
Current = List[Current].Next;
}

return 0;
}

// gcc -D_GNU_SOURCE -Wall -Wextra ll1.c -o out

I think the classification for what is a singly-linked list must be
fairly broad. In this, there aren't new nodes created dynamically.
--
fred
 
Reply With Quote
 
frank
Guest
Posts: n/a
 
      02-09-2010
bartc wrote:
> "Phred Phungus" <> wrote in message


>> , am I correct to think that the list is visible to entire program?
>> My question is how main would correctly output what I input.

>
> read_numbers() returns a pointer to an array of ints, which is stored in
> numbers. At this point it's only visible to main().
>
> This example tries to print the results:

[code elided]
>
> But it doesn't quite work; I don't know what read_numbers does exactly:
> it seems to read one integer per line, following by 0 on a line by
> itself (and does this recursively, into an internal linked-list). Then
> returns an array of the numbers, however without a terminator, so main()
> doesn't know how many numbers there are.
>


Thanks bart, it's starting to look like something:

dan@dan-desktop:~/source/unleashed/ch11$ gcc -D_GNU_SOURCE -Wall
-Wextra ac2.c -o out
dan@dan-desktop:~/source/unleashed/ch11$ ./out
3
6
18
0
3
6
18
135153
dan@dan-desktop:~/source/unleashed/ch11$ cat ac2.c
#include <stdio.h>
#include <stdlib.h>

struct list {
struct list *next;
int n;
};

static int *read_numbers(struct list *list)
{
char buf[100];
int val, i, *ret;
struct list node, *p;
if(fgets(buf, sizeof buf, stdin)) {
if(sscanf(buf, "%d", &val)!=1) {
fprintf(stderr, "That's not a number\n");
exit(EXIT_FAILURE);
}
if(val!=0) {
node.n = val;
node.next = list;
return read_numbers(&node);
}
} else {
if(ferror(stdin)) {
perror("standard input");
exit(EXIT_FAILURE);
}
/* Naughty user sent EOF; pretend it was a 0 but with a warning */
fprintf(stderr, "Didn't get 0 terminator. List may be incomplete\n");
}

for(i=0,p=list;p;++i,p=p->next)
/*nothing*/;
ret = malloc(i * sizeof *ret);
if(!ret) {
perror("malloc");
exit(EXIT_FAILURE);
}
for(--i,p=list;p;--i,p=p->next)
ret[i] = p->n;
return ret;
}

int main(void)
{
int *numbers,*p;
numbers = read_numbers(0);
/* do stuff with numbers[] I guess */
p=numbers;
while (*p) printf("%d\n",*p++);
return 0;
}

// gcc -D_GNU_SOURCE -Wall -Wextra ac2.c -o out
dan@dan-desktop:~/source/unleashed/ch11$

Again with input as above, can we cheat in main somehow to determine how
many integers are in this array?
--
fred
 
Reply With Quote
 
Phred Phungus
Guest
Posts: n/a
 
      02-09-2010
Tom St Denis wrote:
> On Feb 8, 5:26 am, Phred Phungus <Ph...@example.invalid> wrote:
>> , am I correct to think that the list is visible to entire program? My
>> question is how main would correctly output what I input.

>
> There is no variable named "list" in the entire program. There is a
> struct named 'list' which is visible to the entire unit.
>
> Tom


So it doesn't go away until the entire program does, right?
--
fred
 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      02-09-2010
In article <>,
Phred Phungus <> wrote:
|Alan Curry wrote:
|>
|> In the very same article in which I posted it, I listed 3 reasons why the
|> challenge (i.e. make a growable list/array-ish data structure without
|> realloc) was stupid. And I also explained the particular shortcoming of my
|> code which I was too lazy to fix: it provides no way for the caller to know
|> how big the list is.
|
|You mentioned passing a counter to a recursive function.
|
|I didn't see a better posting than yours, and now it's Feb. 9 east of
|Chicago.

What part of everything I just said did you not understand?

Every program posted in that thread was awful, because the thread was
centered around a challenge to write a program around a ridiculous
restriction. The result is: the program comes out ugly.

|>
|> Yet somehow you missed all of that, and decided to hold on to that code and
|> treat it as if it was worthy of further study, and start a new thread
|> dedicated to analyzing it. Woosh!
|>

Double woosh.

|
|I'm having trouble hooking up data structures with a platform and
|extensions that are somewhat new to me. So I look for simpler toy
|programs that do something similar.

This is not a toy. Just a bad program, written in response to a challenge
whose rules guaranteed that only bad programs would be submitted. Keep out of
reach of children. Do not taunt happy fun ball.

|
|It's not hard to mistake differing forms of lists. I was hoping to do
|something with this from Unleashed:

"Do something"?

|
|//c11_022.c - naive single linked list, using an array.
|#include <stdio.h>
|
|typedef struct ITEM
|{
| char Title[30];
| char Author[30];
| int Next;
|} ITEM;
|
|int main(void)
|{
| ITEM List[] =

This demonstrates something more useful: that an array plus an index is a lot
like a pointer, and if the array (base address) is implied (because all of
your structures live in one big array) then you can use integers as if they
were pointers, just by adding the base address when you need to dereference.

This one is the opposite of the "crazy recursion" program. It really is a
toy. You won't see statically-sized linked lists, initialized at compile
time, in grownup programs.

But the use of integers as pointers inside a cluster of objects with a common
base address isn't necessarily "naive". It's serialized! Ready to be
relocated with memcpy, or transferred to another process as a single hunk,
since the contents are not dependent on memory location.

--
Alan Curry
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      02-09-2010
On 9 Feb, 07:57, Richard Heathfield <r...@see.sig.invalid> wrote:
> Phred Phungus wrote:


> > I think the classification for what is a singly-linked list must be
> > fairly broad. *In this, there aren't new nodes created dynamically.

>
> ['this' is an illustration of the linked-list concept that uses an array
> as the container]
>
> Right. The concept of "linked list" depends purely on each item having a
> link to some other item (or items) in the list so that a logical linear
> progression exists. There's nothing in the rules that says the links
> have to be pointers, or that the list has to be capable of arbitrary
> expansion, or anything else.


I learnt my data structures by "emulating" pointers with indexes into
large arrays. This was because the language used didn't have pointers.
In fact I wasn't even aware that this *was* a pointer emulation as I
didn't know about pointers either. I don't think it did me much harm
as far as learning about data structures and algorithms went.


> As far as the *concept* is concerned,
> that's fluff. Highly important and significant fluff from a practical
> point of view, but fluff nonetheless from the perspective of what
> actually constitutes a linked list.
>
> You have probably already noted that, in your source text, the author of
> your example points out that the array implementation is "naive", and
> shortly moves on to present pointer-based lists.

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      02-09-2010
On 9 Feb, 06:33, Phred Phungus <Ph...@example.invalid> wrote:
> Alan Curry wrote:
> > In article <7ta771Ffi...@mid.individual.net>,
> > Phred Phungus *<Ph...@example.invalid> wrote:


> > |#include <stdio.h>
> > |#include <stdlib.h>
> > |
> > |struct list {
> > | * struct list *next;
> > | * int n;
> > |};
> > |
> > |static int *read_numbers(struct list *list)

>
> > This code, which I posted in another thread, should not be used.


Note well... "should not be used"

> > It was a
> > response to a "challenge" and should be considered mainly as a joke. It does
> > technically do everything the challenge requested, while being useless for
> > any practical purpose.


"useless for any practical purpose"

<snip>

> You mentioned passing a counter to a recursive function.
>
> I didn't see a better posting than yours, and now it's Feb. 9 east of
> Chicago.


better for what? What are you trying to do? It's Feb 9 east of London
as well.

<snip>

> I'm having trouble hooking up data structures with a platform and
> extensions that are somewhat new to me.


data structures are pretty independent of platform extensions (or
should be). Are you learning data structures or platform extensions?
I'd try to learn one at a time.

> So I look for simpler toy programs that do something similar.


simpler than what? similar to what?


> It's not hard to mistake differing forms of lists. *


mistake for what? Differing in what way?


> I was hoping to do
> something with this from Unleashed:


what's something?


> //c11_022.c - naive single linked list, using an array.
> #include <stdio.h>
>
> typedef struct ITEM
> {
> * *char Title[30];
> * *char Author[30];
> * *int Next;
>
> } ITEM;
>
> int main(void)
> {
> * *ITEM List[] =
> * *{
> * * *{"UNIX Unleashed", "Burk and Horvath", 2},

[...]
{"Expert C Programming", "van der Linden", -1},
> * *};
>
> * *int Current = 0;
>
> * *while(Current != -1)
> * *{
> * * *printf("Read %s, by %s.\n",
> * * * * * * List[Current].Title,
> * * * * * * List[Current].Author);
> * * *Current = List[Current].Next;
> * *}
>
> * *return 0;
>
> }
>
> // *gcc *-D_GNU_SOURCE -Wall -Wextra ll1.c -o out
>
> I think the classification for what is a singly-linked list must be
> fairly broad. *In this, there aren't new nodes created dynamically.


not a necessisity of link lists. But in practice a major reason for
using linked lists is their easy extensibility.

I think you need a goal (or if you have one, you need to articulate
it!)


--
"XML is isomorphic to the subset of Lisp data
where the first item in a list is required to be atomic."
John McCarthy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>' asil klin Java 28 03-05-2011 01:59 AM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57