/*
* Copyright 2023, Ali Arya
* Anyone Can Code: Algorithmic Thinking
* Example 5.11c
*/

//simple program demonstrating queues
//source (with modifications):
//https://www.tutorialspoint.com/data_structures_algorithms/queue_program_in_c.htm

#include <stdio.h>
#include <iostream>
using namespace std;

#define MAX 4

/////////////////////////////////
// array print function

void Print(int Data[], int len)
{
	for (int i = 0; i < len; i++)
	{
		cout << Data[i];
		cout << " ";
	}
	cout << "\n";
}

/////////////////////////////////
// stack type

struct Stack
{
	int data[MAX]; 
	int count;
	int max; 
};

/////////////////////////////////
// stack functions

void InitStack(Stack* s)
{
	s->max = MAX;
	s->count = 0;
	for (int i = 0; i < MAX; i++)
		s->data[i] = -1; //not really necessary but let's assume -1 means unused
}

bool PeekStack(Stack* s, int* result)
{
	if (s->count != 0)
	{
		*result = s->data[s->count - 1];
		return true;
	}
	else
	{
		printf("Stack is empty.\n");
		return false;
	}
}

bool PopStack(Stack* s, int* result) 
{
	if (s->count != 0) 
	{
		*result = s->data[s->count-1];
		s->count--;
		return true;
	}
	else 
	{
		printf("Could not remove data. Stack is empty.\n");
		return false;
	}
}

bool PushStack(Stack* s, int newdata)
{
	if (s->count != s->max)
	{
		s->data[s->count] = newdata;
		s->count++;
		return true;
	}
	else
	{
		printf("Could not add data. Stack is full.\n");
		return false;
	}
}

///////////////////////////////////////////////////////////
//main function

int main()
{
	Stack sa, sb, sc;
	InitStack(&sa);
	PushStack(&sa, 4);
	PushStack(&sa, 3);
	PushStack(&sa, 2);
	PushStack(&sa, 1);
	InitStack(&sb);
	InitStack(&sc);

	bool quit = false;
	while (quit == false)
	{
		//show three bars
		cout << "A: ";
		Print(sa.data, sa.count);
		cout << "B: ";
		Print(sb.data, sb.count);
		cout << "C: ";
		Print(sc.data, sc.count);

		int command;
		int data = -1;
		bool success;

		//pop an item
		cout << "\n >> 0-exit 1-pop from A, 2-pop from B, 3-pop from C: ";
		cin >> command;
		switch (command)
		{
			case 0:
				quit = true;
				break;
			case 1:
				success = PopStack(&sa, &data);
				break;
			case 2:
				success = PopStack(&sb, &data);
				break;
			case 3:
				success = PopStack(&sc, &data);
				break;
		}
		if (quit)
			break; //end game
		if (!success)
			continue; //go to the next iteration

		//push the item
		cout << "\n >> 0-exit 1-push to A, 2-push to B, 3-push to C: ";
		cin >> command;
		switch (command)
		{
		case 0:
			quit = true;
			break;
		case 1:
			success = PushStack(&sa, data);
			break;
		case 2:
			success = PushStack(&sb, data);
			break;
		case 3:
			success = PushStack(&sc, data);
			break;
		}
	}
}