How to change my stack into a queue?

This is a discussion on How to change my stack into a queue? within the C++ Programming forums, part of the General Programming Boards category; Without using templates, how can I convert my stack class into a queue class? I can't find anything on the ...

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    How to change my stack into a queue?

    Without using templates, how can I convert my stack class into a queue class? I can't find anything on the web.

    Code:
    class Stack
    {
    private:
    	int stack[100];
    	int top;
    public:
    	Stack(): top(0) { }
    	~Stack() {}
    
    	void push(int x)
    	{
    		if (top < 100)
    			stack[top++] = x; 
    	}
    
    	int pop()
    	{
    		if (top > 0) 
    			return stack[--top];
    		else
    			return 0;
    	}
    
    	void clear() { top = 0; }
    	int getSize() { return top; }
    	int isEmpty() { return top == -1; }
    	int isFull() { return top == 100; }
    };

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The different between a stack and a queue is this:
    Stack: First in, Last out (FILO)
    Queue: First in, First out (FIFO)

    Now when using a fix size array its actually pretty easy. You just need to keep track of the apparent head and apparent tail and just make the array circular.

  3. #3
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I'm sure my logic is messed up, but is this a good start?

    Code:
    class Queue
    {
    private:
    	int stack[100];
    	int bottom;
    public:
    	Queue(): bottom(100) {}
    	~Queue() {}
    
    	void push(int x)
    	{
    		if (bottom > 0)
    			stack[bottom--] = x;
    	}
    
    	int pop()
    	{
    		if (bottom > 0)
    		{
    			return stack[bottom + 1];
    		}
    	}
    };
    I input 1 2 3, and I get this output..
    1122450
    1122450
    1122450
    Last edited by dxfoo; 11-06-2005 at 01:07 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In your pop you should change to top of the stack (or bottom, or whatever you call it). Otherwise you'll keep popping the same value.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    No luck..

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    heres how I could define the private variables:
    Code:
    class Queue
    {
      private:
        int stack[100];
        int* top;
        int* bottom;
    You then keep track of the top and bottom, you pop from the top and push to the bottom. As you pop and push the pointers get incremented and once they reach the end of the array they go back to teh beginning.

  7. #7
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Ok now I'm just confused. Thanks for the efforts. I guess I learn by seeing a full code example, and I practice rewriting it so I understand how it works. Visual learner? I don't know.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    class Queue {
    private:
        int queue[100];
        int top;
        int bottom;
    
    public:
        Queue() : top(0), bottom(0) {}
    
        void push(int x) {
            queue[top] = x;
    
            if(top >= 99) top = 0;
            else top ++;
        }
    
        int pop(void) {
            int x = queue[bottom];
    
            if(bottom <= 99) bottom = 0;
            else bottom ++;
    
            return x;
        }
    };
    Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 09:23 PM
  2. Stack and Queue
    By audinue in forum C Programming
    Replies: 7
    Last Post: 07-04-2008, 12:28 PM
  3. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 06:29 AM
  4. 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
  5. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM

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