Write a routine which will accept a character string as input and determine if i
ID: 3700009 • Letter: W
Question
Write a routine which will accept a character string as input and determine if it is in the form of X l'C lIY where X is a string consisting of the letters 'A' and 'B' and Y is the reverse of X. (That is if X 'ABBAB' then Y BABBA'. At each point in the routine you may read only the next character of the string. The 'C' is the delimiter for 'X' and 'Y') IV. V. Given a doubly linked list write a routine which will travel through the list adding 10 to every node whose information field is an even number and then travel backwards thru the list subtracting 4 from every node whose information field contains an odd number.Explanation / Answer
/***** I have Provided the Solution for both the routines . For first routine i have written the test method also in the main function . While For routine2 i have provided the structure of doubly liked list structure and routine2 . Just add that function to your doubly liked list code and it will work provided you are following the same structure as i have provided Just go through the routines i have Provided the Correct solution*****/
#include<iostream>
#include<string.h>
using namespace std;
bool routine1(char *str){
int len = strlen(str);
// First of all String must be of odd length
if(len %2 !=1){
return false;
}
// Defining String X
char *X = new char[((len-1)/2) + 1];
int i=0;
while(str[i] != '/0' && str[i] != 'C' && i<len){
if(str[i] != 'A' && str[i] != 'B'){
return false;
}
X[i] = str[i];
i++;
}
// Either we have encounter C or end of str
if(i==len){
return false;
}
i++;
// Now we have to see that rest of String must be reverse of X and of equal Size
int xlen = strlen(X);
if(xlen != len-i){
return false;
}
while(str[i] != '/0' && i<len){
if(str[i] != 'A' && str[i] != 'B'){
return false;
}
if(str[i] != X[xlen-1]){
return false;
}
i++;
xlen--;
}
return true;
}
struct Node{
int data;
Node *next;
Node *prev;
};
void routine2(Node *root){
Node *current = root;
Node *tail;
// traverse throught he list and add 10 to every node with even data and keep track of tail also
while(current != NULL){
if(current->data %2==0){
current->data = current->data + 10;
}
current = current->next;
tail = current;
}
// Now tail points to the last Node
while(tail != NULL){
if(tail->data %2 == 1){
tail->data = tail->data -4;
}
tail = tail->prev;
}
}
int main(){
cout<<"result: " <<routine1("ABBABCBABBA");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.