// Lab Problem 5A - file "varlist.h" // SE307 CCT // T Naughton, CS NUIM, tomn@cs.may.ie // Last updated 1 November 2000 #include #include #ifndef VARLIST_H #define VARLIST_H /* This class maintains a dynamic list of variables for machines ** simulating/analysing the code of another machine passed to it ** as input. The methods of this class are chosen to reflect the ** capabilities of the restricted language defined in lab sheet 4. */ class VarDetails { // a simple class defining the contents of each list element public: string name; int value; VarDetails * next; // link to next element }; class VarList { VarDetails * head; // head of the linked list public: VarList(); ~VarList(); void DeleteList(); void Add(string varname, int initval); VarDetails * FindElement(string varname); void Inc(string varname); void Set(string varname, int newval); bool LessThan(string var1, string var2); int Val(string varname); bool Contains(string varname); void PrintList(); }; VarList::VarList() { head = NULL; // mark the end of the list with a NULL } VarList::~VarList() { DeleteList(); // clean up } void VarList::DeleteList() { /* Delete the entire list. */ VarDetails * temp; // pointer to element to be deleted while (head != NULL) { temp = head; head = head->next; delete temp; } // head will have been set to NULL when loop terminates. } void VarList::Add(string varname, int initval) { /* Add a new variable to the list of variables. Initialise ** to initval. */ VarDetails * temp; temp = new VarDetails; temp->name = varname; temp->value = initval; temp->next = head; // add to head of list head = temp; } VarDetails * VarList::FindElement(string varname) { /* Return a pointer to the appropriate list element if it ** exists, else print an error message and return NULL. */ VarDetails * temp; temp = head; while (temp != NULL) { if (temp->name == varname) { return temp; } temp = temp->next; } cout << "\nError: Variable " << varname << " has not been" << " declared and so cannot be accessed." << endl; return NULL; } void VarList::Inc(string varname) { /* Increment the value in a particular variable. */ VarDetails * temp; temp = FindElement(varname); if (temp != NULL) { temp->value = temp->value + 1; } } void VarList::Set(string varname, int newval) { /* Update the value of varname in the list. */ VarDetails * temp; temp = FindElement(varname); if (temp != NULL) { temp->value = newval; } } bool VarList::LessThan(string var1, string var2) { /* Return true if the value in var1 is less than the value ** in var2, otherwise return false. */ VarDetails * temp; int a; // holds the value of var1 temp = FindElement(var1); if (temp != NULL) { a = temp->value; temp = FindElement(var2); if (temp != NULL) { return (a < temp->value); } } return false; } int VarList::Val(string varname) { /* Return the value of varname. */ VarDetails * temp; temp = FindElement(varname); if (temp != NULL) { return (temp->value); } else { return -1; } } bool VarList::Contains(string varname) { /* Returns true if varname is in the list of variables, else ** returns false. */ return (FindElement(varname) == NULL); } void VarList::PrintList() { /* Print all variables and values. */ VarDetails * temp; cout << " \t// "; temp = head; while (temp != NULL) { cout << temp->name << '=' << temp->value << ' '; temp = temp->next; } cout << flush; } #endif