Thread: Linked Lists - Driving me insane

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Linked Lists - Driving me insane

    Hello all,

    I am writing a function for a program which takes the first number from a linked list and uses it elsewhere in the program. It is meant to take the start pointer and if that number is the last number in the list, generate 10 more numbers.

    But, it seems to take the LAST pointer from the list and it does not remove it from the list properly because the next time a number is needed it is the same one. The code is as follows:

    Code:
    int removeFromList(NODE *startPtr)
    {  
       int number, a;
       NODE *currentPtr,*previousPtr;
    
       previousPtr = currentPtr = startPtr;
       currentPtr = startPtr->nextPtr;
    
       if(currentPtr->nextPtr == NULL)
       {
    
         /* generate more numbers */
    
          number = currentPtr->number; 
          for(a = 1; a <= 10; a++)
          {
              number = (number + 11) DIV 10;
              number = createNewNumber(number);
              addToList(currentPtr, number);
          }
       }
    
       else
       {
    
         /* I think the problem is here */
    
          while(currentPtr->nextPtr != NULL)
          {
            previousPtr = currentPtr;
            currentPtr = currentPtr->nextPtr;
          }
          number = currentPtr->number;
          previousPtr->nextPtr = NULL;
       }
      return number;
    }


    The 10 new numbers are generated successfully when required. The problem is just reading the first number and then removing it from the list.

    *** Thanks for your help. ***

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    The removeFromList function is used to get the next number from a linked list and use it as an ID number. It automatically assigns this ID number and the user then enteres their name. The numbers have been added to the linked list by reading a file and getting the numbers from that.

    The initialiseList and addToList functions are as follows:

    Code:
    NODE *initialiseList()
    {
       NODE *startPtr;
       startPtr = (NODE *)malloc(sizeof( NODE));
       startPtr -> nextPtr = NULL;
       return startPtr;
    }
    
    
    void addToList(NODE *startPtr, int number)
    {
       NODE *currentPtr,*previousPtr,*newPtr;
       newPtr = (NODE *) malloc (sizeof(NODE));
       newPtr -> number = number;
       previousPtr = currentPtr = startPtr;
       currentPtr = currentPtr->nextPtr;
    
       while(currentPtr != NULL)
       {
         previousPtr = currentPtr;
         currentPtr = currentPtr->nextPtr;  
       }
    
       newPtr -> nextPtr = currentPtr;
       previousPtr->nextPtr = newPtr;
    }

    I hope I've provided the information you need.

    *** Thanks ***

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Has anyone got any ideas about why the list prints backwards and does not remove the node? (I have to hand the assignment in an hour).

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I noticed that InitializeList() is actually a perfect function for making new nodes, so I just renamed it "NewNode()" in this example:


    Code:
    void addToList(NODE *startPtr, int number)
    {
       NODE *currentPtr, *newPtr;
       newPtr = NewNode();
       newPtr -> number = number;
       
       currentPtr = startPtr;
    
       while(currentPtr->nextPtr != NULL)
       {
         currentPtr = currentPtr->nextPtr;  
       }
    
      currentPtr->nextPtr = newPtr;
    }

    I hope that helps to clarify the simplicity of iterating through a list.
    As to the other problem, I haven't quite looked through it all just yet.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM