Thread: Needing help with linked lists

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    16

    Needing help with linked lists

    I am reviewing material for my c final and have a couple of questions, for example i did this program below to add 1 and 2 to nodes and the print them, if i have them in main, they print if i try to make a function do it, it doesnt print anything, can anyone point to me why it doesnt?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    struct node
    {
        int data;
        struct node *next;
    };
    
    void traverse();
    struct node *head;
    struct node *tail;
    struct node *p;
    int main()
    {
        struct node *head = '\0';
        struct node *tail = '\0';
        struct node *p;
        head = malloc(sizeof(struct node));
        tail = malloc(sizeof(struct node));
        
        
        head -> data = 1;
        head -> next = tail;
        tail -> data = 2;
        tail ->next = '\0';
        traverse();
        
        while(head)
        {
                 printf("%d\n",head ->data);
                 head=head ->next;
        }
        
        system("pause");
    }
    
    void traverse()
    {
         struct node *p;
         p=head;
         while(p)
         {
                  printf("%d\n",p ->data);
                  p=p ->next;
         }
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    nevermind i figured it out, had it as a local and not global

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You have head, tail, and p defined both globally and in main. The beginner's rule for when to use globals is "never, unless told otherwise". They should just be in main and head needs to be passed to your function. And get rid of the loop in main since it's now in traverse.

    Also, this is incorrect:
    struct node *head = '\0';
    It may work on most machines, but should be
    struct node *head = NULL;
    Same for tail and next.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    that's actually how i learned how to do it and not NULL and i have noticed that everyone does it the way you mentioned bu thanks for the tip, i noticed that i have them as local instead of global that was why the function didnt "see" them.

    Got another question, if i wanted to find out how many nodes i have in my program?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > Got another question, if i wanted to find out how many nodes i have in my program?
    Write another traverse(), which counts instead of prints.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Now that I think about it, using '\0' will work on all machines since it's really just a constant integer 0 and that will be translated into the proper null-pointer data for whatever type by the compiler.

    However, it's still incorrect since it signifies the wrong thing. Basically there's 4 kinds of "zero" in C.
    Code:
    0	integer zero (also 0U, 0L, 0UL)
    0.0	double floating point zero (or 0.0f for float zero)
    '\0'	ascii-null
    NULL	pointer-null (usually defined as either 0 or (void*)0)
    They're interchangeable (for the most part) but signify different concepts to the programmer.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM