Demo entry 6656135

1

   

Submitted by anonymous on Oct 29, 2017 at 10:34
Language: C++. Code size: 629 Bytes.

#include <iostream>

class MyIntStack{
	int *p;
	int size;
	int tos;
public:
	MyIntStack(){
		p=NULL;
		tos=0;
	}
	MyIntStack(int size){
		this->size=size;
		p=new int[size];
		tos=0;
	}
	MyIntStack(MyIntStack& s){
		this->size=s.size;
		p=new int[size];
		this->tos=s.tos;
		for(int i=0;i<s.tos;i++){
			p[i]=s.p[i];
		}
	}
	~MyIntStack(){
		if(p!=NULL)
			delete []p;
	}
	bool push(int n){
		if(tos<size){
			p[tos]=n;
			tos++;
			return true;
		}
		else
			return false;
	}
	bool pop(int &n){
		if(tos>0){ 
			tos--;
			n=p[tos];
			return true;
		}
		else
			return false;
	}
};

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).