Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- struct Phrase {
- string subject, verb, noun, adjective;
- bool ExtendedType; //is ExtenedType, if contains an adjective
- Phrase(const string &subject, const string &verb, const string &noun) {
- ExtendedType = false;
- this->subject = string(subject);
- this->verb = string(verb);
- this->noun = string(noun);
- }
- Phrase(const string &subject, const string &verb, const string &adjective, const string &noun) {
- ExtendedType = true;
- this->subject = string(subject);
- this->verb = string(verb);
- this->adjective = string(adjective);
- this->noun = string(noun);
- }
- Phrase(const Phrase *phrase) { // copy constructor from a pointer
- subject = phrase->subject;
- verb = phrase->verb;
- noun = phrase->noun;
- ExtendedType = phrase->ExtendedType;
- if (ExtendedType)
- adjective = phrase->adjective;
- }
- //No dynamically allocated fields, so no need for destructor.
- string getSubject() {
- return subject;
- }
- };
- ostream& operator<<(ostream& os, Phrase *phrase) {
- os << phrase->subject << " " << phrase->verb << " kein ";
- if (phrase->ExtendedType)
- os << phrase->adjective << " ";
- os << phrase->noun << ".";
- return os;
- }
- struct Node {
- Phrase *phrase;
- Node *nextNode;
- Node *prevNode;
- Node(Phrase *phrase, Node *nextNode = nullptr, Node *prevNode = nullptr) : phrase(phrase), nextNode(nextNode), prevNode(prevNode) {}
- ~Node() {
- delete phrase;
- }
- };
- class Stack {
- Node *head;
- int size;
- public:
- Stack() : head(nullptr), size(0) {}
- ~Stack() {
- Node *cur, *next = nullptr;
- cur = head;
- if (cur != nullptr)
- next = cur->nextNode;
- delete cur;
- while (cur != nullptr) {
- delete cur;
- cur = next;
- next = next->nextNode;
- }
- }
- void push(const Phrase *phrase) {
- Phrase *tempPhrase = new Phrase(*phrase); //creating a copy of a phrase
- Node *node = new Node(tempPhrase);
- if (size == 0)
- head = node;
- else {
- Node *cur = head;
- head->nextNode = node;
- node->prevNode = head;
- head = node;
- }
- size++;
- }
- Phrase *peek() {
- if (size == 0)
- return nullptr;
- return head->phrase;
- }
- Phrase *pop() {
- if (size == 0)
- return nullptr;
- Node *temp = head;
- head = head->prevNode;
- Phrase *tempPhrase = new Phrase(*temp->phrase);
- delete temp;
- size--;
- return tempPhrase;
- }
- int getSize() {
- return size;
- }
- bool isEmpty() {
- return size == 0;
- }
- };
- int main() {
- vector<Phrase *> strings;
- strings.push_back(new Phrase("Ich", "hore", "Larm"));
- strings.push_back(new Phrase("Er", "sah", "Katzen"));
- strings.push_back(new Phrase("Ich", "hore", "sanfte", "Musik"));
- strings.push_back(new Phrase("Er", "sieht", "schwarze", "Katze"));
- Stack *stack = new Stack();
- for (int i = 0; i < 4; i++)
- stack->push(strings[i]);
- cout << stack->pop() << endl;
- cout << "Vector still contains it, so it is copied properly:\n" << strings[3] << endl;
- Phrase *phr = stack->peek();
- cout << "Stack size is " << stack->getSize() << ", which is proper.\n" << endl;
- cout << "Peek() return phrase: " << phr << endl;
- stack->pop();
- stack->pop();
- stack->pop();
- cout << "When the stack size is " << stack->getSize() << ", pop() and push() will return a nullptr.";
- phr = stack->pop();
- if (phr == nullptr)
- cout << "This phrase pointer is a nullptr." << endl;
- else
- cout << "This phrase pointer is not a nullptr." << endl;
- cout << "Now, let's delete the stack completely, and check that the original phrases are still in place." << endl;
- delete stack;
- for (int i = 0; i < 4; i++)
- cout << strings[i] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement