Link lists..

Discussion in 'C Programming' started by johnnash, Feb 29, 2008.

  1. johnnash

    johnnash Guest

    i'm declaring a data structure for link list of integers in A.h

    #ifndef A_H (can anyone please explain how ifndef works as well..i
    just seem to see it in almost every program)

    #define A_H

    typedef struct node nodestruct

    {

    int data;

    struct node *next;

    }node;

    extern node * head /*indicates head node of the list , declaring as
    extern so that it can be used in A.c */



    Now i want to use this link list so..A.c


    #include "A.h"

    node * head; /*definition, is this sufficient for link list ?, can i
    initialize head to NULL here itself */

    LL()/* computes the link list */
    {

    node *p, *prev; /*prev is previous node */

    head = NULL;

    head->next = NULL;

    for(i=0;i<10;i++) /*Creating list of 10 elements */

    {

    p = (node *) malloc(sizeof(node));

    p->next = NULL;

    scanf("%d",&(p->data));

    if(head ==NULL)

    head = p;

    else
    {
    prev->next = p;

    prev = p;
    }

    }


    main()

    {

    struct node *p;

    LL();

    p = head;

    while(p!= NULL)
    {
    printf("%d\n",p->data);

    p = p->next;

    }

    }
     
    johnnash, Feb 29, 2008
    #1
    1. Advertising

  2. johnnash

    Mark Bluemel Guest

    johnnash wrote:
    > i'm declaring a data structure for link list of integers in A.h
    >
    > #ifndef A_H (can anyone please explain how ifndef works as well..i
    > just seem to see it in almost every program)


    Doesn't your text book or tutorial explain this?

    If they don't help, simply Googling for "ifndef" gave me loads of
    hits...

    The pattern

    #ifndef something
    #define something
    ....
    ....
    #endif

    is a common technique for handling multiple inclusions of headers.

    [snip]

    > node * head; /*definition, is this sufficient for link list ?, can i
    > initialize head to NULL here itself */
    >
    > LL()/* computes the link list */
    > {
    >
    > node *p, *prev; /*prev is previous node */
    >
    > head = NULL;
    >
    > head->next = NULL;


    Bang! you've just tried to dereference a NULL Pointer...

    [snip]

    What was your actual question?
     
    Mark Bluemel, Feb 29, 2008
    #2
    1. Advertising

  3. johnnash wrote:
    > i'm declaring a data structure for link list of integers in A.h
    >
    > #ifndef A_H (can anyone please explain how ifndef works as well..i
    > just seem to see it in almost every program)
    >
    > #define A_H
    >
    > typedef struct node nodestruct
    >
    > {
    >
    > int data;
    >
    > struct node *next;
    >
    > }node;
    >
    > extern node * head /*indicates head node of the list , declaring as
    > extern so that it can be used in A.c */
    >
    >
    >
    > Now i want to use this link list so..A.c
    >
    >
    > #include "A.h"
    >
    > node * head; /*definition, is this sufficient for link list ?, can i
    > initialize head to NULL here itself */
    >
    > LL()/* computes the link list */
    > {
    >
    > node *p, *prev; /*prev is previous node */
    >
    > head = NULL;
    >
    > head->next = NULL;

    Swap these 2 lines, otherwise you derefference a NULL pointer


    > for(i=0;i<10;i++) /*Creating list of 10 elements */
    >
    > {
    >
    > p = (node *) malloc(sizeof(node));

    lose the cast. Instead #include <stdlib.h>

    > p->next = NULL;

    check p first. malloc() can fail, if so you derefferent a NULL pointer here

    >
    > scanf("%d",&(p->data));

    Avoid scanf. At least consider what happens to the <return>

    > if(head ==NULL)
    >
    > head = p;
    >
    > else
    > {
    > prev->next = p;
    >
    > prev = p;
    > }
    >
    > }
    >
    >
    > main()
    >
    > {
    >
    > struct node *p;
    >
    > LL();
    >
    > p = head;
    >
    > while(p!= NULL)
    > {
    > printf("%d\n",p->data);
    >
    > p = p->next;
    >
    > }
    >
    > }
     
    Joachim Schmitz, Feb 29, 2008
    #3
  4. johnnash

    johnnash Guest

    On Feb 29, 4:58 pm, "Joachim Schmitz" <>
    wrote:
    > johnnash wrote:
    > > i'm declaring a data structure for link list of integers in A.h

    >
    > > #ifndef A_H (can anyone please explain how ifndef works as well..i
    > > just seem to see it in almost every program)

    >
    > > #define A_H

    >
    > > typedef struct node nodestruct

    >
    > > {

    >
    > > int data;

    >
    > > struct node *next;

    >
    > > }node;

    >
    > > extern node * head /*indicates head node of the list , declaring as
    > > extern so that it can be used in A.c */

    >
    > > Now i want to use this link list so..A.c

    >
    > > #include "A.h"

    >
    > > node * head; /*definition, is this sufficient for link list ?, can i
    > > initialize head to NULL here itself */

    >
    > > LL()/* computes the link list */
    > > {

    >
    > > node *p, *prev; /*prev is previous node */

    >
    > > head = NULL;

    >
    > > head->next = NULL;

    >
    > Swap these 2 lines, otherwise you derefference a NULL pointer
    >
    > > for(i=0;i<10;i++) /*Creating list of 10 elements */

    >
    > > {

    >
    > > p = (node *) malloc(sizeof(node));

    >
    > lose the cast. Instead #include <stdlib.h>
    >
    > > p->next = NULL;

    >
    > check p first. malloc() can fail, if so you derefferent a NULL pointer here
    >
    >
    >
    > > scanf("%d",&(p->data));

    >
    > Avoid scanf. At least consider what happens to the <return>
    >
    > > if(head ==NULL)

    >
    > > head = p;

    >
    > > else
    > > {
    > > prev->next = p;

    >
    > > prev = p;
    > > }

    >
    > > }

    >
    > > main()

    >
    > > {

    >
    > > struct node *p;

    >
    > > LL();

    >
    > > p = head;

    >
    > > while(p!= NULL)
    > > {
    > > printf("%d\n",p->data);

    >
    > > p = p->next;

    >
    > > }

    >
    > > }



    yeah I admit head->next = NULL was actually mistyping.

    why should i return something here ?

    also can you please tell me the scope of this link list ? If it is
    declared as extern does it mean that its scope is the entire program
    or the values are immediately destroyed after ll() ends
     
    johnnash, Feb 29, 2008
    #4
  5. johnnash wrote:
    > i'm declaring a data structure for link list of integers in A.h
    >
    > #ifndef A_H (can anyone please explain how ifndef works as well..i
    > just seem to see it in almost every program)

    Mark Bluemel explained that already

    > #define A_H
    >
    > typedef struct node nodestruct

    wrong syntax

    > {
    > int data;
    > struct node *next;
    > }node;
    >
    > extern node * head /*indicates head node of the list , declaring as
    > extern so that it can be used in A.c */

    No, but so that it can be used in any module that #include's this file
    without being defined there

    > Now i want to use this link list so..A.c
    >
    >
    > #include "A.h"
    >
    > node * head; /*definition, is this sufficient for link list ?, can i
    > initialize head to NULL here itself */

    This is done for you automagically

    > LL()/* computes the link list */
    > {
    > node *p, *prev; /*prev is previous node */
    >
    > head = NULL;
    > head->next = NULL;

    Wrong way round and without checking head first, still won't work as NULL
    isn't knwon here, due to lack of #include the relevant file

    > for(i=0;i<10;i++) /*Creating list of 10 elements */

    i is indefined

    > {
    > p = (node *) malloc(sizeof(node));

    Loose the cast, #include <stdlib.h>,use sizeof *p, check the result

    > p->next = NULL;
    >
    > scanf("%d",&(p->data));

    varadic function without ptototyp -> UB. Use #include <stdio.h>, better
    don't use scanf anyway

    > if(head ==NULL)
    > head = p;
    > else
    > {
    > prev->next = p;
    > prev = p;

    ??? so now prev->next == NULL, as p->next == NULL !

    > }
    > }

    } missing, probably due to poor indentation style

    >
    > main()

    int main(void)

    > {
    > struct node *p;

    why struct here, it's typedef'd

    >
    > LL();
    >
    > p = head;
    >
    > while(p!= NULL)
    > {
    > printf("%d\n",p->data);

    varadic funtion without prototype -> UB

    > p = p->next;

    free whatever you malloc'd when you're done with it

    > }

    return 0;

    > }

    Please post something that at least compiles. Idealy without compiler
    warnings even if warning level is set at it's highest.

    Bye, Jojo
     
    Joachim Schmitz, Feb 29, 2008
    #5
  6. "johnnash" wrote:

    > i'm declaring a data structure for link list of integers in A.h
    >
    > #ifndef A_H (can anyone please explain how ifndef works as well..i
    > just seem to see it in almost every program)
    >
    > #define A_H
    >
    > typedef struct node nodestruct
    >
    > {
    >
    > int data;
    >
    > struct node *next;
    >
    > }node;
    >
    > extern node * head /*indicates head node of the list , declaring as
    > extern so that it can be used in A.c */
    >
    > Now i want to use this link list so..A.c
    >
    > #include "A.h"
    >
    > node * head; /*definition, is this sufficient for link list ?, can i
    > initialize head to NULL here itself */
    >
    > LL()/* computes the link list */
    > {
    >
    > node *p, *prev; /*prev is previous node */
    >
    > head = NULL;
    >
    > head->next = NULL;
    >
    > for(i=0;i<10;i++) /*Creating list of 10 elements */
    >
    > {
    >
    > p = (node *) malloc(sizeof(node));
    >
    > p->next = NULL;
    >
    > scanf("%d",&(p->data));
    >
    > if(head ==NULL)
    >
    > head = p;
    >
    > else
    > {
    > prev->next = p;
    >
    > prev = p;
    > }
    > }
    >
    >
    > main()
    >
    > {
    >
    > struct node *p;
    >
    > LL();
    >
    > p = head;
    >
    > while(p!= NULL)
    > {
    > printf("%d\n",p->data);
    >
    > p = p->next;
    >
    > }
    >
    > }



    Ewww.

    Chock full o' errors.

    A "linked list" is a kind of a thing, no? So start by defining
    the kind of thing you want. But since your list will consist of
    nodes, you need to define the concept of "node" first:

    typedef struct my_node_tag
    {
    struct my_node_tag *pNext;
    /*struct my_node_tag *pPrev; (optional) */
    int data;
    } my_node_t;

    The list will be a chain of these nodes, with a head and a tail:

    typedef struct my_list_tag
    {
    my_node_t *pHead;
    my_node_t *pTail;
    } my_list_t;

    Now define a way to paste a node to the end of the list:

    int my_list_push (my_list *pList, my_node_t *pNode)
    {
    // Bail out if either input pointer is NULL:
    if (!pList || !pNode)
    {
    return 666;
    }

    // If list is empty, new node becomes new head and tail:
    if (!pList->pHead)
    {
    pNode->pNext = NULL; /* new node doesn't have next yet */
    pList->pHead = pNode; /* new node becomes new head */
    pList->pTail = pNode; /* new node becomes new tail */
    }
    // Otherwise, stick it on the end:
    else
    {
    pNode->pNext = NULL; /* new node doesn't have next yet */
    pList->pTail->pNext = pNode; /* old tail's next -> new node */
    pList->pTail = pNode; /* new node becomes new tail */
    }
    }

    You can test this out in main:

    int main(void)
    {
    my_list_t Argle;
    my_node_t *pNode;

    pNode = malloc(sizeof(my_node_t));
    pNode->data = 42;
    my_list_push(Argle, pNode);

    pNode = malloc(sizeof(my_node_t));
    pNode->data = 17;
    my_list_push(Argle, pNode);

    pNode = malloc(sizeof(my_node_t));
    pNode->data = 98;
    my_list_push(Argle, pNode);

    for ( pNode = Argle.pHead ; pNode ; pNode = pNode->pNext )
    {
    printf("%d\n", pNode->data);
    }

    return 0;
    }

    Should print:
    42
    17
    98

    (Caveat: All completely untested, off the top of my head. Probably
    has bugs. You'll have to debug it yourself, I don't have time right
    now, gotta run. Hope this helps, though.)

    --
    Cheers,
    Robbie Hatley
    lonewolf aatt well dott com
    www dott well dott com slant user slant lonewolf slant
     
    Robbie Hatley, Mar 1, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    949
    Kevin Spencer
    May 17, 2005
  2. JustSomeGuy

    Sorting lists of lists...

    JustSomeGuy, Jun 17, 2004, in forum: C++
    Replies:
    0
    Views:
    342
    JustSomeGuy
    Jun 17, 2004
  3. Jon Slaughter

    lists of lists

    Jon Slaughter, Dec 13, 2004, in forum: C++
    Replies:
    4
    Views:
    453
    Buster
    Dec 13, 2004
  4. Charlotte Henkle

    Counter for items in lists in lists?

    Charlotte Henkle, Sep 25, 2004, in forum: Python
    Replies:
    8
    Views:
    418
    Charlotte Henkle
    Sep 26, 2004
  5. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    442
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
Loading...

Share This Page