Answer to Question #288762 in C++ for Roe

Question #288762

class Node{


public:


int data;


Node * next;


};


class LinkedList{


private:


Node * head;


public:


Node * head;


void insert(int value);


void delete(int value);


void reverse();


};



Task:


Make a stack with no duplicate elements.


Keeping in mind the above class exists.

1
Expert's answer
2022-01-19T10:35:51-0500
#ifndef Stack_h
#define Stack_h
	using namespace  std;
	const int MAX = 3;
	template <class T>
	class Stack {
	private:
	    int top;
	    T st[MAX];
	public:
	    class Full:public exception {};
	    class Empty :public runtime_error {
	    public: Empty(const char* msg, int f):runtime_error(msg){
	        this->n = f;
	        cout<<n<<endl;}
	        int n;
	    };
	    Stack();
	    void push(T value);
	    T pop ();
	};
	

	template <class T>
	Stack<T>::Stack():top(-1) {}
	

	template <class T>
	void Stack<T>::push(T value) {
	    st[++top] = value;
	    if (top>MAX-1) {
	        throw Full();
	    }
	}
	

	template <class T>
	T Stack<T>::pop() {
	    if(top<0) {
	        throw Empty("Stack is empty!", 5);
	    }
	    return st[top--];
	}
	

	#endif /* Stack_h */

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog