ISourceCode

Make the frequent cases fast and the rare case correct

Implementation of Stack in C++ with Push and Pop Operations

The following is tested code of a stack implementation using C++. I have used templates to illustrate their usage. Push and Pop ADT is illustrated. The stack is a Last In First Out data structure

#include <iostream>

using namespace std;
//Define the stack class, set default stack size to 2
//use a template to define type at later point
template<class T,int size=2>
class stack{
	private: T data[size];
	int stack_ptr;
	public:
	stack(void);
	void push(T x);
	T pop();
};
//constructor function to initialize stack and data
template<class T,int size>
stack<T,size>::stack(void)
{
	int i;
	for(i=0;i<size;i++) data[i]=0;
	stack_ptr=0;
}
//Push data onto stack
template<class T,int size>
void stack<T,size>::push(T x)
{
	if(stack_ptr>=size)
	{
		cout<<"cannot push data: stack full"<<endl;
		return;
	}
	data[stack_ptr++]=x;
	cout<<"Pushed\t" << x << "\tonto the stack"<<endl;
	return;
}
//Pop data from stack
template<class T,int size>
T stack<T,size>::pop()
{
	if(stack_ptr<=0)
	{
		cout<<"cannot pop data: stack empty"<<endl;
		return data[0];
	}
	cout<<"popped\t"<< data[--stack_ptr]<< "\tfrom stack"<<endl;
	return data[stack_ptr];
}
int main()
{
	//create stack of integers
	stack<int,10> s;
	s.push(45);
	s.pop();
 	//try to pop an empty stack
	s.pop();
	s.push(56);
	s.push(29);
	s.push(31);
	s.pop();
	s.pop();
	//create stack of doubles
	stack<double,2> d;
	d.push(4.5);
	d.push(5.9);
	//trying to pushing on a full stack
	d.push(7.2);
	d.pop();
	d.pop();
	//trying to pop an empty stack
	d.pop();
	//declaring stack of characters and using default size
	stack<char> c;
	char w;
	c.push('n');
	c.push('l');
	//trying to push on full stack
	c.push('w');
	c.pop();
	//pick up the stack value
	w=c.pop();
	cout<<"Picked the character ***"<< w << "*** which was popped from top"<<endl;
	//trying to pop an empty stack
	c.pop();
	return 0;
}

OUTPUT:

laptop:~/code$ ./a.out
Pushed 45 onto the stack
popped 45 from stack
cannot pop data: stack empty
Pushed 56 onto the stack
Pushed 29 onto the stack
Pushed 31 onto the stack
popped 31 from stack
popped 29 from stack
Pushed 4.5 onto the stack
Pushed 5.9 onto the stack
cannot push data: stack full
popped 5.9 from stack
popped 4.5 from stack
cannot pop data: stack empty
Pushed n onto the stack
Pushed l onto the stack
cannot push data: stack full
popped l from stack
popped n from stack
Picked the character ***n*** which was popped from top
cannot pop data: stack empty

In future i will implement a FIFO data structure i.e Queue

About these ads

4 responses to “Implementation of Stack in C++ with Push and Pop Operations

  1. Dannida March 2, 2011 at 3:42 pm

    Your code helped me to some extent. I would be glad if you could add search and empty.

  2. hussain October 17, 2011 at 2:28 am

    nice and send to easy program okay

  3. Naeem iqbal November 5, 2012 at 4:26 am

    when you push 56 ,29,31 to stack and than pop() call 2 time and than you push 4.5 and 5.9
    now how many element in stack?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: