cis22c/01-stacks/stacks_01_strings.cpp
2024-04-28 11:54:46 -07:00

85 lines
1.5 KiB
C++

/**~*~*~*
CIS 22C
Project: Stack of strings
Written by: Iurii Tatishchev
IDE: CLion
*~*/
#include <iostream>
#include <string>
using namespace std;
class Stack_str {
private:
// Structure for the stack nodes
struct StackNode {
string value; // Value in the node
StackNode *next; // Pointer to next node
};
StackNode *top; // Pointer to the stack top
int length;
public:
Stack_str(){ top = NULL; length = 0; } //Constructor
//~Stack_str(); // Destructor
// Stack operations
bool isEmpty() {
return length == 0;
}
bool push(string);
// string pop();
string peek() {
return top->value;
}
int getLength() {
return length;
}
};
/**~*~*~*
Member function push: pushes the argument onto the stack.
*~**/
bool Stack_str::push(string item) {
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store item there.
newNode = new StackNode;
if (!newNode)
return false;
newNode->value = item;
// Update links and counter
newNode->next = top;
top = newNode;
length++;
return true;
}
int main() {
Stack_str s;
string item;
getline(cin, item);
while (item != "0") {
s.push(item);
cin.clear();
getline(cin, item);
}
cout << s.getLength() << "\n";
cout << (s.isEmpty() ? "Empty Stack!" : s.peek()) << "\n";
cout << s.getLength() << "\n";
return 0;
}