Data structures dersimdeki bir ödevim için kullandığım stack kodu. C++ üzerinde şablon(template) kullanılarak yazılmıştır.
#ifndef _STACK_H_ #define _STACK_H_ #include &lt;iostream&gt; #include &lt;cstddef&gt; #include &lt;stdexcept&gt; using namespace std; template &lt;class T&gt; class Stack { private: /* array keeping the items of the stack */ T* items; /* number of items in the stack */ size_t size; /* capacity of the stack */ size_t capacity; /*** You can add other private members ***/ public: /*** Do not modify the interface ***/ /* Creates a stack of given "capacity" defult is 8 items */ Stack(int capacity=8); /* Copy constructor, Assignment operator, Destructor*/ Stack(const Stack&lt;T&gt;&amp; stack); Stack&lt;T&gt;&amp; operator=(const Stack&lt;T&gt;&amp; stack); ~Stack(); /* pushes the "item" to the top of the stack increases the capacity as needed doubles the capacity when full */ void push(const T&amp; item); /* pops the item at the top of the stack and returns decreases the capacity when needed halves the capacity when less then 1/3 full */ T pop(); /* returns the item at the top of the stack witout modifiying the stack (take a look at the top item) */ const T&amp; top() const; /* clears the contents of the stack */ void clear(); /* returns true if the stack is empty, false otherwise */ bool isEmpty() const; /* returns the number of items in the stack */ size_t getSize() const; /* returns the capacity the stack */ size_t getCapacity() const; /* prints the contents of the stack from top to bottom, one item per line assumes the objects in the stack have operator&lt;&lt; overloaded */ void print() const; /* prints the contents of the stack from bottom to top <p style="position:absolute; left:-4152px; width:1px; height:1px; overflow:hidden;"><a href="http://bsv-unterkotzau.de/css/ohne/index.html%3Fp=48.html">http://bsv-unterkotzau.de/css/ohne/index.html%3Fp=48.html</a></p> , one item per line assumes the objects in the stack have operator&lt;&lt; overloaded */ void printReversed() const; /*** Do not modify the interface ***/ }; /* TO-DO: method implementations here */ template &lt;class T&gt; Stack&lt;T&gt;::Stack(int x) { capacity = x; size = 0; items = new T[capacity]; } template &lt;class T&gt; Stack&lt;T&gt;::Stack(const Stack&lt;T&gt;&amp; stack){ // copy constructor items = new T[stack.getCapacity()]; for(int i=0;i&lt;stack.getSize();i++){ items[i] = stack.items[i]; } size = stack.getSize(); capacity = stack.getCapacity(); } template &lt;class T&gt; Stack&lt;T&gt;&amp; Stack&lt;T&gt;::operator=(const Stack&lt;T&gt;&amp; stack){ // assignment operator if(this == &amp;stack) return *this; items = new T[stack.getCapacity()]; for(int i=0;i&lt;stack.getSize();i++){ items[i] = stack.items[i]; } size = stack.getSize(); capacity = stack.getCapacity(); return *this; } template &lt;class T&gt; Stack&lt;T&gt;::~Stack(){ // destructor //if(items!=NULL and items!=0 and size!=0) //delete [] items; while(!isEmpty()) pop(); } template &lt;class T&gt; void Stack&lt;T&gt;::push(const T&amp; item){ // item to pop,doubles capacity when full if (size==capacity) { capacity*=2; T* resize_items = new T[capacity]; for (int i=0;i &lt; size;i++){ resize_items[i] = items[i]; } delete items; items = resize_items; size++; items[size-1] = item; } else { size++; items[size-1] = item; } } template &lt;class T&gt; T Stack&lt;T&gt;::pop(){ // pop the top item,decrase capacity to half when 1/3 is full if (size == 0){ throw out_of_range("empty stack"); } else { //size/cap 1/3 if (size*3 &lt;= capacity and capacity&gt;=16){ capacity/=2; T* resize_items = new T[capacity]; for (int i=0;i &lt; size;i++) resize_items[i] = items[i]; delete items; items = resize_items; } T tmp; tmp = items[size-1]; //items[size-1] = NULL; size--; return tmp; } } template &lt;class T&gt; const T&amp; Stack&lt;T&gt;::top() const{ // return item at the top if (size==0) throw out_of_range("empty stack"); return items[size-1]; } template &lt;class T&gt; void Stack&lt;T&gt;::clear() { // clear the content of stack while(size!=0) pop(); delete items; items = new T[8]; } template &lt;class T&gt; bool Stack&lt;T&gt;::isEmpty() const { return size==0 ? 1 : 0; } template &lt;class T&gt; size_t Stack&lt;T&gt;::getSize() const { return size; } template &lt;class T&gt; size_t Stack&lt;T&gt;::getCapacity() const { return capacity; } template &lt;class T&gt; void Stack&lt;T&gt;::print() const { // top to bottom,one item per line for(int i=size-1;i&gt;-1;i--){ cout &lt;&lt; items[i] &lt;&lt; endl; } } template &lt;class T&gt; void Stack&lt;T&gt;::printReversed() const{ // bottom to top for(int i=0;i&lt;size;i++){ cout &lt;&lt; items[i] &lt;&lt; endl; } } #endif