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...
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.
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...
(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.
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.
making a python code for this: A palindrome is a sequence that reads the same backwards...
making a python code for this: A palindrome is a sequence that reads the same backwards as forwards. Numbers can also be palindromes if we consider their digits as a sequence, for example 12121 and 8228 are palindromes. We can find palindromes from an initial seed number using the reverse and add method: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
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...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example,...
2. Palindromes A palindrome is a word that reads the same forwards and backwards. For example, \aibohphobia" (the irrational fear of palindromes) is a word that reads the same forwards and backwards. Write a function called isPalindrome() that accepts a string as a parameter and returns True if the string is a palindrome, False otherwise. Using the function you created, write a program Python home work Python
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT