Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Phrase {
  8.     string subject, verb, noun, adjective;
  9.     bool ExtendedType; //is ExtenedType, if contains an adjective
  10.    
  11.     Phrase(const string &subject, const string &verb, const string &noun) {
  12.         ExtendedType = false;
  13.         this->subject = string(subject);
  14.         this->verb = string(verb);
  15.         this->noun = string(noun);
  16.     }
  17.  
  18.     Phrase(const string &subject, const string &verb, const string &adjective, const string &noun) {
  19.         ExtendedType = true;
  20.         this->subject = string(subject);
  21.         this->verb = string(verb);
  22.         this->adjective = string(adjective);
  23.         this->noun = string(noun);
  24.     }
  25.  
  26.     Phrase(const Phrase *phrase) { // copy constructor from a pointer
  27.         subject = phrase->subject;
  28.         verb = phrase->verb;
  29.         noun = phrase->noun;
  30.         ExtendedType = phrase->ExtendedType;
  31.         if (ExtendedType)
  32.             adjective = phrase->adjective;
  33.     }
  34.  
  35.     //No dynamically allocated fields, so no need for destructor.
  36.  
  37.     string getSubject() {
  38.         return subject;
  39.     }
  40.  
  41. };
  42.  
  43. ostream& operator<<(ostream& os, Phrase *phrase) {
  44.     os << phrase->subject << " " << phrase->verb << " kein ";
  45.     if (phrase->ExtendedType)
  46.         os << phrase->adjective << " ";
  47.     os << phrase->noun << ".";
  48.     return os;
  49. }
  50.  
  51. struct Node {
  52.     Phrase *phrase;
  53.     Node *nextNode;
  54.     Node *prevNode;
  55.  
  56.     Node(Phrase *phrase, Node *nextNode = nullptr, Node *prevNode = nullptr) : phrase(phrase), nextNode(nextNode), prevNode(prevNode) {}
  57.  
  58.     ~Node() {
  59.         delete phrase;
  60.     }
  61. };
  62.  
  63. class Stack {
  64.     Node *head;
  65.     int size;
  66.  
  67. public:
  68.     Stack() : head(nullptr), size(0) {}
  69.    
  70.     ~Stack() {
  71.         Node *cur, *next = nullptr;
  72.         cur = head;
  73.         if (cur != nullptr)
  74.             next = cur->nextNode;
  75.         delete cur;
  76.         while (cur != nullptr) {
  77.             delete cur;
  78.             cur = next;
  79.             next = next->nextNode;
  80.         }
  81.     }
  82.  
  83.     void push(const Phrase *phrase) {
  84.         Phrase *tempPhrase = new Phrase(*phrase); //creating a copy of a phrase
  85.         Node *node = new Node(tempPhrase);
  86.         if (size == 0)
  87.             head = node;   
  88.         else {
  89.             Node *cur = head;
  90.             head->nextNode = node;
  91.             node->prevNode = head;
  92.             head = node;
  93.         }
  94.         size++;
  95.     }
  96.  
  97.     Phrase *peek() {
  98.         if (size == 0)
  99.             return nullptr;
  100.         return head->phrase;
  101.     }
  102.  
  103.     Phrase *pop() {
  104.         if (size == 0)
  105.             return nullptr;
  106.  
  107.         Node *temp = head;
  108.         head = head->prevNode;
  109.         Phrase *tempPhrase = new Phrase(*temp->phrase);
  110.         delete temp;
  111.         size--;
  112.         return tempPhrase;
  113.     }
  114.  
  115.     int getSize() {
  116.         return size;
  117.     }
  118.  
  119.     bool isEmpty() {
  120.         return size == 0;
  121.     }
  122.  
  123. };
  124.  
  125. int main() {
  126.     vector<Phrase *> strings;
  127.     strings.push_back(new Phrase("Ich", "hore", "Larm"));
  128.     strings.push_back(new Phrase("Er", "sah", "Katzen"));
  129.     strings.push_back(new Phrase("Ich", "hore", "sanfte", "Musik"));
  130.     strings.push_back(new Phrase("Er", "sieht", "schwarze", "Katze"));
  131.    
  132.     Stack *stack = new Stack();
  133.     for (int i = 0; i < 4; i++)
  134.         stack->push(strings[i]);
  135.  
  136.     cout << stack->pop() << endl;
  137.     cout << "Vector still contains it, so it is copied properly:\n" << strings[3] << endl;
  138.     Phrase *phr = stack->peek();
  139.     cout << "Stack size is " << stack->getSize() << ", which is proper.\n" << endl;
  140.     cout << "Peek() return phrase: " << phr << endl;
  141.     stack->pop();
  142.     stack->pop();
  143.     stack->pop();
  144.     cout << "When the stack size is " << stack->getSize() << ", pop() and push() will return a nullptr.";
  145.     phr = stack->pop();
  146.     if (phr == nullptr)
  147.         cout << "This phrase pointer is a nullptr." << endl;
  148.     else
  149.         cout << "This phrase pointer is not a nullptr." << endl;
  150.    
  151.     cout << "Now, let's delete the stack completely, and check that the original phrases are still in place." << endl;
  152.     delete stack;
  153.     for (int i = 0; i < 4; i++)
  154.         cout << strings[i] << endl;
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement