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

haskell : write a function that reverse the first three element of a list, but not...
haskell : write a function that reverse the first three element of a list, but not the rest. example [1,2,3,4,5,6] == [3,2,1,4,5,6]
Using a programming language of your choice, write a complete and fully functional program that uses...
Using a programming language of your choice, write a complete and fully functional program that uses reference and pointer types to swap two double precision floating-point numbers. The two numbers are read in by the program’s user. Use a proper prompt for each number. Use one function that uses formal parameter reference types to swap the two numbers Use another function that uses formal parameter pointer types to swap the two numbers. In the main or driver function, call these...
write a general example of polling in C language with comments
write a general example of polling in C language with comments
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
(Programming Language: Python) Complete the function remove number such that given a list of integers and...
(Programming Language: Python) Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. # DO NOT ADD ANY OTHER IMPORTS from typing import List def remove_number(lst: List[int], number: int) -> None: """ Remove every instance of number in lst. Do this *in-place*, i.e. *modify* the list. Do NOT return a...
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...
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.
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.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT