Generic Stack Implementation
Creating a reusable stack structure requires leveraging C++ templates. This allows the data structure to handle various types without rewriting logic for each specific type. The following implemantation defines a dynamic stack capable of storing elements of any specified type.
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
const int DEFAULT_CAPACITY = 100;
template <typename ItemType>
class TemplateStack {
private:
ItemType* buffer;
int capacity;
int position;
public:
TemplateStack() : capacity(DEFAULT_CAPACITY), position(-1) {
buffer = new ItemType[capacity];
}
TemplateStack(int limit) : capacity(limit), position(-1) {
buffer = new ItemType[capacity];
}
~TemplateStack() {
delete[] buffer;
}
void store(ItemType item) {
if (position + 1 < capacity) {
buffer[++position] = item;
} else {
throw runtime_error("Stack overflow detected");
}
}
ItemType retrieve() {
if (position >= 0) {
return buffer[position--];
} else {
throw runtime_error("Stack underflow detected");
}
}
bool checkEmpty() {
return position == -1;
}
void renderReverse() {
if (checkEmpty()) {
cout << "Stack is empty" << endl;
return;
}
for (int i = position; i >= 0; --i) {
cout << buffer[i] << " ";
}
cout << endl;
}
void renderForward() {
if (checkEmpty()) {
cout << "Stack is empty" << endl;
return;
}
for (int i = 0; i <= position; ++i) {
cout << buffer[i];
}
cout << endl;
}
void clear() {
position = -1;
}
};
int main() {
TemplateStack<char> charStack;
TemplateStack<int> intStack(5);
string inputText;
cin >> inputText;
for (char c : inputText) {
charStack.store(c);
}
for (int i = 0; i < 5; ++i) {
int val;
cin >> val;
intStack.store(val);
}
charStack.renderReverse();
intStack.renderReverse();
return 0;
}
Text Processing Utility
A stack can simulate text editing behaviors such as backspacing or celaring a line. In this scenario, specific characters trigger stack operations: a less-then sign removes the last character, an at-sign clears the entire line, and a newline triggers output. The process terminates when a hash symbol is encountered.
#include <iostream>
#include <vector>
using namespace std;
class LineBuffer {
private:
char* storage;
int limit;
int cursor;
public:
LineBuffer(int size) : limit(size), cursor(-1) {
if (size > 0 && size <= 100) {
storage = new char[size];
} else {
storage = nullptr;
limit = 0;
cout << "Invalid buffer size initialization" << endl;
}
}
~LineBuffer() {
if (storage != nullptr) {
delete[] storage;
}
}
void addChar(char c) {
if (cursor + 1 < limit) {
storage[++cursor] = c;
}
}
void removeChar() {
if (cursor >= 0) {
cursor--;
} else {
cout << "Buffer empty, cannot remove" << endl;
}
}
void reset() {
cursor = -1;
}
void printContent() {
if (cursor >= 0) {
for (int i = 0; i <= cursor; ++i) {
cout << storage[i];
}
cout << endl;
} else {
cout << "Buffer empty" << endl;
}
}
bool isEmpty() {
return cursor == -1;
}
};
int main() {
int bufferSize;
cout << "Enter buffer size: ";
cin >> bufferSize;
cin.ignore();
LineBuffer editor(bufferSize);
char currentChar;
cout << "Enter text (< for backspace, @ for clear, # to end):" << endl;
while (true) {
currentChar = cin.get();
if (currentChar == '#') {
editor.printContent();
editor.reset();
break;
} else if (currentChar == '\n') {
editor.printContent();
editor.reset();
} else if (currentChar == '<') {
editor.removeChar();
} else if (currentChar == '@') {
editor.reset();
} else {
editor.addChar(currentChar);
}
}
return 0;
}