Question

In: Computer Science

In C++: This is the problem: [(1)] A palindrome is a string that reads the same...

In C++:

This is the problem:

[(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate

[(2)] Let Q be a non-empty queue, and let S be an empty stack. Using only the stack and queue ADT functions and a single element variable X, write an algorithm to reverse the order of the elements in Q.

[(3)] Use singly linked lists to implement integers of unlimited size. Each node of the list should store one digit of the integer. You should implement addition, subtraction, multiplication, and exponentiation operations. Limit exponents to be positive integers. What is the asymptotic running time for each of your operations, expressed in terms of the number of digits for the two operands of each function?

[(4)] Implement a program that can input an expression in postfix notation and output its value.

I need number 3 completed.

This is the code:

#include <iostream>

#include <string.h>

#include <stack>

#include <queue>

#include <string>

#include <bits/stdc++.h>

using namespace std;

// Stack type

struct Stack

{

int top;

unsigned capacity;

int* array;

};

// Stack Operations

struct Stack* createStack( unsigned capacity )

{

struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));

if (!stack) return NULL;

stack->top = -1;

stack->capacity = capacity;

stack->array = (int*) malloc(stack->capacity * sizeof(int));

if (!stack->array) return NULL;

return stack;

}

int isEmpty(struct Stack* stack)

{

return stack->top == -1 ;

}

char peek(struct Stack* stack)

{

return stack->array[stack->top];

}

char pop(struct Stack* stack)

{

if (!isEmpty(stack))

return stack->array[stack->top--] ;

return '$';

}

void push(struct Stack* stack, char op)

{

stack->array[++stack->top] = op;

}

// The main function that returns value of a given postfix expression

int evaluatePostfix(char* exp)

{

// Create a stack of capacity equal to expression size

struct Stack* stack = createStack(strlen(exp));

int i;

// See if stack was created successfully

if (!stack) return -1;

// Scan all characters one by one

for (i = 0; exp[i]; ++i)

{

// If the scanned character is an operand (number here),

// push it to the stack.

if (isdigit(exp[i]))

push(stack, exp[i] - '0');

// If the scanned character is an operator, pop two

// elements from stack apply the operator

else

{

int val1 = pop(stack);

int val2 = pop(stack);

switch (exp[i])

{

case '+': push(stack, val2 + val1); break;

case '-': push(stack, val2 - val1); break;

case '*': push(stack, val2 * val1); break;

case '/': push(stack, val2/val1); break;

}

}

}

return pop(stack);

}

// Utility function to print the queue

void Print(queue<int>& Q)

{

while (!Q.empty()) {

cout << Q.front() << " ";

Q.pop();

}

}

// Function to reverse the queue

void reverseQueue(queue<int>& Q)

{

stack<int> S;

while (!Q.empty()) {

S.push(Q.front());

Q.pop();

}

while (!S.empty()) {

Q.push(S.top());

S.pop();

}

}

void convertTosmall(string& str)

{

int length = str.length();

for (int i = 0; i < length; i++) {

int c = str[i];

if (isupper(c))

str[i] = tolower(c);

}

return;

}

string removeSpaces(string& word) {

string newWord;

for (int i = 0; i < word.length(); i++) {

if (word[i] != ' ') {

newWord += word[i];

}

}

return newWord;

}

int main()

{

while( true )

{

std::string letters;

std::cout << "Please enter a string (Enter - exit): ";

std::getline( std::cin, letters );

convertTosmall(letters);

letters = removeSpaces(letters);

if ( letters.empty() ) break;

std::stack<char>

s( std::stack<char>::container_type( letters.begin(), letters.end() ) );

std::queue<char>

q( std::queue<char>::container_type( letters.begin(), letters.end() ) );

while ( !s.empty() && s.top() == q.front() )

{

s.pop();

q.pop();

}

if ( s.empty() ) std::cout << "The string is a palindrome" << std::endl;

else std::cout << "The string is not a palindrome" << std::endl;

}

queue<int> Q;

Q.push(10);

Q.push(20);

Q.push(30);

Q.push(40);

Q.push(50);

Q.push(60);

Q.push(70);

Q.push(80);

Q.push(90);

Q.push(100);

reverseQueue(Q);

Print(Q);

char exp[] = "231*+9-";

cout<<"\npostfix evaluation: "<< evaluatePostfix(exp);

cout<<"\n"<<endl;

return 0;

}

Solutions

Expert Solution

#include<iostream>
#include <sstream>
#include <string>
#include <cmath>

using namespace std;
class node{
public:
int data;
node* next;
};
node* head=NULL;
void insert(int val)
{
node* newnode=new node();
newnode->data=val;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
return;
}
node* temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}

long long Addition()
{
long long additionResult = 0;
node* temp=head;
while(temp->next!=NULL)
{
additionResult += temp->data;
temp=temp->next;
}
additionResult += temp->data;
return additionResult;
}

long long Subtraction()
{
  
node* temp=head;
long long subtractionResult = temp->data;
temp=temp->next;
while(temp->next!=NULL)
{
subtractionResult -= temp->data;
temp=temp->next;
}
subtractionResult -= temp->data;
return subtractionResult;
}
long long Multiplication()
{
long long multiplicationResult = 1;
node* temp=head;
while(temp->next!=NULL)
{
multiplicationResult *= temp->data;
temp=temp->next;
}
multiplicationResult *= temp->data;
return multiplicationResult;
}

long long Exponent()
{
  
node* temp=head;
long long exponentResult = temp->data;
temp=temp->next;
while(temp->next!=NULL)
{
cout<<endl<<exponentResult<<" "<<temp->data<<endl;
exponentResult += pow(exponentResult, temp->data);
cout<<exponentResult<<" ";
temp=temp->next;
}
exponentResult += pow(exponentResult, temp->data);
return exponentResult;
}

void display()
{
node* temp=head;
while(temp->next!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<"\n";
}
int main()
{
int inputInteger = 1234;
  
ostringstream str1; // for converting integer to string
str1 << inputInteger;
string inputString = str1.str();
  
for( int itr = 0; itr < inputString.length(); insert(inputString[itr]-48), itr++); // insert each digit of the input integer to list

int choice;
cout<<"Enter choice : \n 1: Addition\n 2:Subtraction\n 3: Multiplication\n 4: Exponent\n";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Addition of list is "<<Addition()<<endl;
break;
case 2:
cout<<"Subtraction of list is "<<Subtraction()<<endl;
break;
case 3:
cout<<"Multiplication of list is "<<Multiplication()<<endl;
break;
case 4:
cout<<"Exponent of list is "<<Exponent()<<endl;
break;
}
  
}

//Asymptotic running time of this code will be O(n)

// Kindly give me a thumbs up


Related Solutions

IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
C++: A palindrome is a string that is the same backward as it is forward. For...
C++: A palindrome is a string that is the same backward as it is forward. For example, “tot” and “otto” are rather short palindromes. Write a program that lets a user enter a string and that passes to a bool function a reference to the string. The function should return true if the string is a palindrome and false otherwise. When you do the judgment, capitalization, spaces, and punctuation should be neglected, that is, “Madam, I’m Adam” should test as...
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT