Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <exception>
- using namespace std;
- template <typename T> class item {
- template <typename> friend class stack;
- private:
- T data;
- item<T>* next;
- public:
- item() : data(T()), next(nullptr) {}
- item(T _data) : data(_data), next(nullptr) {}
- item(T _data, item<T>* _next) : data(_data), next(_next) {}
- };
- template <typename T> class stack {
- private:
- item<T>* top;
- public:
- stack() : top(nullptr) {}
- ~stack() { while (!empty()) pop(); }
- bool empty() { return top == nullptr; }
- void push(T data) { top = new item<T>(data, top); }
- bool pop() {
- if (empty()) {
- throw exception();
- return false;
- }
- T data = top->data;
- item<T>* old = top;
- top = top->next;
- delete old;
- return true;
- }
- T showtop() {
- if (empty())
- throw exception();
- return top->data;
- }
- };
- int main() {
- stack<int> s, s1;
- int A;
- size_t count(0);
- for (int i = 1; i <= 10; i++)
- s.push(i);
- cout << "Enter A: ";
- cin >> A;
- while (!s.empty()) {
- if (s.showtop() != A) {
- s1.push(s.showtop());
- s.pop();
- count++;
- }
- else {
- s.pop();
- break;
- }
- }
- while (!s1.empty()) {
- if (count != 1) {
- cout << s1.showtop() << endl;
- s1.pop();
- count--;
- }
- else
- s1.pop();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement