Stack homework help!

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Stack homework help!

Postby zeicht » Fri Apr 09, 2004 1:06 am

Create a Stack class. Use a typedef to define an Entry_type data type (see page 58). Your Stack class should work with any datatype by changing the typedef. Make int the default data type. Implement your Stack as a static array of size 100. Include the standard operations on your stack: push, pop, top, empty, full. Initialize your stack in the constructor. If you want you may use an enumerated class to return error conditions: stack_overflow, stack_underflow, success. You do not have to follow this model, you may instead use C++ exception handling. In either case, you must implement error checking. Use const wherever appropriate.

Add to your stack the following:
# pop_top: removes the top entry from the Stack and returns its value as the output parameter t.
# clear: deletes all entries and leaves an empty Stack.
# size: leaves the Stack unchanged and returns a count of the number of entries in the Stack.

I know this is too long answer, any advise on how should i construct my stack.h file? I have the basic idea, but in an experienced point of view, i wonder how they would approach this problem. If you can please elaborate with code example. I will not cut and paste your code, my purpose here it to learn from other people's point of view on how to approach / design stack header files. In real world, we are not to change an stack.h files .. it's like reinventing the wheel. But somehow we are to create a stack.h. I guess for us to see how it really works.

Thanks.

- Mike
zeicht
 

Re: Stack homework help!

Postby Root » Fri Apr 09, 2004 10:23 am

zeicht wrote:In real world, we are not to change an stack.h files .. it's like reinventing the wheel. But somehow we are to create a stack.h. I guess for us to see how it really works.


The best way to find understanding of how wheels work is to invent your own. All invention is re-invention.

Anyhoo, this should give you a start:

Code: Select all
//stack.h

#ifndef STACK_H
#define STACK_H

//*****************************************************************************
class Stack
{
private:

   Entry_type   m_pBottom[100];
   Entry_type*   m_pTop;

public:

   Stack() : m_pTop(m_pBottom) { };
   void push(const Entry_type& e) { *m_pTop++ = e; };
   Entry_type pop() { const Entry_type e = *m_pTop--; return e; };
   bool empty() const { return m_pTop == m_pBottom; };
   bool full() const { return m_pTop == m_pBottom + 100; };

};   //End - Stack
//*****************************************************************************

#endif //STACK_H


Code: Select all
//main.cpp

typedef int Entry_type;

#include "stack.h"

//*****************************************************************************
int main(int argc, char* argv[])
{
   Stack s;

   //do some stuff with the stack

   return 0;
}
User avatar
Root
 
Posts: 191
Joined: Fri Oct 31, 2003 12:30 am

Postby zeicht » Fri Apr 09, 2004 6:24 pm

Thanks, that makes sense ...

...try to look at the stack.h file .... and see if you can figure out what's going on. Since I am sort of lost of what i'm doind exactly. I'd like to know if i have done something wrong in this header file.

Code: Select all
  1  typedef int Entry_type;
      2 #include <iostream>
      3
      4 const int maxstack = 20;
      5
      6 class Stack {
      7    public:
      8       Stack();
      9         void menu1();     // ask user to either choose int or char stack
     10         void menu2();     // ask user to pop, push, top, pop_top, etc ..
     11         void switch1();   // switch cases for int stack
     12         void switch2();   // switch cases for char stack
     13         Error_code pop();
     14         Error_code push( const Entry_type &item );
     15         Error_code top( Entry_type &item ) const;
     16         Error_code pop_top( Entry_type &item );
     17         bool empty() const;
     18         void clear();
     19         bool full() const;
     20         int size() const;
     21    private:
     22        int count;
     23        Entry_type entry[maxstack];
     24 };


Notice, this header files seems to be hectic ... hey that's how we invite stuff. :-)


- Mike
zeicht
 

Postby Guest » Fri Apr 09, 2004 6:40 pm

ALSO ... continuing with the one that i just posted.

I'm having an "undefined " Error_code ...... what seems to be the problem?

- Mike
Guest
 

Postby Root » Sat Apr 10, 2004 1:31 pm

You should be using Include Guards in all your header files.

Anonymous wrote:I'm having an "undefined " Error_code ...... what seems to be the problem?


Well, have you defined 'Error_code' yet?
User avatar
Root
 
Posts: 191
Joined: Fri Oct 31, 2003 12:30 am

Postby Guest » Sat Apr 10, 2004 6:21 pm

I've already defined my Error_code already. My problem with that is over. However, another one came up.

Code: Select all
     Error_code Stack::top( Entry_type &item ) const
     {
        Error_code outcome = success;
           if( count == 0 )
              outcome = underflow;
           else
              item = entry[count - 1];
     
        return outcome;
     }


Here's what the compiler says:

Code: Select all
cxx: Info: stack.cpp, line 95: variable "outcome" was set but never used
  Error_code outcome = success;
-------------^


Thanks.
Mike
Guest
 

Postby Root » Sat Apr 10, 2004 11:29 pm

I'm not sure where the error is and can't really help without more code. Though, you might want to use exception for error checking instead of the Error_code as there are already C++ exception for just this type of error. This is what I mean:

Code: Select all
#include <iostream>

using namespace std;

//*****************************************************************************
template<typename TYPE, unsigned MAX>
class Stack
{
   friend ostream& operator<<(ostream&, const Stack&);

private:

   TYPE   m_pBottom[MAX];
   TYPE*   m_pTop;

public:

   Stack() : m_pTop(m_pBottom) { };
   bool empty() const { return m_pTop == m_pBottom; };
   bool full() const { return m_pTop == m_pBottom + MAX; };
   unsigned size() const { return m_pTop - m_pBottom; };
   void clear() { m_pTop = m_pBottom; };

   void push(const TYPE& e) {
      if (full()) throw overflow_error("Stack::push()");
      *m_pTop++ = e;
   };

   void pop(TYPE& e) {
      if (empty()) throw underflow_error("Stack::pop()");
      e = *m_pTop--;
   };   

};

//-----------------------------------------------------------------------------
template<typename TYPE, unsigned MAX>
ostream& operator<<(ostream& out, const Stack<TYPE, MAX>& stack)
{
   for (const TYPE* p = stack.m_pTop; p-- != stack.m_pBottom; )
      out << *p << endl;

   return out;
};

//*****************************************************************************
int main(int argc, char* argv[])
{
   try
   {
      Stack<int, 100> s;

      cout << std::boolalpha;

      for (unsigned i = 0; i <= 200; i++)
         s.push(i);

      cout << s << endl;
      cout << s.full() << endl;
      cout << s.empty() << endl;
   }
   catch (exception& e) {
      cerr << "Exception: " << typeid(e).name() << endl;
      cerr << "Thrown from: " << e.what() << endl;
   }

   return 0;
}
User avatar
Root
 
Posts: 191
Joined: Fri Oct 31, 2003 12:30 am

Postby Guest » Sun Apr 11, 2004 1:54 am

I see where you're going. However, we are to use "exception handling" ...

I know it is difficult to figure out what's wrong with my codes without any more codes for clarity. Anyhow, i will have to figure this myself.

- Mike :-(
Guest
 


Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 1 guest