I didnt get exactly what do you mean by stack class template, but this is one which implements a stack! and also shows how to implement it with templates
http://thetechnofreaks.com/2011/10/26/4-creating-a-stack/#ixzz1bvq2V1Ws
No, because C does not support the concept of template functions. Template functions only exist in C++, never in C.
Template class: A generic definition or a parametrized class not instantiated until the client provides the needed information. It?s jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It?s jargon for plain classes. Class template is a template used to generate template classes. We cannot declare an object of a class template. Template class is an instance of a class template.
The following function template will reverse any stack of type T: template<typename T> void reverse_stack (sd::stack<T>& A) // Pass by reference! { std::stack<T> B, C; // Two additional stacks (initially empty). while (!A.empty()) { B.push (A.top()); A.pop(); } while (!B.empty()) { C.push (B.top()); B.pop(); } while (!C.empty()) { A.push (C.top()); C.pop(); } // A is now in reverse order. } A more efficient method is to pop the stack onto a queue. template<typename T> void reverse_stack_optimised (sd::stack<T>& A) // pass by reference! { std::queue<T> B; while (!A.empty()) { B.push_back (A.top()); A.pop(); } while (!B.empty()) { A.push (B.front()); B.pop(); } // A is now in reverse order. }
The c language does not have template functions. That is a c++ thing.
include <iostream> using namespace std; #define SIZE 10 template <class StackType> class stack { StackType stck[SIZE]; int topOfStack; public: void init() { topOfStack = 0; } void push(StackType ch); StackType pop(); }; template <class StackType> void stack<StackType>::push(StackType ob) { try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout << "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++; } template <class StackType> StackType stack<StackType>::pop() { try { if( topOfStack == 0) throw 0; } catch(int) { cout << "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack]; } int main() { stack<char> stack1, stack2; int i; stack1.init(); stack2.init(); stack1.push('a'); stack2.push('x'); stack1.push('b'); stack2.push('y'); stack1.push('c'); stack2.push('z'); for(i = 0; i <3; i++) cout << "Pop stack1: " << stack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop stack2: " << stack2.pop() << endl; // demonstrate double stacks stack<double> doubleValueStack1, doubleValueStack2; // create two stacks // initialize the stacks doubleValueStack1.init(); doubleValueStack2.init(); doubleValueStack1.push(1.1); doubleValueStack2.push(2.2); doubleValueStack1.push(3.3); doubleValueStack2.push(4.4); doubleValueStack1.push(5.5); doubleValueStack2.push(6.6); for(i = 0; i <3; i++) cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl; return 0; }
No, because C does not support the concept of template functions. Template functions only exist in C++, never in C.
Template class: A generic definition or a parametrized class not instantiated until the client provides the needed information. It?s jargon for plain templates. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed. It?s jargon for plain classes. Class template is a template used to generate template classes. We cannot declare an object of a class template. Template class is an instance of a class template.
The standard template library (STL) already includes a stack container. There is no need to develop your own. If you need to see the implementation details, consult the C++ ISO standard 23.6.5.2.
Use a vector with a base class type. Any objects derived from the base class can be pushed and popped from the vector just as you would from a stack.
The following function template will reverse any stack of type T: template<typename T> void reverse_stack (sd::stack<T>& A) // Pass by reference! { std::stack<T> B, C; // Two additional stacks (initially empty). while (!A.empty()) { B.push (A.top()); A.pop(); } while (!B.empty()) { C.push (B.top()); B.pop(); } while (!C.empty()) { A.push (C.top()); C.pop(); } // A is now in reverse order. } A more efficient method is to pop the stack onto a queue. template<typename T> void reverse_stack_optimised (sd::stack<T>& A) // pass by reference! { std::queue<T> B; while (!A.empty()) { B.push_back (A.top()); A.pop(); } while (!B.empty()) { A.push (B.front()); B.pop(); } // A is now in reverse order. }
class is template of object and object is instance of a class
The c language does not have template functions. That is a c++ thing.
What do you mean by stack-refreshing? Anyway, there are no stack handling functions in the standard C library.
Standard Template Library (STL) is part of the C++ standard library. The <i>template</i> concept in C++ allows to define generic classes which may then be brought to life with concrete types. For example, one could define a template <i>list</i> with repective methods and operators (including an element to a list, finding an element, ...) without specifying the concrete type of the list elements. Later on, one simply defines, e.g., a list of addresses by first defining the class <i>address</i> and then defining a new class as list of this address type.
Mark Nelson has written: 'C++ programmer's guide to the standard template library' -- subject(s): C++ (Computer program language), Standard template library, C. 'C [plus plus] programmer's guide to the Standard Template Library' -- subject(s): C.
include <iostream> using namespace std; #define SIZE 10 template <class StackType> class stack { StackType stck[SIZE]; int topOfStack; public: void init() { topOfStack = 0; } void push(StackType ch); StackType pop(); }; template <class StackType> void stack<StackType>::push(StackType ob) { try { if(topOfStack==SIZE) throw SIZE; } catch(int) { cout << "Stack is full.\n"; return; } stck[topOfStack] = ob; topOfStack++; } template <class StackType> StackType stack<StackType>::pop() { try { if( topOfStack == 0) throw 0; } catch(int) { cout << "Stack is empty.\n"; return 0; } topOfStack--; return stck[topOfStack]; } int main() { stack<char> stack1, stack2; int i; stack1.init(); stack2.init(); stack1.push('a'); stack2.push('x'); stack1.push('b'); stack2.push('y'); stack1.push('c'); stack2.push('z'); for(i = 0; i <3; i++) cout << "Pop stack1: " << stack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop stack2: " << stack2.pop() << endl; // demonstrate double stacks stack<double> doubleValueStack1, doubleValueStack2; // create two stacks // initialize the stacks doubleValueStack1.init(); doubleValueStack2.init(); doubleValueStack1.push(1.1); doubleValueStack2.push(2.2); doubleValueStack1.push(3.3); doubleValueStack2.push(4.4); doubleValueStack1.push(5.5); doubleValueStack2.push(6.6); for(i = 0; i <3; i++) cout << "Pop doubleValueStack1: " << doubleValueStack1.pop() << endl; for(i = 0; i <4; i++) cout << "Pop doubleValueStack2: " << doubleValueStack2.pop() << endl; return 0; }
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.