Help with singly link list....

This is a discussion on Help with singly link list.... within the C Programming forums, part of the General Programming Boards category; I have researched all over the internet yet I cannot figure out why this code doesn't work. Any suggestions and ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Help with singly link list....

    I have researched all over the internet yet I cannot figure out why this code doesn't work. Any suggestions and instructions on how to fix the problems will be greatly appreciated.

    Code:
    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <getopt.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    typedef enum
    {
       FALSE = 0,
       TRUE = 1
    } Bool;
    
    struct node
    {
       char *x;
       struct node *next;
    };
    
    struct node *tptr = NULL, *cptr = NULL;
    void addNode(char *input);
    
    int main()
    {
       ssize_t readReturn = 0;
       size_t bufLen = 0;
       char *input;
       int x = 0;
       Bool stillGoing = TRUE;
    
       while(stillGoing)
       {
         printf("enter text: ");
    
         readReturn = getline(&input, &bufLen, stdin);
    
         for(x = 0; x < strlen(input) + 1; ++x)
         {
            if(input[x] == '\n')
            {
               input[x] = '\0';
            }
         }
    
         if((strncmp(input,"exit",4)) == 0)
         {
            stillGoing = FALSE;
            break;
         }
         addNode(input);
    
       }
    
       if(tptr!=NULL)
       {
          for(cptr = tptr; cptr != NULL; cptr = cptr->next)
          printf("%s\n", cptr->x);
       }
       /*for some reason tptr is ALWAYS null at this point*/
       else
          printf("something isn't right\n");
       
       /*i know these frees arent complete i have yet to code this part*/
       free(cptr);
       free(tptr);
       free(input);
       return 0;
    }
    
    void addNode(char *input)
    {
       struct node *nptr;
       cptr = tptr;
    
       if ((nptr = malloc(sizeof(struct node))) != NULL)
       {
          nptr->x = input;
          nptr->next = NULL;
    
          if(tptr == NULL)
          {
             tptr = nptr;
          }
          else
          {
             while(cptr->next != NULL)
             {
                cptr = cptr->next;
             }
             cptr->next = malloc(sizeof(struct node));
             cptr->next = nptr;
          }
       }
       else
       {
          fprintf(stderr, "%s", "Not enough memory.\n");
          exit(EXIT_FAILURE);
       }
    }
    thanks for your help.

    **edit
    i fixed the tptr always being null problem. I accidently made tptr and cptr local variables. But im getting the output:

    exit
    exit

    when it tries to list the contents of the list.
    Last edited by LightsOut06; 10-09-2005 at 07:54 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Dynamically Allocating a 2D Array.
    Amount of prior knowledge transferred - 0%

    Sorry, I can't be bothered to spend another 20 minutes writing a bunch of words explaining the issues (yes, you have the whole bloody subscription there) you have with your code just for you to blindly ignore it all again.

    Guess your nickname finally caught up with you - turn off the light and put down the compiler.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    im really trying man . I gave up on the 2d array and im now trying the link list. You're right, im having the same problems though. There is something pretty major that i just dont grasp. ive been working on this stuff for like 12 straight hours.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Just a Hint. You are not allocating any space for input in main. ( declare in e.g. char input[255]; )
    the input should then be
    Code:
        readReturn = getline(input, 255, stdin);
    Kurt

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    ^i think getline automatically does that.

    However I think I fixed it. Thanks to Salem. I looked at his last post in my last thread and he said to make input = NULL before you loop again. That's what I did and it's working now. Thanks alot Salem. Getline has been the butt of all my problems in this class thus far . This is the new code:

    Code:
    #define _GNU_SOURCE 1
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <getopt.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    typedef enum
    {
       FALSE = 0,
       TRUE = 1
    } Bool;
    
    struct node
    {
       char *x;
       struct node *next;
    };
    
    struct node *tptr = NULL, *cptr = NULL;
    void addNode(char *input);
    
    int main()
    {
       ssize_t readReturn = 0;
       size_t bufLen = 0;
       char *input;
       int x = 0;
       Bool stillGoing = TRUE;
    
       while(stillGoing)
       {
         printf("enter text: ");
    
         readReturn = getline(&input, &bufLen, stdin);
    
         for(x = 0; x < strlen(input) + 1; ++x)
         {
            if(input[x] == '\n')
            {
               input[x] = '\0';
            }
         }
    
         if((strncmp(input,"exit",4)) == 0)
         {
            stillGoing = FALSE;
            break;
         }
         addNode(input);
         input = NULL;
       }
    
       if(tptr!=NULL)
       {
          for(cptr = tptr; cptr != NULL; cptr = cptr->next)
          printf("%s\n", cptr->x);
       }
       else
          printf("your prog is ........ed up\n");
    
       free(cptr);
       free(tptr);
       free(input);
       return 0;
    }
    
    void addNode(char *input)
    {
       struct node *nptr;
       cptr = tptr;
    
       if ((nptr = malloc(sizeof(struct node))) != NULL)
       {
          nptr->x = input;
          nptr->next = NULL;
    
          if(tptr == NULL)
          {
             tptr = nptr;
          }
          else
          {
             while(cptr->next != NULL)
             {
                cptr = cptr->next;
             }
             cptr->next = malloc(sizeof(struct node));
             cptr->next = nptr;
          }
       }
       else
       {
          fprintf(stderr, "%s", "Not enough memory.\n");
          exit(EXIT_FAILURE);
       }
    }
    i guess i have to learn to make input = NULL after im done with it.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't you need <stdbool.h>?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Linked List, Please Help!
    By CodeMonkeyZ in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 05:23 AM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  4. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21