Stacks Are Confusing

This is a discussion on Stacks Are Confusing within the C++ Programming forums, part of the General Programming Boards category; // project2.cpp : Defines the entry point for the console application. //HELP ME I NEED TO DETERMINE BOUNDARY CONDITIONS IN ...

  1. #1
    student1115
    Guest

    Angry Stacks Are Confusing

    // project2.cpp : Defines the entry point for the console application.
    //HELP ME I NEED TO DETERMINE BOUNDARY CONDITIONS IN THE
    //STACK

    //I ALSO NEED TO CHECK IF STACK IS EMPTY..SHOULD I MAKE //ANOTHER FUNCTION

    //#include "stdafx.h"
    #include "iostream.h"

    class Stack {
    private:

    char *top;
    char *bottom;



    public:
    Stack();
    ~Stack();
    void push(char); // add a char to the stack
    char pop(); // remove a char from the stack


    };




    Stack::Stack()
    {
    top = new char[20]; //dynamic memory allocation
    bottom = top; //set bottom = top



    }


    Stack::~Stack()

    {

    delete top;

    }


    void Stack:ush( char c )
    {

    determine boudary conditions
    if (*top > 1 && *top < 100)
    {
    ++top;

    *top = c;
    }
    }


    char Stack:op()
    {

    // determine how to verify stack is not empty
    //hint: check pointers..if pointers are equal.....

    //if empty return character you defined

    char ch;

    ch = *top;
    --top;

    return(ch);
    }






    int main(int argc, char* argv[])
    {
    cout<<"Hello World!\n";

    Stack *p_Stack;

    p_Stack = new Stack;

    p_Stack->push('a');
    p_Stack->push('b');
    p_Stack->push('c');
    p_Stack->push('d');
    cout << p_Stack->pop();

    cout << endl;


    return 0;
    }

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    //I ALSO NEED TO CHECK IF STACK IS EMPTY..SHOULD I MAKE //ANOTHER FUNCTION
    I would implement a new data-member named size. This would keep track how many elements currently are in the stack. Create a function like isEmpty() to achive this (the user of your class shoudnīt have direct acces to size).

    I donīt really understand your BOUNDARY CONDITIONS??? I would guess that you mean that a number that you push on the stack must be between x and y?!?!? If you explain it more i detail it would sertenly help.

    Iīve also detected a memory leak in your prog.
    In your destructor it should be
    Code:
    Stack::~Stack()
    {
    delete []top;
    }

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    :-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    namespace ansley {
      class stack {
        char *base;
        int   top;
        int   limit;
      public:
        explicit stack(int);
        ~stack();
    
        void push(char);
        char pop();
    
        bool empty() const;
        bool full() const;
      };
    
      stack::stack(int init_limit)
        : limit(init_limit), top(0)
      {
        base = new char[limit];
      }
    
      stack::~stack()
      {
        delete [] base;
      }
    
      void stack::push(char new_item)
      {
        if (!full())
        {
          base[top++] = new_item;
        }
        else
        {
          cerr<<"Stack is full"<<endl;
        }
      }
    
      char stack::pop()
      {
        if (!empty())
        {
          return base[--top];
        }
        else
        {
          cerr<<"Stack is empty"<<endl;
          return 0; // Return a safe value
        }
      }
    
      bool stack::empty() const
      {
        return top == 0;
      }
    
      bool stack::full() const
      {
        return top == limit;
      }
    }
    
    int main()
    {
      ansley::stack mystack(3);
    
      mystack.push('a');
      mystack.push('b');
      mystack.push('c');
      mystack.push('d');
    
      cout<< mystack.pop() <<endl;
      cout<< mystack.pop() <<endl;
      cout<< mystack.pop() <<endl;
      cout<< mystack.pop() <<endl;
    }
    *Cela*

  4. #4
    student1115
    Guest

    Stacks are Confusing

    Thanks for your replay...I understand more clearly now..
    but yes I do need boundary conditiions between 0 and 20 and i must use pointers...

    I guess the instructor wants us to see the difference between using pointers in a stack from using arrays

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help Me With This Code Of Implementing Stacks
    By raghu_equinox in forum C Programming
    Replies: 3
    Last Post: 10-19-2006, 07:22 AM
  2. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  3. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  4. Trouble with Linked List Stacks
    By ChristianTool in forum C Programming
    Replies: 6
    Last Post: 05-11-2004, 02:02 PM
  5. Stacks stacks stacks
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-06-2002, 02:01 AM

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