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

write a general example of polling in C language with comments
write a general example of polling in C language with comments
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.
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Write a general example of interrupts in C language with comments. Thank you
Write a general example of interrupts in C language with comments. Thank you
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).
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.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT