Thread: implementing a stack with template

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    implementing a stack with template

    How would you write a header file to implement a stack of const char* pointers using the above template class?? thanks guys

    Code:
    #include <cassert>
    #include <stdlib>
    
    template<class Type> class TArray
    {
      Type* _data;   // an array to store the data
      int _size;     // the number of elements in the array
      int _capacity; // the number of spaces currently available
    
      void growTo(int index)
      {
        if (index<_capacity)
          return;
        int newCapacity = _capacity?(2*_capacity):1;
        if (newCapacity<=index)
          newCapacity = index+1;
        Type* newData   = new Type[newCapacity];
        for (int i = 0; i<size(); i++) 
          newData[i] = _data[i]; 
        delete[] _data;
        _data     = newData;
        _capacity = newCapacity;
      }
    public:
      ~TArray<Type>() { delete[] _data;}
      TArray<Type>(unsigned int capacity = 0)
      {
        _size = 0;
        _capacity = capacity;
        if (capacity)
          _data = new Type[capacity];
        else
          _data = 0;
      }
    
      int size() const { return _size;}
    
      Type& operator[](int index)
      {
        assert(index>=0);
        if (index>=_capacity)
          growTo(index);
        if (index>=_size)
          _size = index+1;
        return _data[index];
      }
    };

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    82
    Presumably in your char-pointer stack class you would have a member of type TArray

    Something like:
    Code:
    // CHAR_PTR_STACK.H
    
    #include "T_ARRAY_FILE"
    
    class CharStack {
    
       private:
          TArray<char*> myStackData  // array holds char* elements
    
          // ....
    }
    Last edited by AH_Tze; 03-31-2005 at 06:31 PM.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Or for any type T:

    Code:
    template <class T>
    class Stack {
    
       private:
          TArray<T> myStackData 
    
          // ....
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    would anyone like to implement this and test it with ..

    Code:
    #include <iostream>
    using namespace std;
    
    #include "CStringStack.h"
    
    CStringStack stack;
    
    int main(int argc, char* argv[])
    {
      for (int i = 0; i<argc; i++) {
        stack.push(argv[i]);
      }
      while (!stack.empty()) {
        cout << stack.pop() << endl;
      }
    }

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    82
    This sounds a lot like either an assignment, or something you should be doing yourself.

    I like being helpful, but not doing your work for you. If you have a conceptual, syntactic, or just really !#$% annoying bug, you're sure to find some help. But asking someone to implement and test something is somewhat ridiculous.

    If you really don't want to do the coding, I'm sure you can find an existing example online. You just need to be as persistent with google as you are with the message board.

  6. #6
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Why don't you implement it yourself? After all, you might learn something, and you might enjoy it as well. And then it wouldn't seem too much like work. Homework, that is.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM