Question

In: Computer Science

Write a function in any functional programming language that will reverse a general list. For example,...

Write a function in any functional programming language that will reverse a general list. For example, if an input is (A (B C (D E)) F), output is (F ((E D) C B) A).  Please note that any built-in/pre-defined function, e.g., reverse, cannot be used in your answer.

Please DO NOT hard-code any input values, output values in your code.

Please submit a screenshot of where your code got compiled, executed, showing the execution result

Solutions

Expert Solution

Here we can use stack to reverse the list. I have written very easy logic which could be easily understandable.

#include<bits/stdc++.h>
using namespace std;
int main()
{
  stack<char> stk;
  //taking the complete input first in a string
  string s;
  cout << "Enter the list to be reversed "; //input message
  getline(cin,s);
  for(int i = 0; i<s.length(); i++){
    stk.push(s[i]);  //then pushing each character of the string into stack
  }
  cout << "Output is "; //output message
   //then printing every character one by one
  for(int i = 0; i<s.length(); i++){
    char k = stk.top();
    stk.pop();
    if(k== ')'){ // we have to seperately deal with with the brackets.
      cout << "(";
    }
    else if(k == '('){
      cout << ")";
    }
    else{
      cout << k;
    }
  }
  
}

output will look like below picture. (i have customised the output and input message and it can be removed if required)


Related Solutions

C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Code in C++ programming language description about read and write data to memory example.
Code in C++ programming language description about read and write data to memory example.
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT