Thread: need help printing some values from a queue class.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    need help printing some values from a queue class.

    ok here is my queue function that prints all the values

    Code:
    void Queue::displayAll()
    {
      if (isEmpty()) cout << "Empty  ";
      QueueNode *temp = frontPtr;
    
      while (temp!= NULL) 
      {
        cout << temp->item.word<<" line: ";
        cout << temp->item.line<<endl;
        temp = temp->next;
      }
    } // end

    I declared my linked list like this with a back pointer and a frontpointer.
    Code:
    struct JOB {
      string word;
      int line;
    };
    
    typedef JOB QueueItemType;
    
    
    struct QueueNode {
         QueueItemType  item;
         QueueNode     *next;
         }; // end struct
    
       QueueNode *backPtr;
       QueueNode *frontPtr;
    };  // end class
    so what im doing is printing the word the user enters up to 200 then I will list them like this.

    Concordance:

    A: 3 5 10
    ABLE: 4
    AND: 6
    ANYWAY: 11
    APPEARS: 2
    AS: 5
    ASK: 9 10
    COLLAPSED: 7
    CONCORDANCE: 3
    DOG: 9 11
    HE: 5 6 7
    HE'D: 10
    HOW: 4

    the numbers being the line in which the word appears, but right now my displayall function will show the word even if it appears on a different line separately like this take A for example:
    A: 3 5 10

    mine will be
    A: 3
    A: 5
    A: 10

    I tried a few if statements to check if the next word is the same as current word like this:

    if(temp.item.word==temp.next)

    and

    if (temp->>item==temp->>next->>item.next)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to keep track of the previous word, you'll have to ... keep track of the previous word in some sort of variable (i.e. store it at the end of the loop before you follow the link to the next one).

    This is another bit of work that if you had followed the assignment (and used a linked-list of line numbers) would be completely irrelevant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  2. Printing values from Enum
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 02-16-2009, 10:28 PM
  3. printing a queue
    By vidioholic in forum C Programming
    Replies: 3
    Last Post: 10-18-2008, 09:56 PM
  4. noob help on queue class
    By robaster in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 10:20 AM
  5. Printing all items in a Queue
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2004, 01:25 PM