Thread: Check my code Please

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    16

    Check my code Please

    I've been given an assignment to make 3 template classes for stacks. The 1st is for a static stack. The 2nd for dynamic stacks. The 3rd is for linked lists. The code for the first one is below. I would appreciate someone going over it and seeing if my thinking and code were right. Thank You

    #ifndef Main_Savitch_Stack1_H
    #define Main_Savitch_Stack1_H
    #include <cstdlib>

    namespace main_savitch_stack1_H
    {
    template <class Item>
    class stack
    {
    public:

    typedef std::size_t size_type;
    typedef Item value_type;
    static const size_type CAPACITY = 30;
    stack(){used=0;}


    void push(const Item& entry);
    void pop( );

    bool empty( ) const { return (used == 0); }
    size_type size( ) const { return used; }
    Item top( ) const;

    private:
    Item data[CAPACITY];
    size_type used;
    };
    }


    //IMPLEMENTATION BEGINS HERE

    #include <cassert>

    template <class Item>
    void stack<item>:ush(const item& entry)
    {
    assert (size())<capacity);
    data[used]=entry;
    ++used;
    }

    template <class Item>
    void stack<item>:op()
    {
    assert (!empty());
    if (!empty())
    --used;
    }


    template <class item>
    item stack<item>::top()
    {
    assert(!empty());
    return data[used-1]
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It was full of syntax errors, but I managed to clear them up. This code at least compiles, but I didn't test to see if the processing is correct or not. The static const member of your class caused my compiler fits despite the correct syntax so I axed it and used a #define macro instead, you can change that as you see fit.
    Code:
    #define CAPACITY 30
    namespace Main_Savitch_Stack1_H
    { 
      template <class Item> 
      class stack 
      { 
      public:    
        stack ( void ) { used=0; } 
        void push ( const Item& entry ); 
        void pop ( void ); 
        bool empty ( void ) const { return ( used == 0 ); } 
        size_t size ( void ) const { return used; } 
        Item top ( void ); 
      private: 
        Item data[CAPACITY]; 
        size_t used; 
      }; 
    }
    
    // stack class method fleshy
    #include <cassert> 
    using namespace Main_Savitch_Stack1_H;
    
    template <class Item> 
    void stack<Item> :: push ( const Item& entry ) 
    { 
      assert ( size() ) < capacity ); 
      data[used] = entry; 
      ++used; 
    } 
    
    template <class Item> 
    void stack<Item> :: pop ( void ) 
    { 
      assert ( !empty() ); 
      if ( !empty() ) 
        --used; 
    } 
    
    template <class Item> 
    Item stack<Item> :: top ( void ) 
    { 
      assert( !empty() ); 
      return data[used - 1] 
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code Check
    By Dae in forum C++ Programming
    Replies: 12
    Last Post: 01-08-2009, 07:01 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM