Posts

Length of LL (recursive)

  Length of LL (recursive) Send Feedback Given a linked list, find and return the length of the given linked list recursively. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First and the only line of each test case or query contains elements of the singly linked list separated by a single space.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. Output format : For each test case, print the length of the linked list. Output for every test case will be printed in a separate line.  Constraints : 1 <= t <= 20 0 <= N <= 10^4 Time Limit: 1 sec Sample Input 1: 1 3 4 5 2 6 1 9 -1 Sample Output 1: 7 /**************************************************************** Following is the class structure of the Node class: class Node { public: ...

Delete node

Image
  Delete node Send Feedback You have been given a linked list of integers. Your task is to write a function that deletes a node from a given position, 'POS'. Note : Assume that the Indexing for the linked list always starts from 0. If the position is greater than or equal to the length of the linked list, you should return the same linked list without any change. Illustration : The following images depict how the deletion has been performed. Image-I : Image-II : Input format : The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the linked list separated by a single space. The second line of each test case contains the integer value of 'POS'. It denotes the position in the linked list from where the node has to be deleted.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the si...

Print ith node

  Print ith node Send Feedback For a given a singly linked list of integers and a position 'i', print the node data at the 'i-th' position.  Note : Assume that the Indexing for the singly linked list always starts from 0. If the given position 'i', is greater than the length of the given singly linked list, then don't print anything. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line contains the value of 'i'. It denotes the position in the given singly linked list.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. Output format : For each test case, print the node data at the 'i-th' position of the link...

Length of LL

  Length of LL Send Feedback For a given singly linked list of integers, find and return its length. Do it using an iterative method. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First and the only line of each test case or query contains elements of the singly linked list separated by a single space.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. Output format : For each test case, print the length of the linked list. Output for every test case will be printed in a separate line.  Constraints : 1 <= t <= 10^2 0 <= N <= 10^5 Time Limit: 1 sec Sample Input 1 : 1 3 4 5 2 6 1 9 -1 Sample Output 1 : 7 Sample Input 2 : 2 10 76 39 -3 2 9 -23 9 -1 -1 Sample Output 2 : 8 0 /**************************************************************** Following is the cl...

Code : Polynomial Class

  Code : Polynomial Class Send Feedback Implement a polynomial class, with the following properties and functions. Properties : 1. An integer array (lets say A) which holds the coefficient and degrees. Use array indices as degree and A[i] as coefficient of ith degree. 2. An integer holding total size of array A. Functions : 1. Default constructor 2. Copy constructor 3. setCoefficient - This function sets coefficient for a particular degree value. If the given degree is greater than the current capacity of polynomial, increase the capacity accordingly and add then set the required coefficient. If the degree is within limits, then previous coefficient value is replaced by given coefficient value 4. Overload "+" operator (P3 = P1 + P2) : Adds two polynomials and returns a new polynomial which has result. 5. Overload "-" operator (P3 = p1 - p2) : Subtracts two polynomials and returns a new polynomial which has result 6. Overload * operator (P3 = P1 * P2) : Multiplies tw...

Complex Number Class

  Complex Number Class.... Doubt Send Feedback A ComplexNumber class contains two data members : one is the real part (R) and the other is imaginary (I) (both integers). Implement the Complex numbers class that contains following functions - 1. constructor You need to create the appropriate constructor. 2. plus - This function adds two given complex numbers and updates the first complex number. e.g. if C1 = 4 + i5 and C2 = 3 +i1 C1.plus(C2) results in: C1 = 7 + i6 and C2 = 3 + i1 3. multiply - This function multiplies two given complex numbers and updates the first complex number. e.g. if C1 = 4 + i5 and C2 = 1 + i2 C1.multiply(C2) results in: C1 = -6 + i13 and C2 = 1 + i2 4. print - This function prints the given complex number in the following format : a + ib Note : There is space before and after '+' (plus sign) and no space between 'i' (iota symbol) and b. Input Format : Line 1 : Two integers - real and imaginary part of 1st complex number Line 2 : Two integers - ...