standard entered on standard input and is not q input string
Solution
Please find all the code below
-------------------------------------------------------------------------------------------------------------------------------------------------------------
stack.hpp
/*
* stack.hpp
*
* Created on: 16-Feb-2017
* Author: yourname
*/
#include<iostream>
#ifndef SRC_STACK_HPP_
#define SRC_STACK_HPP_
class stack {
public:
void push(char data); //Function that inserts elements into the stack
bool isEmpty(); //Function to test whether the stack is empty
char top(); //Returns top element of stack
void pop(); //Removes element at the top of the stack
int size(); //Returns size of stack
stack();
struct node {
char node_data;
node *next;
node() {
next = NULL;
node_data = \'\ \';
}
node(char data, node *link) {
node_data = data;
next = link;
}
};
private:
node *head;
int sz;
};
#endif /* SRC_STACK_HPP_ */
-------------------------------------------------------------------------------------------------------------------------------------------------------------
stack.cpp
/*
* stack.cpp
*
* Created on: 16-Feb-2017
* Author: yourname
*/
#include \"stack.hpp\"
stack::stack() {
head = NULL;
sz = 0;
}
void stack::push(char data) {
head = new node(data, head);
++sz;
}
void stack::pop() {
if (head == NULL) {
throw \"Stack is empty\";
}
head = head->next;
--sz;
}
int stack::size() {
return sz;
}
bool stack::isEmpty() {
return sz == 0;
}
char stack::top() {
if (head == NULL) {
throw \"Stack is empty\";
}
return head->node_data;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
queue.hpp
/*
* queue.hpp
*
* Created on: 16-Feb-2017
* Author: yourname
*/
#ifndef SRC_QUEUE_HPP_
#define SRC_QUEUE_HPP_
#include<iostream>
class queue {
public:
void enque(char data);
bool isEmpty();
char front();
void deque();
int size();
queue();
struct node {
char node_data;
node *next;
node() {
next = NULL;
node_data = \'\ \';
}
node(char data, node *link) {
node_data = data;
next = link;
}
};
private:
node *head;
node *tail;
int sz;
};
#endif /* SRC_QUEUE_HPP_ */
-------------------------------------------------------------------------------------------------------------------------------------------------------------
queue.cpp
/*
* queue.cpp
*
* Created on: 16-Feb-2017
* Author: yourname
*/
#include \"queue.hpp\"
queue::queue() {
head = NULL;
tail = NULL;
sz = 0;
}
int queue::size() {
return sz;
}
bool queue::isEmpty() {
return sz == 0;
}
char queue::front() {
if (head == NULL)
throw \"queue is empty\";
return head->node_data;
}
void queue::enque(char data) {
if (head == NULL) {
tail = new node(data, tail);
head = tail;
} else {
node *t = new node(data, NULL);
tail->next = t;
tail = t;
}
++sz;
}
void queue::deque() {
if (head == NULL)
throw \"queue is empty\";
head = head->next;
if (!head)
tail = NULL;
--sz;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
palindrome.cpp
/*
* palindrome.cpp
*
* Created on: 15-Feb-2017
* Author: yourname
*/
#include<iostream>
#include \"stack.hpp\"
#include \"queue.hpp\"
using namespace std;
void add(string &str, int idx, stack &s, queue &q) {
if (idx == str.length()) {
return;
}
s.push(str[idx]);
q.enque(str[idx]);
add(str, idx + 1, s, q);
}
void remove(stack &s, queue &q, int idx) {
if (s.isEmpty() && q.isEmpty()) {
std::cout << \"-1\" << std::endl;
return;
} else if (s.top() != q.front()) {
std::cout << idx << std::endl;
while (s.isEmpty()) {
s.pop();
}
while (q.isEmpty()) {
q.deque();
}
} else {
s.pop();
q.deque();
remove(s, q, idx + 1);
}
}
int main(int argc, const char * argv[]) {
string str;
while (cin >> str) {
if (str == \"q\") {
break;
}
stack s;
queue q;
add(str, 0, s, q);
remove(s, q, 0);
}
}






