954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

mel2005
Newbie Poster
24 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

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.

aman_yadav11
Newbie Poster
1 post since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

So you have a stack of queues and you want to convert it to a queue of queues?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

yes, the right, please help

mel2005
Newbie Poster
24 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

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

mel2005
Newbie Poster
24 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

can you not post up the correct code? It may be of some help to others?

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

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

mel2005
Newbie Poster
24 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You