Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

stack implementation using class in c++

#include<iostream>
using namespace std;
#define Size 5

class Stack
{
private:
	int Array[Size];
	int top;
public:
	Stack()
	{
		top = -1;
	}
	void Push(int x)
	{
		if (top == Size - 1)
		{
			cout << "Error, stack overFlow!" << endl;
			return;
		}

		Array[++top] = x;
	}
	void Pop()
	{
		if (top == -1)
		{
			cout << "Error, stack is Empty!" << endl;
			return;
		}
		top--;
	}

	int Top()
	{
		return Array[top];
	}
	bool IsEmpty()
	{
		if (top == -1)
			return 1;
		return 0;
	}
	void print()
	{
		cout << "Stack: ";
		for (int i = 0; i <= top; i++)
		{
			cout << Array[i] << " ";
		}
		cout << "
";
	}

};
int main()
{
	Stack s;
	s.Push(1);
	s.Push(2);
	s.print();

	return 0;
}
Source by codejudge.blogspot.com #
 
PREVIOUS NEXT
Tagged: #stack #implementation #class
ADD COMMENT
Topic
Name
7+4 =