Stack and Queues questions

This is a discussion on Stack and Queues questions within the C++ Programming forums, part of the General Programming Boards category; I am preparing for a test and have answered some questions dealing with stacks and queues the teacher has provided ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    14

    Stack and Queues questions

    I am preparing for a test and have answered some questions dealing with stacks and queues the teacher has provided as samples. Could someone check over my work?

    Code:
    // stack.h  complete header file for Stack ADT
    #include <iostream>
    using namespace std;
    
    class StackFull { /* Empty Exception Class */ };
    class StackEmpty { /* Empty Exception Class */ };
    class Stack // An array implementation of Stack ADT for storing integers
    {
    private:
    int* ptr; // Holds address of dynamically-allocated integer array
    int top; // Array index of top-most integer on the stack
    int size; // Maximum number of integers that may be stored in the stack
    
    public:
    Stack(int num); // Dynamically allocate 1-D array of integers of size num
                            // and configure empty stack
    ~Stack(); // Deallocates stack array
    void Push(int n); // Add integer n to the top of the stack if not full
                              // Otherwise, throw StackFull exception
    int Pop(); // Removes top integer from stack AND returns value to
                   // client; throws StackEmpty exception if stack is empty
    void MakeEmpty(); // Return stack to empty but usable state
    bool IsFull() const; // Return true if stack is full; otherwise return false
    bool IsEmpty() const; // Return true if stack is empty; otherwise return false
    int Capacity() const; // Returns the maximum number of integers that may be
                                    // stored in Stack object
    }; // End Class Stack
    1) Implement Stack.
    Code:
    Stack::Stack(int num)
    {
     ptr=new int[num];
     top=0;
     size=num;
    }
    2) Implement Push method
    Code:
    void Stack::Push(int n)
    {
     if(IsEmpty() )
       throw StackFull();
     else
     {
      ptr[top]=n;
      top++;
     }
    }
    3) Implement IsEmpty
    Code:
    bool Stack::IsEmpty() const
    {
     return (top == 0);
    }
    4) Implement Pop
    Code:
    int Stack::Pop()
    {
     if (IsEmpty())
       throw StackEmpty();
     else
     {
      int temp=ptr[top];
      top--;
      return temp;
     }
    }
    5) Implement Desctructor
    Code:
    Stack::~Stack()
    {
     MakeEmpty();
    }
    6) Implement Capacity
    Code:
    int Stack::Capacity() const
    {
     return size;
    }
    7) Implement IsFull
    Code:
    bool Stack::IsFull() const
    {
     return (top == size);
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you tested the code? It seems to have a pretty big bug which you should find easily.

    Other than that, such classes also require a user-defined copy constructor and assignment operator to perform a deep copy.

    Also you haven't shown the implementation of MakeEmpty. If it does what the comment suggests, then I don't see how the destructor can avoid a leaking memory.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    You need to provide your own copy constructor/operator, or make this class non-copyable (inherit from boost::noncopyable).
    I never put signature, but I decided to make an exception.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    The header code was provided and the questions 1-7 were multiple choice questions I had to answer. I am not writing specific code, just using the header to write a few functions. Just wanted to check and see if I got each one right.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Oh, so these points were actually questions.
    You still have a bug in one of your method - the Pop() method.
    I never put signature, but I decided to make an exception.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Quote Originally Posted by kmdv View Post
    Oh, so these points were actually questions.
    You still have a bug in one of your method - the Pop() method.
    Correct. Just had to chose between four sample codes provided or None of the above, because it is a multiple choice question. Are the others correct except Pop?

    Implement Pop
    Code:
    int Stack::Pop()
    {
     if (IsEmpty())
       throw StackEmpty();
     else
     {
      return ptr[top];
      top--;
     }
    }

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    int Stack::Pop()
    O_o

    You have no idea how much seeing that upsets me.

    Anyway, anon wasn't teasing you. You have a big bug and it isn't localized to any one function.

    Put the code into a testbed and see what happens.

    Soma

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Quote Originally Posted by phantomotap View Post
    O_o

    You have no idea how much seeing that upsets me.

    Anyway, anon wasn't teasing you. You have a big bug and it isn't localized to any one function.

    Put the code into a testbed and see what happens.

    Soma
    Read the other posts. This is not code I am testing. Those 1-7 were questions are dealing with the header file up top.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    why does seeing that upset you?

    anyways for the int pop..if the first if statement part executes you need to return something..that is one bug

    also looks like you don't clean up after yourself..so I assume that option is incorrect
    You ended that sentence with a preposition...Bastard!

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    why does seeing that upset you?
    Let me just say that in real life C++ stack implementations such a signature causes a problem.

    Read the other posts. This is not code I am testing.
    I'm well aware. If you threw the code at a testbed you would know that those options are the wrong ones and have an idea about what the right ones would look like.

    Soma

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    @phantomtap: It is unclear whether such a signature causes a problem here.

    @OP: Which part is your answer? There appear to be bugs in both Push and Pop. These are your code?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Quote Originally Posted by anon View Post
    @phantomtap: It is unclear whether such a signature causes a problem here.

    @OP: Which part is your answer? There appear to be bugs in both Push and Pop. These are your code?
    Not my code, for each question there are 4 code options to choose from and also 'none of the above'. So for those, pop and push, I have chose the incorrect one. When I get back I will try to chose the correct one and post it. Most choices are very similar with just 1-2 lines different.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    14
    Quote Originally Posted by anon View Post
    @phantomtap: It is unclear whether such a signature causes a problem here.

    @OP: Which part is your answer? There appear to be bugs in both Push and Pop. These are your code?
    Here is my correction for Push and Pop.

    Implement Push
    Code:
    void Stack::Push(int n)
     if (IsFull())
      throw StackFull();
     else
     {
      top++;
      ptr[top]=n;
     }
    }
    Implement Pop
    Code:
    int Stack::Pop()
    {
     if (IsEmpty())
      throw StackEmpty();
     else
     {
      top--;
      return ptr[top];
     }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2010, 06:04 AM
  2. queue and stack implemented with linked list
    By popapez in forum C Programming
    Replies: 14
    Last Post: 09-22-2009, 10:56 PM
  3. Replies: 1
    Last Post: 05-04-2009, 07:54 PM
  4. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 02:55 AM
  5. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 09:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21