Created
August 5, 2016 17:17
-
-
Save tareq-si-salem/d2204a7910208ab20682e27e39285d5a to your computer and use it in GitHub Desktop.
Test the Queue and Stack data structures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "datastructure.h" | |
// include our custom header file | |
int main() { | |
char option; | |
bool loop = true; | |
Queue queue; | |
StackArray stack; | |
cout << "1. Add new Person" << endl; | |
cout << "2. Delete a Person" << endl; | |
cout << "3. Print the Queue and Stack content in increasing order" << endl; | |
cout << "4. print the Queue and Stack content in decreasing order" << endl; | |
cout << "q. Exit" << endl; | |
while (loop) { | |
cout << "Option: "; | |
cin >> option; | |
// choose which option (1,2,3,4 or q). | |
cin.ignore(); | |
// ignore to skip to new line | |
switch (option) { | |
case '1': | |
Person person; | |
cout << "Person's first name: "; | |
person.firstName = new char[10]; | |
cin.getline(person.firstName, 20); | |
cout << "Person's last name: "; | |
person.lastName = new char[10]; | |
cin.getline(person.lastName, 20); | |
cout << "Person's age: "; | |
cin >> person.age; | |
// read Person details (first name, last name, age) | |
queue.enqueue(person); | |
stack.push(person); | |
// push this person to stack and enqueue it to the queue. | |
break; | |
case '2': | |
cout << "The popped person: "; | |
if (!stack.isEmpty()) | |
printPerson(stack.pop()); | |
else { | |
cout << "stack is empty" << endl; | |
} | |
// display the popped value from the stack | |
cout << "The dequeued person: "; | |
printPerson(queue.dequeue()); | |
// display the dequeued value from the queue | |
break; | |
case '3': | |
cout << "Stack content: " << endl; | |
stack.traverse_forward(); | |
// display the stack content in forward order | |
cout << "Queue content: " << endl; | |
queue.traverse_forward(); | |
// display the queue content in forward order | |
break; | |
case '4': | |
cout << "Stack content: " << endl; | |
stack.traverse_backward(); | |
// display the stack content in reverse order | |
cout << "Queue content: " << endl; | |
queue.traverse_backward(); | |
// display the queue content in reverse order | |
break; | |
case 'q': | |
loop = false; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment