Lab 5: Queue, Stack, Operator Overloading
Lab 5: Queue, Stack, Operator Overloading
1. Create a class called IntQueue, implement integer queue using an array and
linked list. Implement the following functions:
create: Create a new, empty queue object.
empty: Determine whether the queue is empty; return true if it is
and false if it is not.
enqueue: Add a new element at the rear of a queue.
dequeue: Remove an element from the front of the queue and return it.
(This operation cannot be performed if the queue is empty.)
front: Return the element at the front of the queue (without removing it
from the queue). (Again, this operation cannot be performed if the queue
is empty.)
Example:
Input: Enter your choice
Press 1. To create a queue
Press 2. To check whether queue is empty or not
Press 3. Add new element to queue
Press 4. Remove element from queue
Press 5. Show the element at front
Press 6. To exit
1
Queue is created
Enter your choice again
3
Enter new element
4
.…
2. In the last lab, we implemented the integer stack. Now we have integer
stack and integer queue. Our task is to compute an alternating series using
the numbers that you entered. An alternating series switches the sign of the
number being added for every other number. For example, if we enter the
number 1, 3, 15, 9 then we would compute the alternating series as 1 - 3 +
15 - 9 = 4. Modify the intQueue code so that it computes the sum of an
alternating series of the numbers as they are removed from the queue (so
the first number is positive, the second is negative, etc.). Display the
alternating series to the screen on a single line. So if the numbers 1, 3, 15,
and 9 were entered by the user, the code should display:
1 - 3 + 15 - 9 = 4.
Next modify the integer stack code so that it computes the sum of an
alternating series of the numbers as they are removed from the stack (so
the first number off the stack is positive, the second is negative, etc.).
Display the alternating series to the screen on a single line as above. How
is the result off the stack different from the result off the queue. In the
comments section at the top of your code, explain how this is different than
the result you get when using a queue. In addition, there are some cases
where the result of using either a stack or a queue will be exactly the same.
Indicate in your comment under what conditions the result will be the same.
Finally, how could you modify your stack code so that it always gives the
same result as your queue code? Indicate that in your comments as well.
3. Your next task is to take what we've done with integer queues and stacks
and apply it to a new data type. You should make two new classes -
stringQueue and stringStack. Using integerQueue and integerStack as a
reference, write up two programs that will take a sequence of Strings from
the console and place them into either a queue or a stack as appropriate
until the user provides a line with ** as the first two characters. Then your
code should process the queue (or stack) in the appropriate order,
concatenating all of the strings together into a single String and then, when
the queue is empty, display that string to the console.
Again, consider the differences in the behaviours of the two data structures.
When might we want to use a stack for String processing instead of a
queue? Put a brief description of the differences between the two pieces of
code into the comments of your stringStack code and give an example of
when you might need to use a stack.
4. We are given a string and we have to perform some steps. In each step we
need to take the first character of the string and put it at the end of the
string. We have to find out what will be the string after N steps. Consider
the string as a queue. At each step dequeue the character from the front and
enqueue it at the end and repeat the process for N times except the final
character.
Example:
Input String: DAIICT
Output: TDAIIC
Example
Q1: 2,5,6,3,8
Q2: 1,3,7,4,3
Q3: 3,8,13,7,11
6. In your integer queue class implement sortqueue function which sorts the
element in ascending order. Implement another function called as
evenOddMerge, merge 2 pre sorted queues into a new larger sorted queue
such that all the evens are in order then all of the odds are in order. You
may assume that the function sortqueue is always called before
evenOddMerge.
In the main method of your driver program, create two queues with several
items each, print the queues, call the sortqueue method then call the merge
method. Then print the original and merged queues.
Example
Sorted q1 contains 1, 2, 4, 6 and sorted q2 contains 0, 1, 2, 3, 5
Call the merge method:
q3 = evenOddMerge(q1, q2);
After the call to merge, q3 contains 0, 2, 2, 4, 6, 1, 1, 3, 5
class RationalNumber {
public:
RationalNumber( int = 0, int = 1 ); // default constructor
RationalNumber operator+( const RationalNumber& );
RationalNumber operator-( const RationalNumber& );
RationalNumber operator*( const RationalNumber& );
RationalNumber operator/( RationalNumber& );
bool operator>( const RationalNumber& );
bool operator<( const RationalNumber& );
bool operator>=( const RationalNumber& );
bool operator<=( const RationalNumber& );
bool operator==( const RationalNumber& );
bool operator!=( const RationalNumber& );
void printRational( void );
private:
int numerator;
int denominator;
};
Example:
7/3 + 1/3 = 8/3
7/3 - 1/3 = 2
7/3 * 1/3 = 7/9
7/3 / 1/3 = 7
7/3 is:
> 1/3 according to the overloaded > operator
>= 1/3 according to the overloaded < operator
>= 1/3 according to the overloaded >= operator
> 1/3 according to the overloaded <= operator
!= 1/3 according to the overloaded == operator
!= 1/3 according to the overloaded != operator