Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- typedef unsigned int index;
- template<class T>
- class Element
- {
- public:
- T d;
- Element() {}
- Element(T& d) : d(d) {};
- template<class U> friend ostream& operator<<(ostream& buffer, const Element<U>& x);
- ~Element() {};
- };
- template<class T>
- ostream& operator<<(ostream& buffer, const Element<T>& x)
- {
- buffer << x.d;
- return buffer;
- }
- template<class T>
- class Stack
- {
- private:
- Element<T>* S;
- index max_size;
- int top;
- public:
- Stack(index N);
- bool isEmpty() const;
- bool isFull() const;
- void push(const Element<T>& x);
- Element<T> look_top();
- void pop();
- template<class U> friend ostream& operator<<(ostream& buffer, const Stack<U>& stack);
- ~Stack();
- };
- template<class T>
- ostream& operator<<(ostream& buffer, const Stack<T>& stack)
- {
- if (stack.isEmpty()) { buffer << "Stog je prazan."; return buffer; }
- else
- {
- for (int i = 0; i <= stack.top; i++) buffer << stack.S[i]<" ";
- return buffer;
- }
- }
- //konstruktori i destruktori
- template<class T>
- Stack<T>::Stack(index N) :max_size(N), top(-1)
- {
- S = new Element<T>[N];
- }
- template<class T>
- Stack<T>::~Stack()
- {
- delete[] S;
- }
- template<class T>
- bool Stack<T>::isEmpty() const
- {
- return top == -1;
- }
- template<class T>
- bool Stack<T>::isFull() const
- {
- return top == max_size - 1;
- }
- template<class T>
- void Stack<T>::push(const Element<T>& x)
- {
- if (isFull()) return;
- else
- {
- top += 1;
- S[top] = x;
- return;
- }
- }
- template<class T>
- Element<T> Stack<T>::look_top()
- {
- if (isEmpty()) return NULL;
- else
- {
- return S[top];
- }
- }
- template<class T>
- void Stack<T>::pop()
- {
- if (isEmpty()) return;
- else
- {
- top--;
- return;
- }
- }
- int main()
- {
- index n;
- cin >> n;
- Stack<int> stog(n);
- int unos;
- string naredba;
- while (1)
- {
- cout << "Stanje stoga: " << stog << endl;
- cin >> naredba;
- if (!naredba.compare("PUSH"))
- {
- cin >> unos;
- stog.push(unos);
- continue;
- }
- if (!naredba.compare("POP"))
- {
- stog.pop();
- continue;
- }
- if (!naredba.compare("exit"))
- {
- cout << "Izlaz" << endl;
- break;
- }
- cout << "Naredba nije poznata";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement