Thread: Stack and Queue "quack" circular array

  1. #16
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    Daved,
    thank you for all your help! I do have updated code.
    I know i still need to check if full, but i will worry about that later.

    I did try what you suggested: back started at 6 and front started at 7.
    However, i couldnt get it in the right order

    This is what i have.

    Code:
    quack::quack(int capacity)
    {
    
    	items = new item[capacity];
    
    	items->n = 0;
        count = 0;
        back = capacity;           
        front = capacity-1;
    	maxsize = capacity;
        
        }
    Code:
    bool quack::pushFront(const int n)
    {
    
     if ( count == maxsize )
        {   
          
            return false;
        }
        front = (front - 1) % maxsize;
        items[front].n = n;
        count++;
        
            return true;    
    
               
       }
    Code:
    bool quack::pushBack(const int n)
    {
      back = (back - 1) % maxsize;
      items[back].n = n;
       count++;
        
            return true;   
    
    }
    If you look at the for loop to get it to display properly i had to
    start at 1 and add 1 to count. Now, that may go back to what you were suggestion before. However, it didnt work.

    Code:
    ostream& operator<<(ostream& out, quack& q)
    {
    	
    
      if ( q.count == 0 ) // no elements have been counted.
        out << endl << "quack: empty" << endl;
        else
        {
            out << endl << "quack: ";
            for ( int i = 1; i < q.count + 1; i++ )
            {
                   
            out << q.items[i].n << ", ";
                   
            }
            out << endl << endl;
        }
        return out;

    output is what i suppse to have

    Code:
    CS260 - Lab2 - Your Name
    
    quack: empty
    >>> pushFront 1 succeeded
    >>> pushFront 2 succeeded
    >>> pushFront 3 succeeded
    >>> pushFront 4 succeeded
    >>> pushBack 0 succeeded
    >>> pushFront 9 succeeded
    
    quack: 9, 4, 3, 2, 1, 0,
    
    --- # of items: 6
    
    >>> popFront failed
    
    quack: 9, 4, 3, 2, 1, 0,
    
    >>> popFront failed
    
    quack: 9, 4, 3, 2, 1, 0,
    
    >>> pushBack 7 succeeded
    
    quack: 9, 4, 3, 2, 7, 0, -33686019,
    
    >>> pushBack 8 succeeded
    Last edited by mrsirpoopsalot; 10-20-2009 at 03:31 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. queue and stack implemented with linked list
    By popapez in forum C Programming
    Replies: 14
    Last Post: 09-22-2009, 10:56 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Pushing a Queue onto a Stack?
    By MiroMage in forum C Programming
    Replies: 5
    Last Post: 10-14-2008, 09:23 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. queue / stack
    By smd in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2002, 01:30 PM