Link lists..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnnash

    Link lists..

    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(n ode));

    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;

    }

    }


  • Mark Bluemel

    #2
    Re: Link lists..

    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?

    Comment

    • Joachim Schmitz

      #3
      Re: Link lists..

      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(n ode));
      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;
      >
      }
      >
      }

      Comment

      • johnnash

        #4
        Re: Link lists..

        On Feb 29, 4:58 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-digital.de>
        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(n ode));
        >
        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

        Comment

        • Joachim Schmitz

          #5
          Re: Link lists..

          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(n ode));
          Loose the cast, #include <stdlib.h>,us e 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


          Comment

          • Robbie Hatley

            #6
            Re: Link lists..


            "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(n ode));
            >
            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(m y_node_t));
            pNode->data = 42;
            my_list_push(Ar gle, pNode);

            pNode = malloc(sizeof(m y_node_t));
            pNode->data = 17;
            my_list_push(Ar gle, pNode);

            pNode = malloc(sizeof(m y_node_t));
            pNode->data = 98;
            my_list_push(Ar gle, 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


            Comment

            Working...