Thread: Stack functions

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    Smile Stack functions

    Hi , I've been trying to write a program that will stack numbers in a row (while the number is less than 9). The pop function which I've written is supposed to remove all of the elements that are more than 9 and list them as being popped. The problem I am having now is that I want to be able to "peek" into the stack before the numbers are "popped" and after they are "popped".
    I've written the "peek" function but am unable to get the correct answer. It only returns one number in the current stack instead of all of them (before pop).
    Also i wanted to reverse the order of my push/pop so that the number greater then 9 is the one that gets popped but I'll figure that one out. Please let me know if you have any ideas because I am not receiving any error messages when I compile which would tell me what's wrong with the program.

    Thanks
    Special thanks to Elad and Shakti

    Code:
    #include <iostream.h>
    const int Stack_Size=100;
    
    
    class Stack
    {
    
    public:
    	Stack();
    	void push(int);
    	int pop();
    	int peek();
    	bool empty();
    	bool full();
    
    	private:
    	int count; 
    	int data[Stack_Size]; 
    
    };
    
    		Stack::Stack()
    		{
    			count=-1;
    		}
    		void Stack::push(int value)
    		{
    			count++;
    			data[count]=value;
    		}
    		int Stack::pop()
    		{
    			int topval;
    
    			topval=data[count];
    			count--;
    			return(topval);
    		}
    
    		int Stack::peek()
    		{
    		int elements;
    			
    			elements=data[count];
    			return (elements);
    		}
    		bool Stack::empty()
    		{
    			if (count==-1)
    				return 1;
    			else
    				return 0;
    		}
    		bool Stack::full()
    		{
    			if (count==Stack_Size-1)
    				return 1;
    			else
    				return 0;
    		}
    
    	
    	int main()
    		{
    			Stack digits;
    			int newnum;
    cout<<" Enter as many digits as you wish\n  ";
    
    		
    while(1)
    {
    	cout<<"enter a digit\n";
    	cin>>newnum;
    	if (newnum>9)
    		break;
    	if (digits.full())
                    {
    		cout<<"The stack is full and you do not have any space left\n";
                                    break;
                    }
    
                    else
                       digits.push(newnum);
    					
    }
    		cout<<"The values that are currently in the stack (peek) are\n"<<digits.peek()<<endl;
    
    	cout<<"The values that were popped from the stack are\n";
    	if (digits.empty())
    	{
    		cout<<"The stack is empty\n";
    		
    	}
    	else
    	
    	while (!digits.empty())
    	{
    		cout<<digits.pop()<<endl;
    	}
    
    
    	return 0;
    	}

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The reason you aren't getting any error messages is that all the code you have written is legal. When your code is legal and there is no run time error, then you need to start thinking about logic errors.
    Code:
    while(1)
    {
       cout<<"enter a digit\n";
       cin>>newnum;
       if (newnum>9)
         break;				
    }
    The above snippet means that if user enters a value > 9 the while loop will end, and the value of newnum will be ignored, not pushed on to stack. If you want to allow all values to be entered on to stack and then remove any value user has entered that is larger than 9, that's a different series of steps. Your code prevents the need to remove anything greater than 9, because those values aren't pushed in the first place.

    In your Peek function you only allow peeking at the top value of the stack. You don't ask to look lower down. To do so would require additional code, and I think it would violate the spirit of using a stack to begin with, but it can be done. To look further down you would need to pop the top and store it in another, temporary stack. That way you can keep track of all the values peeked at. When you are done peeking you can pop the values off the temporary stack and push them back on the original stack. As you are peeking, if you don't like a value you can remove it from the original stack by not pushing it on the temporary stack. The problem is I think of peeking as looking and replacing, but not removing. If you remove, you intervene, not just peek.

    Code:
    int Stack:Peek()
    {
      int elements;
    			
      elements=data[count];
      return (elements);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  5. Functions, stack
    By Ariod in forum C Programming
    Replies: 9
    Last Post: 07-28-2005, 03:52 AM