// You are using GCC
#include <iostream>
#include <sstream>
using namespace std;

// Stack node
struct Node {
    string data;
    Node* next;
    Node(string val) {
        data = val;
        next = nullptr;
    }
};

Node* top = nullptr;

// Check if stack is empty
bool isEmpty() {
    return top == nullptr;
}

// Push word
void push(string word) {
    Node* newNode = new Node(word);
    newNode->next = top;
    top = newNode;
    cout << "Pushed: " << word << endl;
}

// Pop word
string pop() {
    if (isEmpty()) {
        cout << "Stack is empty" << endl;
        return "";
    }
    Node* temp = top;
    string poppedWord = temp->data;
    top = top->next;
    delete temp;
    return poppedWord;
}

// Display stack contents
void display() {
    if (isEmpty()) {
        cout << "Stack is empty" << endl;
        return;
    }
    Node* temp = top;
    cout << "Stack elements (top to bottom): ";
    while (temp != nullptr) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

int main() {
    string input;
    int choice;
    bool running = true;

    while (running) {
        cout << "\n1. Enter sentence and reverse words\n2. Display stack\n3. Exit\nEnter your choice: ";
        cin >> choice;
        cin.ignore();  // ignore newline from input

        switch (choice) {
            case 1: {
                cout << "Enter a line of text: ";
                getline(cin, input);

                // Clear old stack
                while (!isEmpty()) pop();

                string word;
                stringstream ss(input);

                // Push each word into stack
                while (ss >> word) {
                    push(word);
                }

                cout << "Reversed sentence: ";
                bool first = true;
                while (!isEmpty()) {
                    if (!first) cout << " ";
                    cout << pop();
                    first = false;
                }
                cout << endl;
                break;
            }

            case 2:
                display();
                break;

            case 3:
                cout << "Exiting..." << endl;
                running = false;
                break;

            default:
                cout << "Invalid choice!" << endl;
        }
    }

    return 0;
}