MastaC729

CMPSC 122 Homework 2 tokenlist.h

Feb 21st, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. // Token List Header file
  2. // This is a linked list for use with the tokens for an
  3. // arithmetic expression.  Although it is used for that
  4. // particular application, this implementation mroe
  5. // resembles a generic linked list, which may be used
  6. // either as a stack or a queue.
  7.  
  8. // There will be support for displaying the entire list at once
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. // And of course, the tokens themselves
  13. #include "token.h"
  14.  
  15. // Since this is a linked list, we actually need a linked list node.
  16. struct ListElement {
  17.     Token       token;              // the token data itself
  18.     struct ListElement *next;       // next element of list
  19. };
  20.  
  21. class ListIterator;         // class definition later
  22.  
  23. class TokenList  {
  24.     friend class ListIterator;
  25.     friend ostream& operator<<( ostream &, TokenList &);
  26.     private:
  27.     ListElement *head,  // front of the list
  28.                 *tail;  // tail of the list
  29.     public:
  30.     TokenList()     // create an empty list
  31.     {
  32.         head = NULL;
  33.         tail = NULL;
  34.     }
  35.     TokenList( const char[] );  // or create initial list
  36.     ~TokenList()            // destructor -- clear the list
  37.     {
  38.         ListElement *remove;
  39.         while ( (remove = head) != NULL)
  40.         {
  41.         head = head->next;  // find the successor
  42.         delete remove;      // and deallocate this
  43.         }
  44.     }
  45.  
  46.     bool empty() const
  47.     {
  48.         return head == NULL;
  49.     }
  50.  
  51.     Token first() const
  52.     {
  53.         return head->token;
  54.     }
  55.  
  56.     void push_back( Token t );
  57.     void push_front( Token t );
  58.     Token pop_front();
  59.  
  60.     // and now some support for the List Iterator
  61.     ListIterator begin();
  62.     ListIterator end();
  63. };
  64.  
  65. class ListIterator
  66. {
  67.     friend class TokenList; // let that class create iterators
  68.     private:
  69.     TokenList *list;    // which list
  70.     ListElement *curr;  // pointer to current node
  71.     ListIterator( TokenList *l, ListElement *le )
  72.     {
  73.         list = l;
  74.         curr = le;
  75.     }
  76.     public:
  77.     Token& token() const
  78.     {
  79.         return curr->token;
  80.     }
  81.     char tokenChar()
  82.     {
  83.         if (curr != NULL)
  84.         return curr->token.tokenChar();
  85.         else
  86.         return 0;
  87.     }
  88.     bool currentIsInteger()
  89.     {
  90.         return curr->token.isInteger();
  91.     }
  92.     int integerValue()
  93.     {
  94.         return curr->token.integerValue();
  95.     }
  96.     void advance()
  97.     {
  98.         if (curr != NULL)
  99.             curr = curr->next;
  100.     }
  101.     int operator !=( const ListIterator &other ) const
  102.     {
  103.         return list != other.list || curr != other.curr;
  104.     }
  105. };
Advertisement
Add Comment
Please, Sign In to add comment