0% found this document useful (0 votes)
2 views

Lab V - COSC 215

The document outlines two programming exercises for a lab session on stacks and queues. The first exercise involves creating a function to check if a string is a palindrome using a stack, while the second exercise requires managing passenger boarding for an airline using priority queues based on passenger class. Sample input and output scenarios are provided for both exercises to illustrate expected functionality.

Uploaded by

mmezher50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab V - COSC 215

The document outlines two programming exercises for a lab session on stacks and queues. The first exercise involves creating a function to check if a string is a palindrome using a stack, while the second exercise requires managing passenger boarding for an airline using priority queues based on passenger class. Sample input and output scenarios are provided for both exercises to illustrate expected functionality.

Uploaded by

mmezher50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Affordable Excellence in Education

COSC 215 – Advanced Programming and Data Structures

Lab Session V
Stacks and Queues

Dr. Ali Rammal (Assistant Professor – CIS Department)


Exercise 1: Palindrome
A palindrome is a word, number, phrase, or other sequence of symbols that reads the same
backwards as forwards, such as the words madam or Racecar, the date and time 12/21/33
12:21, and the sentence: "A man, a plan, a canal – Panama"

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

Enter a string: madam

madam is a palindrome

Enter a string: Racecar

Racecar is a palindrome

Enter a string: 12/21/33 12:21

12/21/33 12:21 is a palindrome

Enter a string: A man, a plan, a canal - Panama

A man, a plan, a canal - Panama is a palindrome

Enter a string: palindrome

palindrome isn't a palindrome

Enter a string: stop

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

Queue 0: Empty Queue


Queue 1: Empty Queue
Queue 2: Empty Queue

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1

(ID: 5800, Priority: 2) is added to the structure

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1

(ID: 10362, Priority: 0) is added to the structure

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1

(ID: 15897, Priority: 1) is added to the structure

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1

(ID: 29213, Priority: 0) is added to the structure

4
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 1

(ID: 10712, Priority: 1) is added to the structure

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3

Queue 0: (ID: 10362, Priority: 0) (ID: 29213, Priority: 0)


Queue 1: (ID: 15897, Priority: 1) (ID: 10712, Priority: 1)
Queue 2: (ID: 5800, Priority: 2)

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2

(ID: 10362, Priority: 0) is served

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2

(ID: 29213, Priority: 0) is served

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2

(ID: 15897, Priority: 1) is served

5
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3

Queue 0: Empty Queue


Queue 1: (ID: 10712, Priority: 1)
Queue 2: (ID: 5800, Priority: 2)

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2

(ID: 10712, Priority: 1) is served

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2

(ID: 5800, Priority: 2) is served

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 2

No more passengers to be served

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 3

Queue 0: Empty Queue


Queue 1: Empty Queue
Queue 2: Empty Queue

6
Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 5

Invalid choice. Try again!

Press:
1: Add a passenger to the structure
2: Serve a passenger
3: Print the structure
0: Exit
Enter your choice: 0

Thank you!

You might also like