Question

C++ Program: Check if queue is rotation. [32] Write a function bool isRotation(Queue *p, Queue *q)...

C++ Program:

Check if queue is rotation.

[32] Write a function bool isRotation(Queue *p, Queue *q) to tell if p is a rotation of q. The integrity of the queues should be maintained on return of the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:-

# include <stdio.h>

# include <string.h>
# include <stdlib.h>

/* Function checks if passed strings (str1 and str2)
are rotations of each other */
int areRotations(char *str1, char *str2)
{
int size1 = strlen(str1);
int size2 = strlen(str2);
char *temp;
void *ptr;

/* Check if sizes of two strings are same */
if (size1 != size2)
   return 0;

/* Create a temp string with value str1.str1 */
temp = (char *)malloc(sizeof(char)*(size1*2 + 1));
temp[0] = '\0';
strcat(temp, str1);
strcat(temp, str1);

/* Now check if str2 is a substring of temp */
ptr = strstr(temp, str2);

free(temp); // Free dynamically allocated memory

/* strstr returns NULL if the second string is NOT a
   substring of first string */
if (ptr != NULL)
   return 1;
else
   return 0;
}

/* Driver program to test areRotations */
int main()
{
   char *str1 = "AACD";
   char *str2 = "ACDA";

   if (areRotations(str1, str2))
   printf("Strings are rotations of each other");
   else
   printf("Strings are not rotations of each other");

   getchar();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Program: Check if queue is rotation. [32] Write a function bool isRotation(Queue *p, Queue *q)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • A priority queue is a collection of items each having a priority. A priority queue supports three...

    A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...

  • Write a program in C++ to simulate a dispatcher using a priority queue system. A GUI...

    Write a program in C++ to simulate a dispatcher using a priority queue system. A GUI has to be used to enter new processes with priority included (numbering should be automatic). GUI command should be used to terminate processes. Context switches are to be by command with the cause of the switch being immaterial. Assume only one CPU. Priorities and numbers of processes can be kept small, big enough to demonstrate the listed functions below. You can pre-populate the queues...

  • Write only function. In c++ 3. a) Write a function, to be included in a queue...

    Write only function. In c++ 3. a) Write a function, to be included in a queue class - circular array implementation (20 p that will print all the items in the queue from front to rear

  • C++ ((USE STL verison please)) Implement the queue class as shown above. Use your queue to...

    C++ ((USE STL verison please)) Implement the queue class as shown above. Use your queue to store the names of 5 students waiting outside Student Services to meet with advisors. You may want to add more data to each node including student ID, Major, etc. Add a member function displayQueue which displays the contents of the queue. Write the main function to test the queue. CLASS: #ifdef queue_h #define queue_h namespace queues { struct queueNode{ char data; queueNode *link; };...

  • typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q);...

    typedef struct queue * Queue; Queue queueCreate(int maxSize); void enqueue(Queue q, int item); int queueFront(Queue q); int dequeue(Queue q); int queueSize(Queue q); void queueDestroy(Queue q)Exercise 1: Writing Assert Based Black Box Unit Tests for a Queue For this exercise you will need a copy of Queue.h, the queue interface for this exercise. You can download it or use the following command to copy it into your current directory cp «dp1091/public_html/2012/tlb/11/Queue.h. And a copy of the testQueue.c, the testing code we...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • In C++ if needed, (Weight: 2096) Programming: Write a Compare function class that inserts Person objects in a priority queue based on the number of dependents a person has. The Person object with the...

    In C++ if needed, (Weight: 2096) Programming: Write a Compare function class that inserts Person objects in a priority queue based on the number of dependents a person has. The Person object with the largest number of dependents should be removed first. 3. Hint: In order to use the priority queue class, we need to pass in a structure that allows comparing the objects. If the max queue keeps track of primitive values (e.g. integers, strings), then the built-in C++...

  • Q.1. Write a C program that determines whether a line entered by a user is a...

    Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out). Q.2. Write a charQueue header file containing all the function headers of following functions: 1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the...

  • Can you please write the two C++ modules below is a step by step instuctions to...

    Can you please write the two C++ modules below is a step by step instuctions to follow that will make it very easy thanks. Create a module called pqueue.cpp that implements priority queues, with a header file calledpqueue.hthat describes what pqueue.cpp exports. The interface includes the following, and nothing else. Types ItemType and PriorityType are discussed below under the refinement plan. A type, PriorityQueue. Creating a PriorityQueue object with line PriorityQueue q; makes q be an initially empty priority queue....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT