I wrote a encoding program using the C++ stack library. Now I am trying to implement my own stack class, however I notice there are size() and top() member functions in the stack library. I am not sure how to implement my code without these functions, or how to write those functions in my class to get them to work properly with the code I already have.
Here is the areas that the stack library functions are being called in my readFileEncode(string filename, stack<char> &text, string cypher)
function:
ifstream file(fileName, ios::in | ios::binary);
stack<char> temp;
char ch;
while (file.get(ch)){
temp.push(ch ^ cypher[temp.size() % cypher.length()]);
}
while (!temp.isEmpty()){
text.push(temp.top());
temp.pop();
}
Here is my stack class:
#include<iostream>
#define TRUE 1
#define FALSE 0
template <class TYPE>
class stack{
struct node{
TYPE element;
node *next;
};
public:
node *top;
int stackSize;
stack(void); //constructor
~stack(void); //destructor free the stack
void push(TYPE & value);
TYPE pop(void);
TYPE peek(void);
int isEmpty(void); //returns TRUE if empty
void print(void);
void reset(void); //pop all the elements off the stack
size_t size(void) const;
TYPE topOf(void) const;
};
template <class TYPE>
stack<TYPE>::stack(void){
top = NULL;
stackSize = 0;
}
template <class TYPE>
stack<TYPE>::~stack(void){
cout << "Entering Stack Destructor" << endl;
reset();
cout << "Exiting Stack Destructor" << endl;
}
template <class TYPE>
void stack<TYPE>::push(TYPE & value){
node *temp = new node;
if (temp == NULL){
cout << "Push: Memory Allocation Error" << endl;
exit(1);
}
temp->element = value;
temp->next = top;
top = temp;
stackSize++;
}
template <class TYPE>
TYPE stack<TYPE>::pop(void){
TYPE returnElement;
if (top != NULL){
node *temp = top;
returnElement = top->element;
top = top->next;
delete temp; //delete the node
stackSize--;
}
return(returnElement);
}
template <class TYPE>
TYPE stack<TYPE>::peek(void){
TYPE returnElement;
if (top != NULL)
returnElement = top->element;
cout << "Peek: " << returnElement << endl;
return(returnElement);
}
template <class TYPE>
int stack<TYPE>::isEmpty(void){
if (stackSize == 0)
return(TRUE);
else
return(FALSE);
}
template <class TYPE>
void stack<TYPE>::reset(void){
cout << "Reset Stack" << endl;
while (isEmpty() != TRUE){
pop();
}
}
template <class TYPE>
void stack<TYPE>::print(void){
cout << "Inside Print Stack" << endl;
cout << "Stack size = " << stackSize << endl;
node * temp = top;
while (temp != NULL){
cout << " " << temp->element << endl;
temp = temp->next;
}
}
template <class TYPE>
size_t size(void) const{
return stackSize;
}
template <class TYPE>
TYPE stack<TYPE>::topOf(void) const{
return (*top).element;
}
This stack class is based on what I know of stack's. If there is anything wrong, it is because this is the first time I have written a stack class.
So basically, I am having problems either 1) writing the size()
and top()
functions, or 2) rewriting the while loops in my readFileEncode()
function to use what I have. I had help properly writing the code to work with the stack library, but now trying to implement my own class is causing me problems. Any help would be appreciated.
EDIT 1: With the help of dyp, I changed the variable name of int size
to int stackSize
everywhere it appears in the class. Also, I change the function top()
to topOf()
EDIT 2: Changed void push(TYPE &value)
to void push(TYPE const& value)
in all instances. Code is updated above. I used the following main()
to test the class:
#include <iostream>
#include <cstdlib>
#include "stack.h"
using namespace std;
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
cout << "empty: " << s.isEmpty() << endl;
s.pop();
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
//cout << "top element: " << s.top() << endl;
cout << "empty: " << s.isEmpty() << endl;
system ("pause");
}
Everything worked fine, however when I attached the stack.h file to my encoding program, I get the following errors:
At file.put(text.top());
I get "term does not evaluate to a function taking 0 arguments," and the same error at text.push(temp.top());
. I get the error, but not exactly sure how to fix it.
I also had to move public:
in my class above node *top; int stackSize;
because I got errors about them being private. This was not a problem with my test program. Not sure if that is ok either.
I used the following code to test my stack:
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
cout << "top element: " << s.topOf() << endl;
cout << "empty: " << s.isEmpty() << endl;
s.pop();
s.pop();
s.pop();
cout << "size: " << s.size() << endl;
//cout << "top element: " << s.top() << endl;
cout << "empty: " << s.isEmpty() << endl;
system ("pause");
}
I changed the code in my following stack class to the following and it seems to have worked:
void push(TYPE const& value);
size_t size(void) const;
TYPE topOf(void) const;
and:
void stack<TYPE>::push(TYPE const& value){
node *temp = new node;
if (temp == NULL){
cout << "Push: Memory Allocation Error" << endl;
exit(1);
}
temp->element = value;
temp->next = top;
top = temp;
stackSize++;
}
template <class TYPE>
size_t stack<TYPE>::size(void) const{
return stackSize;
}
template <class TYPE>
TYPE stack<TYPE>::topOf(void) const{
return (*top).element;
}
Also changed all occurrences of top()
in my main to topOf()
. Not sure if this is all proper, but it works, and I will take dyp's advice and turn it in at CodeReview.