0 Replies - 146 Views - Last Post: 11 July 2010 - 10:44 PM

#1 alias120   User is offline

  • The Sum over All Paths
  • member icon

Reputation: 125
  • View blog
  • Posts: 706
  • Joined: 02-March 09

Stack

Posted 11 July 2010 - 10:44 PM

Description: -Compiled in VS C++ Express Edition

-Using the C++ Standard Library
Implements a simple stack of integers. Can be modified to push user defined integers.
#include<iostream>

using namespace std;

#define MAX 10

class Stack
{
	int val[MAX];
	int top;
public:

	Stack();
	~Stack();

	void push(int a);
	void pop();

};

Stack::Stack()
{
	top = -1;
}

Stack::~Stack()
{
	cout<<"Desctructing"<<endl;
}

void Stack::push(int a)
{
	if(top!=MAX)
	{
		top++;
		val[top] = a;
		cout<<val[top]<<" is pushed"<<endl;
		cout<<endl;
	}
	else
	{
		cout<<"Stack is full"<<endl;
	}

}

void Stack::pop()
{
	if(top==-1)
	{
		cout<<"Stack is empty!"<<endl;
	}
	else
	{
		cout<<val[top]<<" is popped"<<endl;
		cout<<endl;
		val[top] = NULL;
		top--;
	}

}


int main()
{
	Stack obj;

	obj.push(10);
	obj.push(20);
	obj.push(30);

	obj.pop();
	obj.pop();
	obj.pop();

	cout<<endl;
	cin.get();

	return 0;

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1