Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
linked list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Bill Cunningham  
View profile  
 More options Sep 6 2012, 5:58 pm
Newsgroups: comp.programming
From: "Bill Cunningham" <nos...@nspam.invalid>
Date: Thu, 6 Sep 2012 17:58:06 -0400
Local: Thurs, Sep 6 2012 5:58 pm
Subject: linked list
    Can someone show me a very simple example of a linked list in C? The
simplest possible.

Bill


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Cunningham  
View profile  
 More options Sep 6 2012, 6:04 pm
Newsgroups: comp.programming
From: "Bill Cunningham" <nos...@nspam.invalid>
Date: Thu, 6 Sep 2012 18:04:21 -0400
Local: Thurs, Sep 6 2012 6:04 pm
Subject: Re: linked list

I have this

struct list{
    int number=1;
    struct list *next;

};

I only want to go to the next page right now.

Bill


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pete  
View profile  
 More options Sep 6 2012, 6:22 pm
Newsgroups: comp.programming
From: pete <pfil...@mindspring.com>
Date: Thu, 06 Sep 2012 18:22:26 -0400
Local: Thurs, Sep 6 2012 6:22 pm
Subject: Re: linked list

Bill Cunningham wrote:

>     Can someone show me a very simple example
> of a linked list in C?  The simplest possible.

/* BEGIN d_append.c */
/*
** Demonstration of use of int list functions.
*/
#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 d_node {
    struct d_node       *next;
    int                  data;

};

typedef struct d_node d_type;

int d_fprintf(const d_type *node, FILE *stream);
d_type *d_append(d_type **head, d_type *tail, int data);
void d_free(d_type *node);

int
main(void)
{
    d_type *head = NULL;
    d_type *tail = NULL;
    int numbers[] = {NUMBERS};
    int *ptr = numbers;
    int *const after = numbers + NMEMB(numbers);

    puts("/* BEGIN d_append.c output */");
    puts("\nOriginal order of list of ints:");
    do {
        tail = d_append(&head, tail, *ptr);
        if (tail == NULL) {
            puts("malloc trouble!");
            break;
        }
    } while (++ptr != after);
    d_fprintf(head, stdout);
    d_free(head);
    puts("\n/* END d_append.c output */");
    return 0;

}

int
d_fprintf(const d_type *node, FILE *stream)
{
    int rc = 0;

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

}

d_type *
d_append(d_type **head, d_type *tail, int data)
{
    d_type *node;

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

}

void
d_free(d_type *node)
{
    d_type *next_node;

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

}

/* END d_append.c */

--
pete


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Cunningham  
View profile  
 More options Sep 6 2012, 6:28 pm
Newsgroups: comp.programming
From: "Bill Cunningham" <nos...@nspam.invalid>
Date: Thu, 6 Sep 2012 18:28:48 -0400
Local: Thurs, Sep 6 2012 6:28 pm
Subject: Re: linked list

    Wow if this is simple I'd hate to see complex. Whew. One step at a time
I guess.

> int d_fprintf(const d_type *node, FILE *stream);
> d_type *d_append(d_type **head, d_type *tail, int data);

    I've never worked with pointers to pointers. Except in the command line.
What's head and tail for? I only used next. I don't want to try interation
and reversing functions yet.

[...]

Bill


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pete  
View profile  
 More options Sep 6 2012, 8:52 pm
Newsgroups: comp.programming
From: pete <pfil...@mindspring.com>
Date: Thu, 06 Sep 2012 20:52:16 -0400
Local: Thurs, Sep 6 2012 8:52 pm
Subject: Re: linked list

head points to the first node of the linked list.
tail points to the last node of the linked list.

>I only used next.

What can you do with a linked list,
by only using next?

I simplified it:

/* BEGIN d_start.c */
/*
** Demonstration of use of int list functions.
** d_start
** d_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 d_node {
    struct d_node       *next;
    int                 data;

};

int d_fprintf(const struct d_node *node, FILE *stream);
struct d_node *d_start(int data);
struct d_node *d_append_0(struct d_node *tail, int data);
void d_free(struct d_node *node);

int
main(void)
{
    struct d_node *head;
    struct d_node *tail;
    int numbers[] = {NUMBERS};
    int *ptr = numbers;
    int *const after = numbers + NMEMB(numbers);

    puts("/* BEGIN d_start.c output */");
    puts("\nOriginal order of list of ints:");
    head = tail = d_start(*ptr);
    if (head != NULL) {
        while (++ptr != after) {
            tail = d_append_0(tail, *ptr);
            if (tail == NULL) {
                puts("malloc trouble!");
                break;
            }
        }
    } else {
        puts("malloc trouble!");
    }
    d_fprintf(head, stdout);
    d_free(head);
    puts("\n/* END d_start.c output */");
    return 0;

}

int
d_fprintf(const struct d_node *node, FILE *stream)
{
    int rc = 0;

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

}

struct d_node *
d_start(int data)
{
    struct d_node *node;

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

}

struct d_node *
d_append_0(struct d_node *tail, int data)
{
    struct d_node *node;

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

}

void
d_free(struct d_node *node)
{
    struct d_node *next_node;

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

}

/* END d_start.c */

--
pete


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Cunningham  
View profile  
 More options Sep 7 2012, 5:25 pm
Newsgroups: comp.programming
From: "Bill Cunningham" <nos...@nspam.invalid>
Date: Fri, 7 Sep 2012 17:25:54 -0400
Local: Fri, Sep 7 2012 5:25 pm
Subject: Re: linked list

    Thanks much for your help Pete. But maybe I should stay away from linked
lists for now. I thought that they might be simpler than a binary tree but
my C is not up to par enough to handle such things IMO. But I will save your
code and I will study it in my own time. Do I have your permission to repost
this code giving you credit of course if I have questions later with other
about linked lists? This is kind of a slow list anyway so I might study and
repost it here.

Sincerely
Bill


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pete  
View profile  
 More options Sep 8 2012, 9:35 am
Newsgroups: comp.programming
From: pete <pfil...@mindspring.com>
Date: Sat, 08 Sep 2012 09:35:20 -0400
Local: Sat, Sep 8 2012 9:35 am
Subject: Re: linked list

Bill Cunningham wrote:
> Do I have your permission to repost
> this code giving you credit of course
> if I have questions later with other
> about linked lists?

Yes.

All that that last program does
is to declare a list node type

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

and then to make a linked list by using two functions.

    struct d_node *d_start(int data);
    struct d_node *d_append_0(struct d_node *tail, int data);

and then to display the list data using one function

    int d_fprintf(const struct d_node *node, FILE *stream);

and then to deallocate the list using one function

    void d_free(struct d_node *node);

--
pete


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pete  
View profile  
 More options Sep 8 2012, 2:49 pm
Newsgroups: comp.programming
From: pete <pfil...@mindspring.com>
Date: Sat, 08 Sep 2012 14:49:00 -0400
Subject: Re: linked list

Bill Cunningham wrote:

>     Can someone show me a very simple example
> of a linked list in C?
> The simplest possible.

This is as simple as I could make it,
without getting hung up on the meaning of the word "simple".

/* BEGIN list_linked.c */

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

int
main(void)
{
    struct list {
        struct list         *next;
        int                  data;
    };
    struct list *head;
    struct list *tail;
    int number[] = {15,14,13,7,20,9,8,12,11,6};
    unsigned index;

    puts("/* BEGIN list_linked.c output */\n");
    puts("Original order of list of int:");    
    head = malloc(sizeof *head);
    tail = head;
    if (tail != NULL) {
        index = 0;
        tail -> data = number[index];
        while (sizeof number / sizeof *number > ++index) {
            tail -> next = malloc(sizeof *(tail -> next));
            if (tail -> next == NULL) {
                puts("malloc trouble!");
                break;
            }
            tail = tail -> next;
            tail -> data = number[index];
        }
        tail -> next = NULL;
    } else {
        puts("malloc trouble!");
    }
    for (tail = head; tail != NULL; tail = tail -> next) {
        printf("%d\n", tail -> data);
    }
    while (head != NULL) {
        tail = head -> next;
        free(head);
        head = tail;
    }
    puts("\n/* END list_linked.c output */");
    return 0;

}

/* END list_linked.c */

--
pete


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal J. Bourguignon  
View profile  
 More options Oct 5 2012, 9:32 pm
Newsgroups: comp.programming
From: Pascal J. Bourguignon <p...@informatimago.com>
Date: 6 Oct 2012 01:32:57 GMT
Local: Fri, Oct 5 2012 9:32 pm
Subject: Re: linked list
"Bill Cunningham" <nos...@nspam.invalid> wrote:
> Can someone show me a very simple example of a linked list in C? The
> simplest possible.

> Bill

 Perhaps you'll find this version simplier, but it's essentially the same:

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

typedef struct pair { int car; struct pair* cdr; } pair;

pair* cons(int a,pair* d){
  pair* c=malloc(sizeof(pair));
  if(c){ c->car=a; c->cdr=d; }
  return(c);}

pair* cdr(pair* c){ return(c?c->cdr:c); }
int   car(pair* c){ return(c?c->car:0); }

typedef int (*ifun)(int,int);

int reduce(ifun fun,pair* list,int initial){
   return(list
           ? reduce(fun,cdr(list),fun(car(list),initial))
           : initial);}

int plus(int a,int b){ return(a+b); }

int main(){
   printf("%d\n",reduce(plus,cons(1,cons(2,cons(3,NULL))),0));
   return(0);

}

--
__Pascal J. Bourguignon__

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »