Skip to content

July 18, 2014

Example: Implement a Stack of Strings in C++

by noise

Next example show how to implement a class of strings in C++. We will first read a text from a file and we will store it into a C++ string. Then we will split the string into words (by spaces) and we will store the words in a vector of strings.

Then we will iterate through that vector of strings and we will add elements our stack only if element not equal with “-“. If element is equal with “-” character then we will pop element from top of stack. We will display elements that we remove (using pop()) from our stack.

Class ReadFromFile

Class ReadFromFile header file (ReadFromFile.h):

#include <iostream>
#include <fstream>
 
using namespace std;
 
class ReadFromFile {
private:
    string fileName;
    long long buffLength;
    string buff;
 
public:
    ReadFromFile(string file);
    ~ReadFromFile();
 
    string displayBuffer();
    long long getFileLength();
};

Class ReadFromFile implementation file (ReadFromFile.cpp):

#include "ReadFromFile.h"
 
ReadFromFile::ReadFromFile(string file) {
    fileName = file;
    char *tmpBuff;
    ifstream is;
 
    is.open(file.c_str());
 
    if (is.fail())
        return;
 
    is.seekg(0, is.end);
    buffLength = is.tellg();
    is.seekg (0, is.beg);
 
    tmpBuff = new char[buffLength+1];
 
    is.read(tmpBuff, buffLength);
    buff = tmpBuff;
    delete[] tmpBuff;
    is.close();
}
 
ReadFromFile::~ReadFromFile() {
   // delete[] buff;
}
 
string ReadFromFile::displayBuffer() {
    return buff;
}
 
long long ReadFromFile::getFileLength() {
    return buffLength;
}

Class SplitBufferIntoWords

We will split the buffer that we read from file (which is defined as a string) into words (strings) by space delimiter or end of line delimiter. Then we store the words into a vector of strings.

Class SplitBufferIntoWords header file (SplitBufferIntoWords.h):

#include <string>
#include <vector>
#include <sstream>
 
using namespace std;
 
class SplitBufferIntoWords {
    string word;
    vector<string> vectorOfWords;
 
public:
    SplitBufferIntoWords(string str1);
    vector<string> getVectorOfWords();
    unsigned long getVectorSize();
    string getElemAt(int x);
};

Class SplitBufferIntoWords implementation file (SplitBufferIntoWords.cpp):

#include "SplitBufferIntoWords.h"
 
SplitBufferIntoWords::SplitBufferIntoWords(string str1) {
 
    istringstream iss(str1);
 
    while (iss) {
        string word;
        iss >> word;
        //cout << word << endl;
        vectorOfWords.push_back(word);
    } 
}
 
vector<string> SplitBufferIntoWords::getVectorOfWords() {
    return vectorOfWords;
}
 
unsigned long SplitBufferIntoWords::getVectorSize() {
    return vectorOfWords.size();
}
 
string SplitBufferIntoWords::getElemAt(int x) {
    return vectorOfWords[x];
}

Class StackOfStrings

We use StackOfStrings class to define a stack of strings and work with it. We’ve defined push() and pop() class functions to push and pop elements from stack. We’ve also defined isEmpty() class function which returns 0 if the stack is not empty and 1 if it is empty.

Class StackOfStrings header file (StackOfStrings.h):

#include <string>
using namespace std;
 
class StackOfStrings {  
    class Element {
       public:
            string value;
            Element *next;
        };
 
    Element *topElement;
 
public:
    bool isEmpty();
    void push(string val);
    string pop();
};

Class StackOfStrings implementation file (StackOfStrings.cpp):

#include "StackOfStrings.h"
 
bool StackOfStrings::isEmpty() {
    return topElement == 0;
}
 
void StackOfStrings::push(string val) {
    Element *oldTopElement = topElement;
    topElement = new Element();
    topElement->value = val;
    topElement->next = oldTopElement;
}
 
string StackOfStrings::pop() {
    string val = topElement->value;
    Element *elementToDealloc = topElement;
    topElement = topElement->next;
    delete elementToDealloc;
    return val;
}

Main Program

Dont’t forget that words that we display are the ones we’ve removed from the stack (when “-” is met).

The main program follows (main.cpp):

#include <iostream>
#include <sstream>
#include <string>
 
#include "StackOfStrings.h"
#include "ReadFromFile.h"
#include "SplitBufferIntoWords.h"
 
using namespace std;
 
int main(int argc, const char * argv[]) {
    std::cout << "Stack of Ints implementation\n";
 
    StackOfStrings *stack = new StackOfStrings;
    ReadFromFile *aFile = new ReadFromFile("test.txt");
    string aBuffer = aFile->displayBuffer();
    SplitBufferIntoWords vectorOfWords(aBuffer);
 
    for (int i; i < vectorOfWords.getVectorSize(); i++) {
        if (vectorOfWords.getElemAt(i) == "-") {
            cout << stack->pop() << " ";
        }
        else
            stack->push(vectorOfWords.getElemAt(i));
    }
 
    cout << endl << "stack is empty: " << stack->isEmpty() << endl;
 
    return 0;
}

You will also need to create a test file. Our is called test.txt with the following content:

the brown fox jumped – over the lazy dog – –

Read more from C and C++ Errors

Leave a Reply

required
required

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments