Hello, I would like some help with my code with compiler errors as I am not sure what is wrong. Please help...
linkedStack.h:
#ifndef LINKEDSTACK_H #define LINKEDSTACK_H
#include #include #include "stackADT.h" using namespace std;
template class nodeType{ Type info; nodeType *next; };
template class linkedStackType: public stackADT{ public: const linkedStackType& operator= (const linkedStackType&); //overload assignment operator bool isEmptyStack() const; //to see if stack is empty bool isFullStack() const; //to see if stack is full void initializeStack(); //function to initialize stack to empty void push(const Type& newItem); //add newItem to stack Type top() const; //return top element of stack void pop(); //remove top element of stack linkedStackType(); //default constructor linkedStackType(const linkedStackType& otherStack); //copy constructor ~linkedStackType(); //destructor private: nodeType *stackTop; //pointer to the stack void copyStack(const linkedStackType& otherStack); //function to make a copy of otherStack };
//default constructor template linkedStackType::linkedStackType(){ stackTop = nullptr; }
template bool linkedStackType::isEmptyStack() const{ return(stackTop == nullptr); }//end isEmptyStack
template bool linkedStackType::isFullStack() const{ return false; }//end isFullStack
template void linkedStackType::initializeStack(){ nodeType *temp; //pointer to delete the node while(stackTop != nullptr){ //while the stack is not empty temp = stackTop; //set temp to point to current node stackTop = stackTop->next; //move stackTop to next node delete temp; }//end while }//end initializeStack
template void linkedStackType::push(const Type& newElement){ nodeType *newNode; //pointer to create new node newNode = new nodeType; //create node newNode->info = newElement; //store newElement in node newNode->next = stackTop;//insert newNode before stackTop stackTop = newNode; //set stackTop to point to top node }//end push
template Type linkedStackType::top() const{ assert(stackTop != nullptr){ //if stack is empty return stackTop->info; } }//end top
template void linkedStackType::pop(){ nodeType *temp; if(stackTop != nullptr){ temp = stackTop; stackTop = stackTop->next; delete temp; } else{ cout << "cannot remove from an empty stack" << endl; } }//end pop
template void linkedStackType::copyStack (const linkedStackType& otherStack){ nodeType *newNode, *current, *last; if(stackTop != nullptr){ //if stack is not empty, make it empty initializeStack(); } if(otherStack.stackTop == nullptr){ stackTop = nullptr; } else{ current = otherStack.stackTop; //set current to point to stack //to be copied //copy the stackTop element of stack stackTop = new nodeType; //create the node stackTop->info = current->info; //copy info stackTop->next = nullptr; //set next to nullptr last = stackTop; //set last to point to the node current = current->next; //set current to point to next node //copy remaining stack while(current != nullptr){ newNode = new nodeType; newNode->info = current->info; newNode->next = nullptr; last->next = newNode; last = newNode; current = current->next; }//end while }//end else }//end copyStack
//copy constructor template linkedStackType::linkedStackType( const linkedStackType& otherStack){ stackTop = nullptr; copyStack(otherStack); }//end copy constructor
//destructor template linkedStackType::~linkedStackType(){ initializeStack(); }//end destructor
//overloading assignment operator template const linkedStackType &linkedStackType::operator= (const linkedStackType& otherStack){ if(this != &otherStack){ //avoid self-copy copyStack(otherStack); } return *this; }//end operator =
#endif /* LINKEDSTACK_H */
-------------------------------------
linkedQueue.h:
#ifndef LINKEDQUEUE_H #define LINKEDQUEUE_H
#include #include #include "queueADT.h" using namespace std;
//definition of the node template class nodeType{ Type info; nodeType *next; };
template class linkedQueueType: public queueADT{ public: const linkedQueueType& operator= (const linkedQueueType&); //overload the assignment operator bool isEmptyQueue() const; //function to see if queue is empty bool isFullQueue() const; //function to see if queue is full void initializeQueue(); //function to make queue empty Type front() const; //function to return the first element of queue Type back() const; //function to return the last element of queue void addQueue(const Type& queueElement); //function to add queueElement to queue void deleteQueue(); //function to remove the first element of queue linkedQueueType(); //default constructor linkedQueueType(const linkedQueueType& otherQueue); //copy constructor ~linkedQueueType(); //destructor private: nodeType *queueFront; //pointer to front of queue nodeType *queueRear; //pointer to rear of queue };
//default constructor template linkedQueueType::linkedQueueType(){ this->queueFront = nullptr; //set front to nullptr this->queueRear = nullptr; //ser rear to nullptr }//end default constructor
template bool linkedQueueType::isEmptyQueue() const{ return (this->queueFront == nullptr); }//end isEmptyQueue
template bool linkedQueueType::isFullQueue() const{ return false; }//end isFullQueue
template void linkedQueueType::initializeQueue(){ nodeType *temp; while(this->queueFront != nullptr){ //while there are elements in queue temp = this->queueFront; //set temp to point to current node this->queueFront = this->queueFront->next; // advance first node to next one delete temp; } this->queueRear = nullptr; //set rear to nullptr }//end initializeQueue
template void linkedQueueType::addQueue(const Type& newElement){ nodeType *newNode; newNode = new nodeType; newNode->info = newElement; newNode->next = nullptr; if(this->queueFront == nullptr){ //if queue is empty this->queueFront = newNode; this->queueRear = newNode; } else{ //add newNode at the end this->queueRear->next = newNode; this->queueRear = this->queueRear->next; } }//end addQueue
template Type linkedQueueType::front() const{ assert(this->queueFront != nullptr); return this->queueFront->info; }//end front
template Type linkedQueueType::back() const{ assert(this->queueRear != nullptr); return this->queueRear->info; }//end back
template void linkedQueueType::deleteQueue(){ nodeType *temp; if(!isEmptyQueue()){ temp = this->queueFront; //make temp point to first node this->queueFront = this->queueFront->next; //advance queueFront delete temp; //delete first node if(this->queueFront == nullptr){ //if after deleting the queue //the queue becomes empty this->queueRear = nullptr; //set queueRear to nullptr } }//end if else{ cout << "cannot remove from an empty list" << endl; } }//end deleteQueue
//destructor template linkedQueueType::~linkedQueueType(){ initializeQueue(); }//end destructor
template const linkedQueueType& linkedQueueType::operator= (const linkedQueueType& otherQueue){ if(this != &otherQueue){ //avoid self-copy linkedQueueType(otherQueue); } return *this; }//end assignment operator
//copy constructor template linkedQueueType::linkedQueueType (const linkedQueueType& otherQueue){ nodeType *newNode, *current, *last; if(this->queueFront != nullptr){ //if queue is not empty, make it empty initializeQueue(); } if(otherQueue.this->queueFront == nullptr){ this->queueFront = nullptr; } else{ current = otherQueue.this->queueFront; //set current to point to queue //to be copied //copy the first element of queue this->queueFront = new nodeType; //create the node this->queueFront->info = current->info; //copy info this->queueFront->next = nullptr; //set next to nullptr last = this->queueRear; //set last to point to the node current = current->next; //set current to point to next node //copy remaining stack while(current != nullptr){ newNode = new nodeType; newNode->info = current->info; newNode->next = nullptr; last->next = newNode; last = newNode; current = current->next; }//end while }//end else }
#endif /* LINKEDQUEUE_H */
------------------------------------------------
stackADT.h:
#ifndef STACKADT_H #define STACKADT_H
template class stackADT{ public: virtual void initializeStack() = 0; //to initialize stack to an empty stack virtual bool isEmptyStack() const = 0; //function to see if stack is empty virtual bool isFullStack() const = 0; //function to see if stack is full virtual void push(const Type& newItem) = 0; //function to add newItem to stack virtual Type top() const = 0; //function to return the top element of stack virtual void pop() = 0; //function to remove the top element of stack };
#endif /* STACKADT_H */
-----------------------------------------------
queueADT.h:
#ifndef QUEUEADT_H #define QUEUEADT_H
template class queueADT{ public: virtual bool isEmptyQueue() const = 0; //function to see if queue is empty virtual bool isFullQueue() const = 0; //function to see if queue is full virtual void initializeQueue() = 0; //function to make queue empty virtual Type front() const = 0; //function to return first element of queue virtual Type back() const = 0; //function to return last element of queue virtual void addQueue(const Type& queueElement) = 0; //function to add queueElement to queue virtual void deleteQueue() = 0; //function to remove the first element of queue };
#endif /* QUEUEADT_H */
-----------------------------------------------------
main.cpp:
#include #include #include #include "linkedStack.h" #include "linkedQueue.h"
using namespace std;
int length(char input[]){ int i = 0; while(input[i]){ i++; } return i; }
//function to return priority of the operators int priority(char c){ if(c == '*' || c == '/'){ return 2; } else if(c == '+' || c == '-'){ return 1; } else{ return -1; } }
//return the operator and operators and operands int evaluate(int op1, int op2, char operator){ switch(operator){ case '*': return op2 * op1; case '/': return op2 / op1; case '+': return op2 + op1; case '-': return op2 - op1; default: return 0; } }
//convert infix to postfix void infixToPostfix(char *input){ stack s; int len = length(input), index = 0; char output[1000]; for(int i = 0; i < len; i++){ if(input[i] >= '0' && input[i] <= '9'){ output[index++] = input[i]; } else if(input[i] == '('){ s.push('('); } else if(input[i] == ')'){ //if scanned input is ) pop out from stack while(!s.empty() && s.top() != '('){ char c = s.top(); s.pop(); output[index++] = c; } if(s.top() == '('){ char c = s.top(); s.pop(); } } else{ while(!s.empty() && priority(input[i] <= priority(s.top()))){ char c = s.top(); s.pop(); output[index++] = c; } s.push(input[i]); } } //pop all remaining elements from stack while(!s.empty()){ char c = s.top(); s.pop(); output[index++] = c; } output[index] = '\0'; cout << output << endl; //copying to change input expression //from calling function evaluatePostfix int i = 0; while(i < length(output)){ input[i] = output[i]; i++; } input[i] = '\0'; }
void evaluatePostfix(char postfix[]){ stack s; int i = 0; char ch; int val; int len = length(postfix); while(i < len){ ch = postfix[i]; if(isdigit(ch)){ s.push(ch - '0'); } else{ int op1 = s.top(); s.pop(); int op2 = s.top(); s.pop(); val = evaluate(op1, op2, ch); s.push(val); } i++; } cout << val; }
void evaluatePostfix(char exp[]){ infixToPostfix(exp); evaluatePostfix(exp); }
int main() { char exp; cout << "Enter an equation: " << endl; cin >> exp; infixToPostfix(exp); evaluatePostfix(exp); exit(EXIT_SUCCESS); }