Linked List problems

This is a discussion on Linked List problems within the C++ Programming forums, part of the General Programming Boards category; >> So I should delete pTemp inside of pop after I use it in the return statement? Not after the ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> So I should delete pTemp inside of pop after I use it in the return statement?
    Not after the return statement (it won't get called). Save the value you are popping in a temp variable, then delete the node, then return the temp variable.

    >> For my main method, you are saying that it should be structured like this?
    Yes. That looks exactly like how I would structure it.

    >> Then push would look like:
    That looks ok to me, although I won't guarantee anything since I just looked it over. As far as I can tell myNode doesn't need to be a member variable. Make it a variable local to your push funtion. Otherwise it looks good.

  2. #17
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Daved,

    I thought myNode was a variable local to my push function, since it is declared and used inside of the push function?

    I've run across another problem. I'm trying to modify a variable from inside of a const method - but the variable is a class variable so I thought that a const method would be able to modify it. The variable is "errExists":

    Code:
    class queue
    {
        public:
            queue(void);
            bool push(std::string ss);
            std::string pop(void);
            std::string first(void) const;
            std::string last(void) const;
            bool err(void);
            int queueElements;
            bool errExists;
    [etc]
    And the method that is attempting to modify it:
    Code:
    string queue::last() const
    {
        if(queueElements == 0)
        {
            errExists = true;
            return "";
        } 
        else
        {
            return pTail->nvalue;
        }
    }
    The compiler is giving me this error:
    Code:
    86 queue.cpp assignment of data-member `queue::errExists' in read-only structure

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I thought myNode was a variable local to my push function, since it is declared and used inside of the push function?

    It is used but not declared there. The declaration would be:
    Code:
    Node* myNode = new Node;
    For the errExists problem, that is a situation where you might want to use the mutable keyword. The const on the function indicates that the state of the object is unchanged. But your errExists member variable is not related to the actual state of the list. If you make it mutable (by adding the word mutable before the "bool errExists;" in your class), then it can be changed in a const function.

    Another option is to rethink your error handling strategy. You might want to throw an exception, or just leave it as "undefined behavior". If you do that, you leave it up to the user of your list to make sure that there is something in the list before calling last(). That is what the standard containers often do.

  4. #19
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    I have one last question. In my last program that I wrote I had a statement which looked like this:

    Code:
    stack1.cc = 0;
    while(getline(cin, stack1.aa[stack1.cc]))
    {
      if(stack1.aa[stack1.cc].length() > 80)
        cerr "Too long";
      else if
        etc
      else
        cerr "Bad input";
    }
    
    // end of program
    When reading from a file reached eof the while loop would be finished and the program terminal window would close, or if there was no input file specified and the input was coming from the keyboard instead, if the user just hit return without typing anything the program would exit, too. Now, I have what I thought was the exact same loop structure in this program, and it exits fine when reading from a file but when keyboard input is supplied the program won't exit if the user just hits enter (thus submitting a null value).

    Code:
    int main(int argc, char *argv[])
    {
        queue myQueue;
        string temp, holder;
        int lncount;
    
        lncount = 0;
    
        while(getline(cin, temp))
        {
            lncount++;
    
            if(cin.bad())
            {
                cerr << "Bad input on line " << lncount;
                exit(1);
            }
    
    
            if(temp[0] == '>') // push
            {
                temp = temp.substr(1, temp.length()); // remove '>'
                temp += "\n";
    
                if(!(myQueue.push(temp)))
                {
                    cerr << "No memory for allocating queue element, current queue size is " << myQueue.queueElements << ".\n";
                    exit(1);
                }
            }
    
            else if(temp[0] == '<') // pop
            {
                holder = myQueue.pop();
                if(myQueue.err())
                    cerr << "Empty queue.\n";
                else
                    cout << holder;
            }
    
            else // bad input
            {
                cerr << "Bad input at line " << lncount << ".\n";
            }
    
            // reset error status
            myQueue.errExists = false;
                
        }
    
        system("Pause");
        return 0;
    }
    Any idea to why it won't exit through keyboard input?

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    while (getline(cin, temp)) will only break when a failure occurs. When reading in strings, the only practical cause of an error will be end-of-file. But when inputting through cin, you only get end-of-file if the user types in the end-of-file character (on windows it's Ctrl-D I think). So if your user does the input and hits Ctrl-D and <enter> when done, that should work.

    If you want the loop to break when the line is empty, just check temp to see if it is empty:
    Code:
    while(getline(cin, temp) && !temp.empty())
    >> if(cin.bad())
    This almost never happens, and it's not because of bad input. cin.fail() will be true on bad input, but that won't happen with strings (unless end-of-file is reached) and if it does the loop will break anyway, so that if isn't really necessary.

  6. #21
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    When you see an error that makes no sense -- how can a line consisting solely of '{' be an error -- start looking upward until you find it.

  7. #22
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Quote Originally Posted by Daved View Post
    If you want the loop to break when the line is empty, just check temp to see if it is empty:
    Code:
    while(getline(cin, temp) && !temp.empty())
    This works for keyboard input but if I am using a file input that looks like:

    Code:
    >push me
    >again
    <pop
    
    >more pushing
    <pop
    The program will exit prematurely when it reaches the blank line, not reaching the last two lines in the file. I don't understand how my last program worked as I wanted with just while(getline(cin, temp)) and this one does not with the exact same code.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    As I said, you would use that code "if you want the loop to break when the line is empty". If you don't want to break when the line is empty, remove the !temp.empty() part.

    You can't have it both ways. Either you break on EOF or you break on an empty line.

    If you just want to skip the code in the loop when the line is empty, then at the top of the loop check if temp is empty and continue if it is. The continue will skip the rest of the loop and go on to the next line.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 02:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 06:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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