Skip to content

Instantly share code, notes, and snippets.

@GregaMohorko
Last active July 30, 2017 20:59
Show Gist options
  • Save GregaMohorko/07788a70ada4c0de48a93e055f62bc76 to your computer and use it in GitHub Desktop.
Save GregaMohorko/07788a70ada4c0de48a93e055f62bc76 to your computer and use it in GitHub Desktop.
A static implementation of Stack & Circular Queue data structures.
// Stack is LIFO (Last In First Out)
// Circular Queue is FIFO (First In First Out)
#include <iostream>
#include <sstream>
using namespace std;
#define LENGTH_MAX 10
int stack[LENGTH_MAX];
// The top of the stack.
int top = -1;
int queue[LENGTH_MAX];
// Head of the circular queue.
int head = 0;
// Tail of the circular queue.
int tail = 0;
void stackPush(int value)
{
if((top + 1) == LENGTH_MAX) {
cout << "Stack is full!" << endl;
return;
}
stack[++top] = value;
}
int stackPop()
{
if(top == -1) {
cout << "Stack is empty!" << endl;
return 0;
}
return stack[top--];
}
void queueInsert(int value)
{
if(((tail + 1) % LENGTH_MAX) == head) {
cout << "Queue is full!" << endl;
return;
}
queue[tail++] = value;
tail %= LENGTH_MAX;
}
int queueRead()
{
if(head == tail) {
cout << "Queue is empty!" << endl;
return 0;
}
int value = queue[head++];
head %= LENGTH_MAX;
return value;
}
void outputStack()
{
for(int i = 0; i <= top; ++i) {
cout << stack[i] << " ";
}
}
void outputQueue()
{
int i = head;
while(i != tail) {
cout << queue[i++] << " ";
i %= LENGTH_MAX;
}
}
int showMenu();
int getInt(char *input);
int main()
{
while(true) {
switch(showMenu()) {
case 1: // Push to stack
{
int value = getInt("Insert a number: ");
stackPush(value);
break;
}
case 2: // Pop from stack
{
int value = stackPop();
cout << "Value popped from stack: " << value << endl;
break;
}
case 3: // Output entire stack
{
cout << "Entire stack:" << endl;
outputStack();
cout << endl;
break;
}
case 4: // Insert into queue
{
int value = getInt("Insert a number: ");
queueInsert(value);
break;
}
case 5: // Read from queue
{
int value = queueRead();
cout << "Value read from queue: " << value << endl;
break;
}
case 6: // Output entire queue
{
cout << "Entire queue:" << endl;
outputQueue();
cout << endl;
break;
}
case 7: // Exit
{
return 0;
}
}
}
return 0;
}
int showMenu()
{
cout << endl;
cout << "Stack menu:" << endl;
cout << "1) Push to stack" << endl;
cout << "2) Pop from stack" << endl;
cout << "3) Output entire stack" << endl;
cout << endl;
cout << "Queue menu:" << endl;
cout << "4) Insert into queue" << endl;
cout << "5) Read from queue" << endl;
cout << "6) Output entire queue" << endl;
cout << endl;
cout << "7) Exit" << endl;
cout << endl;
int option;
do {
option = getInt("Choose: ");
} while(option<1 || option>7);
cout << endl;
return option;
}
int getInt(char* prompt)
{
int output;
string input;
while(true) {
cout << prompt;
getline(cin, input);
stringstream ss(input);
if(ss >> output && !(ss >> input)) {
return output;
}
cin.clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment