Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > linked list example

Reply
Thread Tools

linked list example

 
 
Twixer Xev
Guest
Posts: n/a
 
      06-11-2010
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.
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-12-2010
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


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-12-2010
Richard Heathfield wrote:

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-12-2010
Richard Heathfield wrote:

> 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


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-12-2010
"Bill Cunningham" <> writes:
> Richard Heathfield wrote:
>> 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.


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.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-13-2010
Richard Heathfield wrote:

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-13-2010
Keith Thompson wrote:

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-13-2010
#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


 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-13-2010
On Jun 12, 4:23*pm, pete <pfil...@mindspring.com> wrote:
> Bill Cunningham wrote:
>
> > * * 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;

}
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      06-13-2010
> 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.
 
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
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 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