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...
A palindrome is a string that reads the same forward and backward, i.e., the letters are...
A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.      Examples: radar à is a palindrome Able was I ere I saw Elba à is a palindrome good à not a palindrome Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character...
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...
Foundation of Computer Science A palindrome is a string that reads the same forward and backward....
Foundation of Computer Science A palindrome is a string that reads the same forward and backward. 1. Describe an algorithm that determines whether a string of n characters is a palindrome. 2. Write its corresponding program using your favorite programming language.
4.A palindrome is a nonempty string over some alphabet that reads the same forward and backward....
4.A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, civic, racecar, abba,… Give a DP algorithm to find the longest palindrome that is a subsequence of a given input string. What is the running time of your algorithm? (40 points). •Examples: •Given the input “character”, your algorithm should return “carac”. •Given the input “TTATGTGAT”, your algorithm should return “TATGTAT” Python Code only
(Palindrome number - A number is a palindrome if it reads the same from right to...
(Palindrome number - A number is a palindrome if it reads the same from right to left and from left to right, for example 676 is a palindrome number) Write a program that prompts the user to enter a three-digit integer number and determines whether it is a palindrome number or not In Java Please
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.
1. A finite sequence of letters is called a palindrome if it reads the same forward...
1. A finite sequence of letters is called a palindrome if it reads the same forward or backward. Devise an algorithm for determining whether or not a string of n letters is a palindrome. Write your algorithm using the the same sort of pseudocode used in the text. Your algorithm should start with procedure palindrome (a1,a2,...,an: lowercase letters) Your procedure should return the value 0 if the string is a palindrome, and the first integer i such that ai 6=an−i+1...
PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled...
PLEASE DO IN JAVA 4.15 Palindrome A palindrome is a string that is the same spelled forward as it is spelled backward. So, "abcba" is a palindrome, as are "eye" and "madam". This program takes as input from the user, a string and outputs whether the string is a palindrome. (1) Modify the given program to use a loop to output the string one character at a time. (1 pt) Example output for word = radar: Word Entered: radar r...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT