DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   at the moment this is a stack of queue, i am trying to make it into first in first ou (http://www.daniweb.com/forums/thread21077.html)

mel2005 Mar 29th, 2005 6:39 am
at the moment this is a stack of queue, i am trying to make it into first in first ou
 
at the moment this is a stack of queue, i am trying to make it into first in first out, (FIFO) , can anyone help. thank you very much
#include <iostream.h>
#include <stdlib.h>

const int STACK_SIZE = 6;
const int NIL = -1;
const int LAST_ELEMENT = STACK_SIZE - 1;

struct  stackType
{
        int tos;
        char data[STACK_SIZE];
};



void create(stackType&);
bool full(stackType);
bool empty(stackType);
void Push(char, stackType& );
void Pop(char&,stackType& );
void Top( char&, stackType);

void display(stackType);

void create(stackType& s)
{
        s.tos = NIL;
}

bool full(stackType s)
{
        if(s.tos == LAST_ELEMENT)
                return true;
        else
                return false;
}

bool empty(stackType s)
{
        if (s.tos == NIL)
                return true;
        else
                return false;
}

void Push(stackType& s, char achar)
{
        s.tos = s.tos + 1;
        s.data[s.tos] = achar;
}

void Pop(stackType& s, char& achar)

{
        achar = s.data[s.tos];
        s.tos = s.tos + 1;
}

void display(stackType s)
{
        int i;
        for (i = s.tos; i != NIL; i++)
                cout << s.data[i] << endl;
}

void Top(stackType s, char& achar)
{
        achar = s.data[s.tos];
}

void main()
{
        stackType aStack[3];

        char command, letter;
        int use_stack = 0;
        system("cls");

        for (int i = 0 ; i<3 ; i++) create(aStack[i]);

        cout << "This program uses a stack implemented as an array\n";
        cout << "Maximum number of items = " << STACK_SIZE << endl;

        do
        {
                cout << "\nMAKING USE OF STACK " << use_stack;
                cout << "\n[P]ush data, P[O]p data, [T]op show top data";
                cout << "\n[D]isplay stack, [S]witch stack or [Q]uit -> ";
                cin >> command;
                switch(command)
                {
                        case 'p' :
                        case 'P' :        if(!full(aStack[use_stack]))
                                                        {
                                                                cout << "Enter data -> ";
                                                                cin >> letter;
                                                                Push(aStack[use_stack],letter);
                                                        }
                                                        else
                                                                cout << "The stack is full data not pushed\n";
                                                                break;
                        case 'o' :
                        case 'O' :        if (!empty(aStack[use_stack]))
                                                        {
                                                                Pop(aStack[use_stack], letter);
                                                                cout << letter << " has been popped\n";
                                                        }
                                                        else
                                                                cout << "Stack empty cannot pop\n";
                                                        break;
                        case 't' :
                        case 'T' :        if (!empty(aStack[use_stack]))
                                                        {
                                                                Top(aStack[use_stack], letter);
                                                                cout << letter << " is on top of the stack\n";
                                                                cout << "\n";
                                                        }
                                                        else
                                                                cout  << "Stack empty cannot pop\n";
                                                        break;
                        case 's' :
                        case 'S' :        do
                                                        {
                                                                cout << "Which stack do you wish to use (0-2) ";
                                                                cin >> use_stack;
                                                        } while (use_stack < 0 || use_stack > 2);
                                                        break;
                        case 'd' :
                        case 'D' :        if (!empty(aStack[use_stack]))
                                                                display(aStack[use_stack]);
                                                        else
                                                                cout << "stack empty no data to display\n";
                                                                break;
                        case 'q' :
                        case 'Q' :        cout << "Program terminated\n";
                                                        break;
                        default  :        cout << "Unknown command [" <<command << "] try again!\n";
                                                        break;
                        }
        }        while (!(command == 'q' || command == 'Q'));
}
Code tags added. -Narue

aman_yadav11 Mar 29th, 2005 7:10 am
Re: at the moment this is a stack of queue, i am trying to make it into first in first ou
 
i have read your post of c and c++ . this post increase my c and c++ foundamental knowledge and i am thankful to your resoruces and organisation.

Narue Mar 29th, 2005 9:27 am
Re: at the moment this is a stack of queue, i am trying to make it into first in first ou
 
So you have a stack of queues and you want to convert it to a queue of queues?

mel2005 Mar 29th, 2005 9:49 am
Re: at the moment this is a stack of queue, i am trying to make it into first in first ou
 
yes, the right, please help

mel2005 Mar 30th, 2005 8:46 am
Re: at the moment this is a stack of queue, i am trying to make it into first in first ou
 
hello everyone, just to let you know, i don't need help on this thread, i've done it, took me all night. anywhere thanks, have you got any idea how i can remove this thread

Acidburn Mar 30th, 2005 10:45 am
Re: at the moment this is a stack of queue, i am trying to make it into first in first ou
 
can you not post up the correct code? It may be of some help to others?

mel2005 Mar 31st, 2005 4:53 am
Re: at the moment this is a stack of queue, i am trying to make it into first in first ou
 
yes, so i can, i will most probably post it by tommorrow, and i will also post my final piece soon. all i need to do now it switch to different queues, and that the hard bit


All times are GMT -4. The time now is 5:09 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC