The Stack and Queue programming project is one of the first projects I have ever written using the C++ programming language. This was a given assignment in graduate school and I had a very limited time to complete it. This program only uses the command line to interact and does not have a graphical user interface.

This program is focused around a main.cpp file like most other C++ programs. I have split out both the stack and the queue into their own include file. I am using iostream to drive the information into the command line for the user to interact with. The program prompt the user for input and then manipulates the stack and the queue. The user can add and remove items from the queue as well as look at the numbers included in the queue.

The goal of this project is a simple one, but it is a very important one to understand as it is used in memory management for programming. Learning how to manipulate a stack and queue is a process that will make you a better programmer. Even if you use a dynamic programming language that takes care of memory management, it still helps to understand the basics of stack and queue when working with databases.

Every computer task can be boiled down to a few simple data structure concepts. These concepts allow for building fast and stable programs for computers. If your program is not stable, users will not want to have it installed on their computer. Learn the basic concepts of programming before moving on to bigger projects.

/*
*
* Stack and Queue created by Richie Leahey.
*
* This program lets the user create either a stack or a queue and then interact with it.
*
*/

#include "stdafx.h"
#include <iostream>
#include "stack.h"
#include "queue.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[]){

	int choice_main; // Sets the main menu for the user.  They can either choose to create a queue or a stack.
	// Other classes could be created and added here as well.
	stack run_s;
	queue run_q;

	choice_main = ' ';
	while (choice_main != 1 && choice_main != 2 && choice_main != 3)	{
		cout << "Welcome to Stack and Queue!!!" << endl << endl;
		cout << "Please choose a number: " << endl;
		cout << "1 - Create a Stack " << endl;
		cout << "2 - Create a Queue " << endl;
		cout << "3 - Exit the Program " << endl << endl;
		cin >> choice_main;
		cout << endl;
	}

	if (choice_main == 1)	{
		run_s.run_stack();
	}
	if (choice_main == 2){
		run_q.run_queue();
	}
	else if (choice_main == 3){
		return 0;
	}
}
main.cpp
/*
*
* Stack and Queue created by Richie Leahey.
*
* This program lets the user create either a stack or a queue and then interact with it.
*
*/

#pragma once

#include <iostream>

using namespace std;

struct queue_node{
	int data;
	struct queue_node *next;
};

class queue{
	struct queue_node *first, *rear;
public:
	queue(){
		first = rear = NULL;
	}
	void insert();
	void del();
	void check();
	void deleteall();
	void show();
	void run_queue();
};
queue.h
/*
*
* Stack and Queue created by Richie Leahey.
*
* This program lets the user create either a stack or a queue and then interact with it.
*
*/

#include "stdafx.h"
#include "queue.h"

using namespace std;

void queue::insert(){  // Inserts a new number into the queue.
	int value;
	struct queue_node *ptr;
	cout << "Enter a number to insert: " << endl;
	cin >> value;
	cout << endl;
	ptr = new queue_node;
	ptr->data = value;
	ptr->next = NULL;
	if (first == NULL)
		first = ptr;
	else
		rear->next = ptr;
	rear = ptr;
	cout << "A new number is inserted into the Queue. " << endl << endl;
	getchar();
}

void queue::del(){  // Deletes the first number of the queue.
	if (first == NULL){
		cout << "The Queue is Empty. " << endl << endl;
		getchar();
		return;
	}
	struct queue_node *temp;
	temp = first;
	first = first->next;
	cout << "The deleted number is " << temp->data << ". " << endl << endl;
	delete temp;
	getchar();
}

void queue::check(){ // Checks the queue to see if it is empty.
	struct queue_node *ptr1 = first;
	if (first == NULL){
		cout << "The Queue is Empty. " << endl << endl;
		getchar();
		return;
	}
	cout << "The Queue is Not Empty. " << endl;
	while (ptr1 != NULL)
	{
		ptr1 = ptr1->next;
	}
	cout << endl;
	getchar();
}

void queue::deleteall(){ // Deletes all of the numbers in the queue.
	while (first != NULL){
		struct queue_node *temp;
		temp = first;
		first = first->next;
		cout << "The deleted number is " << temp->data << ". " << endl;
		delete temp;
	}
	getchar();
	cout << "The Queue is Empty. " << endl << endl;
	return;
}

void queue::show(){ // Shows all of the numbers in the queue.
	struct queue_node *ptr1 = first;
	if (first == NULL){
		cout << "The Queue is Empty. " << endl << endl;
		getchar();
		return;
	}
	cout << "The Queue is: " << endl;
	while (ptr1 != NULL){
		cout << ptr1->data;
		ptr1 = ptr1->next;
		cout << endl;
	}
	cout << endl;
	getchar();
}

void queue::run_queue()
{
	queue q;
	int choice_queue;
	while (1){
		cout << "Please choose a number: " << endl;  // Displays options for the user for each switch case.
		cout << "1 - Insert a Number in Queue " << endl;
		cout << "2 - Delete a Number from Queue " << endl;
		cout << "3 - Check if the Queue is Empty " << endl;
		cout << "4 - Delete the Entire Queue " << endl;
		cout << "5 - Display Queue " << endl;
		cout << "6 - Exit the Program " << endl;

		cin >> choice_queue;
		cout << endl;

		switch (choice_queue){  // The switch case to run functions based on user input.
		case 1:
			q.insert();
			break;
		case 2:
			q.del();
			break;
		case 3:
			q.check();
			break;
		case 4:
			q.deleteall();
			break;
		case 5:
			q.show();
			break;
		case 6:
			exit(0);
			break;
		default:
			cout << "Please choose a number between 1-6. " << endl << endl;
			getchar();
			break;
		}
	}

}
queue.cpp
/*
*
* Stack and Queue created by Richie Leahey.
*
* This program lets the user create either a stack or a queue and then interact with it.
*
*/

#pragma once

#include <iostream>

using namespace std;

class stack_node{
	int data;
	stack_node *next;
public:
	stack_node(){
		data = 0;
		next = NULL;
	}
	void setData(int k){ data = k; }
	int getData(){ return data; }
	void setNext(stack_node *n){ next = n; }
	stack_node* getNext(){ return next; }
};

class linkedlist{
	stack_node *head;
public:
	linkedlist(){ head = NULL; }
	int addToStack(stack_node *ptr);
	int deleteFromStack();
	int deleteall();
	void display();
	void check();
};

class stack{
	linkedlist ll;
public:
	stack(){}
	int push();
	int pop();
	void check();
	int deleteall();
	void display();
	void run_stack();
};
stack.h
/*
*
* Stack and Queue created by Richie Leahey.
*
* This program lets the user create either a stack or a queue and then interact with it.
*
*/

#include "stdafx.h"

#include "stack.h"

using namespace std;

int linkedlist::addToStack(stack_node *ptr){
	int data;
	cout << "Enter a number to push onto the stack: ";
	cin >> data;
	ptr->setData(data);
	ptr->setNext(head);
	head = ptr;
	cout << endl;
	return 1;
}

int linkedlist::deleteFromStack(){
	if (head != NULL){
		cout << head->getData();
		head = head->getNext();
		return 1;
	}
	else
		return 0;
}

void linkedlist::check(){
	stack_node *ptr = head;
	if (ptr != NULL){
		cout << "The Stack is Not Empty. " << endl << endl;
	}
	else{
		cout << "The Stack is Empty. " << endl << endl;
	}
}

int linkedlist::deleteall(){
	if (head == NULL){
		cout << "The Stack is Empty. ";
	}
	else{
		while (head != NULL){
			head = head->getNext();
		}
		cout << "The Stack is Now Empty. ";
	}
	return 0;
}

void linkedlist::display(){
	stack_node *ptr = head;
	if (ptr == NULL){
		cout << "The Stack is Empty. " << endl;
	}
	else{
		while (ptr != NULL){
			cout << " " << ptr->getData() << "\n";
			ptr = ptr->getNext();
		}
	}
}

int stack::push(){
	int i = 0;
	stack_node *n1 = new stack_node();
	i = ll.addToStack(n1);
	return i;
}

int stack::pop(){
	int i = 0;
	i = ll.deleteFromStack();
	if (i == 0)
		cout << "There are no numbers on the stack. " << endl;
	return i;
}

void stack::check(){
	ll.check();
}

int stack::deleteall(){
	int i = 0;
	i = ll.deleteall();
	if (i == 0)
		cout << endl << endl;
	return i;
}

void stack::display(){
	ll.display();
}

void stack::run_stack(){

	stack s;
	int choice_stack;

	do{
		cout << "Please choose a number: " << endl;
		cout << "1 - Push a Number on Stack " << endl;
		cout << "2 - Pop a Number from Stack " << endl;
		cout << "3 - Check if Stack is Empty " << endl;
		cout << "4 - Delete all Numbers from Stack " << endl;
		cout << "5 - Display Stack " << endl;
		cout << "6 - Exit the Program " << endl;

		cin >> choice_stack;
		cout << endl;

		if (choice_stack == 1){
			int i = s.push();
			if (i == 1){
				cout << "Numbers on stack: " << endl;
				s.display();
				cout << endl;
			}
			else{
				cout << "Error: Number not pushed onto stack. ";
			}
			getchar();
		}

		if (choice_stack == 2){
			int i;
			i = s.pop();
			if (i == 0)
				cout << "Error: Number not popped off stack. " << endl << endl;
			else{
				cout << " is popped off of the stack. " << endl << endl;
			}
			getchar();
		}

		if (choice_stack == 3){
			s.check();
			getchar();
		}

		if (choice_stack == 4){
			s.deleteall();
			getchar();
		}

		if (choice_stack == 5){
			s.display();
			cout << endl;
			getchar();
		}

	} while (choice_stack != 6);
}
stack.cpp