Lab V - COSC 215
Lab V - COSC 215
Lab Session V
Stacks and Queues
Use the template class Stack given in the lectures and posted on Moodle to define a function
isPalindrome that takes as parameter a string and checks whether it’s a palindrome.
Develop a main function to test your function as showed in the following Input/Output sample.
Recall that, to read a string s with blanks, you need to use the function getline(cin, s).
To ignore the case of a letter (uppercase or lowercase) you can convert all uppercase letters to
lowercase letters by adding to them the value 32. For example, ‘A’ + 32 gives ‘a’.
Sample input/output
madam is a palindrome
Racecar is a palindrome
Thank you!
2
Exercise 2: Priority Queue
An airline company has three types of passengers: VIP Class, First Class and Economy Class. In order to
organize the boarding of its passengers, the company assigns a priority for each type. VIP: 0, Business: 1,
Economy: 2 (In computer systems, lowest value is considered as the highest priority). Passengers with
the highest priority are served first. In the case where there are multiple passengers with the same
priority, they will be served by a FIFO policy. To do this, the company decides to use three different
queues. One queue per priority. When a passenger arrives to the airport, he must wait in the
corresponding queue according to his priority.
Each passenger is characterized by an ID and a priority. We give the definition of the class Passenger:
class Passenger
{
public:
int ID, priority;
void print()
{
cout<<"(ID: "<<ID<<", Priority: "<<priority<<") ";
}
};
To create the queues of passengers, you can use the generic class Queue given in the lectures. You must
just adjust the method traversal in order to print the information of the passenger. We give the
updated version of the method traversal:
void traversal()
{
Node<T> * curNode;
if(isEmpty())
cout<<"Empty Queue";
else
{
curNode = qhead;
while(curNode != NULL)
{
curNode->data.print();
curNode = curNode->next;
}
}
cout<<endl;
}
Develop a main function that declares an array of three queues of passengers and to simulate the
managing of the structure by adding a new passenger, serving a passenger or displaying the structure.
The information of a passenger (ID and priority) will be generated randomly using the function
rand() that generates a random positive integer value. To ensure that the value of th priority is
between 0 and 2, you can use rand() % 3. To get different scenarios at each execution call the
function srand(time(NULL)) at the beginning of your main function. Include the library <ctime> in
order to use srand.
3
Sample input/output:
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1
4
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2
5
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3
6
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 5
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 0
Thank you!