linked list example

B

Bill Cunningham

Is this a very simple example of a linked list or is the concept I am
expressing in error again.

#include "c.h"

struct cat kat, *p;

int main(void)
{
for (p = p; p != NULL; p = p->next) {
kat.name = "striper";
kat.color = "striped";
if (p == NULL) {
printf("no more lists");
return 0;
}
return 0;
}
}

c.h just contains a 3 member struct and stdio, stdlib and string.h.

Bill
 
T

Twixer Xev

Is this a very simple example of a linked list or is the concept I am
expressing in error again.

#include "c.h"

struct cat kat, *p;

int main(void)
{
for (p = p; p != NULL; p = p->next) {

The pointer p was never initialized. What does it point to? Nobody
knows.
kat.name = "striper";
kat.color = "striped";
if (p == NULL) {

The pointer p will never equal NULL at this stage of the loop.
printf("no more lists");
return 0;
}
return 0;

If the loop manages to run at all, it will only go once due to this
unconditional return.
 
O

osmium

Bill Cunningham said:
Is this a very simple example of a linked list or is the concept I am
expressing in error again.

#include "c.h"

struct cat kat, *p;

int main(void)
{
for (p = p; p != NULL; p = p->next) {
kat.name = "striper";
kat.color = "striped";
if (p == NULL) {
printf("no more lists");
return 0;
}
return 0;
}
}

c.h just contains a 3 member struct and stdio, stdlib and string.h.

Please don't post things that don't compile. There is no definition for
struct cat.
 
B

Bill Cunningham

Twixer said:
The pointer p was never initialized. What does it point to? Nobody
knows.

Ok. Whew. Would I want it to point to the kat of struct type?
The pointer p will never equal NULL at this stage of the loop.
ok


If the loop manages to run at all, it will only go once due to this
unconditional return.

I'm getting closer to the linked list. I pulled this from an online
example. I have been reading kandr2 lately that goes into detail with a
binary tree. Isn't a pointer usually initialized to NULL now that I
remember?

Bill
 
A

Andrew Smallshaw

Is this a very simple example of a linked list or is the concept I am
expressing in error again.

#include "c.h"

struct cat kat, *p;

int main(void)
{
for (p = p; p != NULL; p = p->next) {
kat.name = "striper";
kat.color = "striped";
if (p == NULL) {
printf("no more lists");
return 0;
}
return 0;
}
}

c.h just contains a 3 member struct and stdio, stdlib and string.h.

....and it would be very helpful here to see what it is you are
trying to do. The above code seems to be a mishmash of various
parts of linked list operation. On the one hand it appears to be
trying to populate a list and on the other it appears to assume
the list is already structured. You should start by separating
out the two parts into separate fucntions - one allocates space
for a new entry using malloc(), populates it with whatever is needed
and links it into the list. The second reads out the list.

Also look at the interaction between that loop and your if statement
- the if condition will never be satisfied since the loop will stop
iterating when p is NULL. Perhaps a do...while loop would be the
more natural way of expressing what you were attempting.

Examples of those functions - not tested, just quickly knocked out
so they may not be perfect:

struct cat add_entry(name, color, old_list)
{
struct cat *new;

if ((new = malloc(sizeof(struct cat))) == NULL)
abort() /* or whatever error handling */

new->name = name;
new->color = color;

new->next = old_list;

return new;
}

void print_entries(struct cat *list)
{
while (list != NULL) {
printf("Cat: %s color: %s\n", list->name, list->color);
list = list->next;
}
}
 
B

Bill Cunningham

osmium said:
Please don't post things that don't compile. There is no definition
for struct cat.

It compiled for me. Of course then it didn't do anything.

Bill
 
B

Bill Cunningham

Andrew said:
...and it would be very helpful here to see what it is you are
trying to do. The above code seems to be a mishmash of various
parts of linked list operation.

It is.

On the one hand it appears to be
trying to populate a list and on the other it appears to assume
the list is already structured. You should start by separating
out the two parts into separate fucntions - one allocates space
for a new entry using malloc(), populates it with whatever is needed
and links it into the list.

ok

The second reads out the list.
 
K

Keith Thompson

Twixer Xev said:
The pointer p was never initialized. What does it point to? Nobody
knows.

Since p was declared at file scope, it has static storage duration and
is therefore implicitly initialized to NULL. The loop body will never
execute.

[...]
 
K

Keith Thompson

Bill Cunningham said:
It compiled for me. Of course then it didn't do anything.

It compiled for you because you have a copy of "c.h". We don't.

Are you still using examples from that same book, the one you've
already been told is ful of serious errors? If so, why?
 
B

Ben Bacarisse

Keith Thompson said:
Since p was declared at file scope, it has static storage duration and
is therefore implicitly initialized to NULL. The loop body will never
execute.

I don't think we can know that. I am not much of a fan of puzzles but
maybe it will grab someone's interest. I have this source file:

#include "cat.h"

struct cat kat, *p;

int main(void)
{
for (p = p; p != NULL; p = p->next)
printf("%d\n", p->age);
return 0;
}

$ gcc -std=c89 -pedantic -o cat cat.c
$ ./cat
2
14
12

What is in cat.h?
 
T

Twixer Xev

On 06/11/2010 03:17 PM, Ben Bacarisse wrote:
[...]
I don't think we can know that. I am not much of a fan of puzzles but
maybe it will grab someone's interest. I have this source file:

#include "cat.h"

struct cat kat, *p;

int main(void)
{
for (p = p; p != NULL; p = p->next)
printf("%d\n", p->age);
return 0;
}

$ gcc -std=c89 -pedantic -o cat cat.c
$ ./cat
2
14
12

What is in cat.h?

Right. "cat.h" might have another definition of p which points to a
valid (or possibly malformed) linked list. That is what makes the
OP's program ambiguous.
 
B

Bill Cunningham

Keith Thompson wrote:

[...]
Are you still using examples from that same book, the one you've
already been told is ful of serious errors? If so, why?

I search the net for examples. Kandr2 in accordance to what I've read
doesn't have an example that I can understand anyway for linked lists. It
does the binary tree as per "Now is the time for all good men to come to the
aid of thier party" or something similar. I probably should've initialied to
NULL. A copy of c.h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct cat{
char *name;
char *color;
};

Bill
 
B

Bill Cunningham

Richard said:
Take a guess.

Linked lists are easy. K&R2 covers the subject in adequate detail.

Of course, if linked lists aren't working for you, you could try +=.
Or perhaps a preprocessor directive.

Richard could you tell me in kandr2 where linked lists are explained
because I can't see it.

Bill
 
B

Bill Cunningham

Richard said:
Take a guess.

Linked lists are easy. K&R2 covers the subject in adequate detail.

Of course, if linked lists aren't working for you, you could try +=.
Or perhaps a preprocessor directive.

Oh I haven't even got to preprocessor directives. I'd be more confused
than I am now.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Oh I haven't even got to preprocessor directives. I'd be more confused
than I am now.

Just in case you aren't already aware of it, Richard was commenting on
your tendency to try random things when you run into problems.
 
B

Bill Cunningham

Richard said:
Section 6.6.

With regards to your other reply, if you haven't yet covered easy easy
stuff like preprocessor directives, you probably want to cover those
first before getting onto complicated easy stuff like linked lists.

I will study the table code at §6.6 and maybe post what it says. I read
it and it didn't click right off.

Bill
 
B

Bill Cunningham

Keith said:
Just in case you aren't already aware of it, Richard was commenting on
your tendency to try random things when you run into problems.

Ok I'll try this that's not random. p 143 ¶ 3 §6.6.

for(hashval=0;*s!='\0';s++)

Why is *s not just s? This speaks of arrays. When I think of linked lists I
don't think of arrays except arrays of structs. I will work with nlist,
lookup, and install and try to figure them out. It might work. But do I
understand what I'm doing? I guess I'll see.

Bill
 
B

Bill Cunningham

#include <stdio.h>
#include <string.h>

struct nlist {
struct nlist *next;
char *name;
char *def;
};

static struct nlist *hashtab[101]; /* What's this? A declared array

to pointer? What's its purpose */

unsigned hash(char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; s++) /*Indent gives me an old style

error here */

hashval = *s + 31 * hasval;
return hashval % 101;
}

Another attempt.

Bill
 
C

Chad

Bill said:
    Is this a very simple example of a linked list
or is the concept I am
expressing in error again.

I don't know.
Here's a fairly simple example of a linked list:

/* BEGIN Lf_start.c */
/*
** Demonstration of use of long double list functions.
** Lf_start
** Lf_append_0
*/
#include <stdio.h>
#include <stdlib.h>

#define NUMBERS         {15,14,13,7,20,9,8,12,11,6}

#define NMEMB(A)        (sizeof (A) / sizeof *(A))

struct Lf_node {
    struct Lf_node      *next;
    long double          data;

};

typedef struct Lf_node Lf_type;

int Lf_fprintf(const Lf_type *node, FILE *stream);
Lf_type *Lf_start(long double data);
Lf_type *Lf_append_0(Lf_type *tail, long double data);
void Lf_free(Lf_type *node);

int main(void)
{
    Lf_type *head;
    Lf_type *tail;
    long double numbers[] = NUMBERS;
    long double *ptr = numbers;
    long double *const after = numbers + NMEMB(numbers);

    puts("/* BEGIN Lf_start.c output */");
    puts("\nOriginal order of list of long doubles:");
    head = tail = Lf_start(*ptr);
    if (head != NULL) {
        while (++ptr != after) {
            tail = Lf_append_0(tail, *ptr);
            if (tail == NULL) {
                puts("malloc trouble!");
                break;
            }
        }
    } else {
        puts("malloc trouble!");
    }
    Lf_fprintf(head, stdout);
    Lf_free(head);
    puts("\n/* END Lf_start.c output */");
    return 0;

}

int Lf_fprintf(const Lf_type *node, FILE *stream)
{
    int rc = 0;

    while (node != NULL) {
        if (0 > (rc = fprintf(stream, "%Lf\n", node -> data))) {
            break;
        }
        node = node -> next;
    }
    return rc;

}

Lf_type *Lf_start(long double data)
{
    Lf_type *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
        node -> next = NULL;
        node -> data = data;
    }
    return node;

}

Lf_type *Lf_append_0(Lf_type *tail, long double data)
{
    Lf_type *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
        node -> next = NULL;
        node -> data = data;
        tail -> next = node;
    }
    return node;

}

void Lf_free(Lf_type *node)
{
    Lf_type *next_node;

    while (node != NULL) {
        next_node = node -> next;
        free(node);
        node = next_node;
    }

}

/* END Lf_start.c */

Maybe I didn't look at your code close enough, but why, in


Lf_type *Lf_start(long double data)
{
Lf_type *node;

node = malloc(sizeof *node);
if (node != NULL) {
node -> next = NULL;
node -> data = data;
}
return node;

}
 
C

Chad

Maybe I didn't look at your code close enough, but why, in
Lf_type *Lf_start(long double data)
{
    Lf_type *node;

    node = malloc(sizeof *node);
    if (node != NULL) {
        node -> next = NULL;
        node -> data = data;
    }
    return node;

}

Disregard that partial reply. I started to ask the question, but then
I decided to think about it, instead of just asking. And now, for
whatever reasons, the post got posted instead of discarded.
 

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,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top