Add to Favorites    Make Home Page 947 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Beginners / Lab Assignments Home » A generic stack class

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title A generic stack class
Author Sourav Datta
Author Email soura_jagat [at] yahoo.com
Description
Category C++ » Beginners / Lab Assignments
Hits 390978
Code Select and Copy the Code
#include <iostream> #include <new> #include <string> #include <sstream> using namespace std; #if !defined __STACK_H #define __STACK_H namespace stk{ template<class T> class Stack; // Forward declaration of Stack class for overloaded << operator template<class T> ostream& operator<<(ostream &,Stack<T> &); // template declaration of << operator template<class T> class Stack{ private: T *p; int top,length; string str()const; public: Stack(); Stack(const int); Stack(const Stack<T>&); ~Stack(); void push(T); T pop(); int get_length()const; bool is_empty()const; Stack<T> operator=(const Stack<T>&); // only for basic types friend ostream& operator<< <>(ostream&,Stack<T> &); class StackException{ private: string desc; public: StackException(string exp){ desc="Exception : "+exp; } string get_exp(){ return desc; } }; }; template<class T> Stack<T>::Stack(){ top=-1; length=0; p=0; } template<class T> Stack<T>::Stack(const int size){ top=-1; length=size; try{ p=new T[length]; }catch(bad_alloc ba){ cout<<"Memory can not be alllocated "; return; } } template<class T> Stack<T>::Stack(const Stack<T> &o){ top=o.top; length=o.length; try{ p=new T[length]; }catch(bad_alloc ba){ cout<<"Memory allocation failed "; return; } for(int i=0;i<length;i++) p[i]=o.p[i]; } template<class T> Stack<T>::~Stack(){ if(p!=0) delete [] p; } template<class T> void Stack<T>::push(T elem){ if(p==0){ try{ p=new T[1]; }catch(bad_alloc ba){ throw StackException("Memory fault "); } length++; top++; p[top]=elem; } else if(top==(length-1)){ T *q; try{ q=new T[length+1]; }catch(bad_alloc ba1){ throw StackException("Memory fault "); } for(int i=0;i<length;i++) q[i]=p[i]; length++; top++; q[top]=elem; delete [] p; p=q; } else{ top++; p[top]=elem; } } template<class T> T Stack<T>::pop(){ if(p==0 || top==-1){ throw StackException("Stack empty! "); } T ret=p[top]; top--; length--; if(top==-1){ delete [] p; p=0; } else{ T *q; try{ q=new T[length]; }catch(bad_alloc ba){ throw StackException("Memory fault "); } for(int i=0;i<length;i++) q[i]=p[i]; delete [] p; p=q; } return ret; } template<class T> int Stack<T>::get_length()const{ return length; } template<class T> bool Stack<T>::is_empty()const{ return ((p==0)? true : false); } template<class T> string Stack<T>::str()const{ // private member function if(p==0) return string(""); stringstream ss; for(int i=0;i<length;i++){ ss << p[i]; if(i!=(length-1)) ss << ", "; } //ss<<" "; return ss.str(); } template<class T> Stack<T> Stack<T>::operator=(const Stack<T> &stk){ length=stk.length; top=stk.top; if(p!=0) delete [] p; try{ p=new T[length]; }catch(bad_alloc ba){ throw StackException("Memory fault in copying! "); } for(int i=0;i<length;i++) p[i]=stk.p[i]; return *this; } template<class T> ostream& operator<<(ostream &o,Stack<T> &s){ o<<s.str(); return o; } } // namespace stk; #endif

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1136


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review