Thread: linked list with add on, please help

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    linked list with add on, please help

    ok, so I have to do a linked list having the user enter 5 numbers and then asking them to add in one more value. I have the linked list, but cannot figure out how get it ask for one more value. Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXNUMS 6
    #define DEBUG 0
    
    
    /*Here is the declaration of a stack structure*/
    
    
    struct numRec
    {
           char value[MAXNUMS];
           struct numRec *nextAddr;
    };
    
    
    /*here is the definition of the top of stack pointer*/
    struct numRec *firstRec;
    
    
    int main()
    {
        void readInsert(); /*function prototype*/
        void display();
        
        firstRec = NULL; /*initialize the top of stack pointer*/
        readInsert();
        display();
        
        return 0;
    }
    /*get a number and insert it into the stacked list */
    void readInsert()
    {
         char value[MAXNUMS];
         void insert(char *);
         
         printf("Enter 5 numbers, one per line");
         printf("\nTo stop entering numbers, enter a single x\n");
         while (1)
         {
               printf("Enter a number: ");
               gets(value);
               if (strcmp(value,"x") == 0)
               break;
               insert(value);
               }
               }
         
    void insert(char *value)
         {
              struct numRec *linearlocate(char *);
              struct numRec *newaddr, *here; /* pointer to structure of type numRec*/
                        
              newaddr = (struct numRec *) malloc(sizeof(struct numRec));
              if (newaddr == (struct numRec *) NULL)
              {
                          printf("\nFailed to allocate memory for this structure\n");
                          exit(1);
                          }
              if (firstRec == NULL)
              {
                           newaddr->nextAddr = NULL;
                           firstRec = newaddr;
                           }
              else if (strcmp(value, firstRec->value)<0)
              {
                   newaddr->nextAddr = firstRec;
                   firstRec = newaddr;
                   }
              else
              {
                  here = linearlocate (value);
                  newaddr->nextAddr = here->nextAddr;
                  here->nextAddr = newaddr;
                  }
                  
              strcpy(newaddr->value,value); /*store the number*/
              }
              
              struct numRec *linearlocate (char *value)
              {
                     struct numRec *one, *two;
                     one = firstRec;
                     two = one->nextAddr;
                     
                     if(two == NULL)
                     return(one);
              while(1)
              {
                      if(strcmp(value, two->value) < 0)
                      break;
                      else if(two->nextAddr == NULL)
                      {
                           one = two;
                           break;
                           }
                      else
                      {
                          one = two;
                          two = one -> nextAddr;
                          }
                          }
                          return(one);
                          }
              void display()
              {
                   struct numRec *contents;
                   
                   contents = firstRec;
                   printf("\nThe numbers currently in the list are:");
                   while(contents != NULL)
                   {
                                  printf("%s\n", contents->value);
                                  contents = contents->nextAddr;
                                  }
                                  
                
        fflush(stdin); /* clear input area so you can pause */
        getchar(); /* force the computer to pause until you press a key on the keyboard */
    }

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    SourceForge.net: cpwiki
    Read about
    - not using gets()
    - not using fflush(stdin)
    - the value of good indentation.
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    I am brand new at this and am only using what the text book and the teacher said to use. I understand it's not perfect, I'm just trying to understand the code. I'll make it pretty later.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I strongly recommend using a C Compiler instead of a C++ Compiler [for compiling C code].
    Or, if you used a C Compiler read about casting malloc FAQ > Casting malloc - Cprogramming.com and casting the value of NULL should not be needed in C.

    Tim S.
    Last edited by stahta01; 06-03-2012 at 05:06 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  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
    > I'll make it pretty later.
    Well you're going to be on your own then.
    If you simply can't be bothered with presentation when asking for help, then don't be disappointed when no help arrives.
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by vhj6 View Post
    I'll make it pretty later.
    "Pretty"?!? Who said anything about making it pretty?

    We merely ask you to wash the dog s**t off it first!

    No professional programmer ever makes code look like that, not even while they are just messing around with an idea. It is properly formatted (or pretty darn close to it) always. You put code like that in front of me, and I aint gonna touch it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring linked list inside linked list
    By blueboyz in forum C Programming
    Replies: 33
    Last Post: 04-20-2012, 10:13 AM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  4. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM
  5. Replies: 6
    Last Post: 03-02-2005, 02:45 AM