en
Feedback
inactive

inactive

Closed channel
3 849
Subscribers
No data24 hours
-297 days
-12730 days
Posts Archive
#include <iostream> using namespace std; struct Node { int data; struct Node* next; }; typedef struct Node Node; Node* createNode(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = NULL; return newNode; } void insertEnd(Node** head, int value) { Node* newNode = createNode(value); if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } void rotateRight(Node** head, int positions) { if (*head == NULL || positions <= 0) { return; } Node* current = *head; int count = 1; while (current->next != NULL) { current = current->next; count++; } positions %= count; if (positions == 0) { return; } current->next = *head; int stepsToRotate = count - positions; current = *head; for (int i = 1; i < stepsToRotate; i++) { current = current->next; } *head = current->next; current->next = NULL; } void displayList(Node* head) { Node* current = head; while (current != NULL) { cout << current->data << " "; current = current->next; } cout << endl; } int main() { int n, positions; cin >> n; Node* head = NULL; for (int i = 0; i < n; i++) { int value; cin >> value; insertEnd(&head, value); } cin >> positions; //awasthi cout << "Original List: "; displayList(head); rotateRight(&head, positions); cout << "Rotated List: "; displayList(head); while (head != NULL) { Node* temp = head; head = head->next; delete temp; } return 0; }

You are working on a program to manage the lineup of a sports team. As part of the lineup management functionality, you need to implement a program that rotates the positions of the team members by a given number of positions to the right. This will allow the coach to reorganize the lineup and make necessary adjustments based on player performance or strategic considerations. The lineup is represented as a grounded header linked list, where each node contains the player's jersey number. The first node serves as the header node and does not contain any actual player data. The subsequent nodes represent the players in the lineup. Note: This is a sample question asked in Capgemini recruitment. Input format : The first line of input consists of the number of elements n in the list. The second line of input consists of n elements, separated by space. The third line of input consists of the number of positions to rotate right. Output format : The first line of output prints the original list. The second line of output prints the rotated list.

#include <iostream> struct Node { char data; Node* next; }; Node* createNode(char value) { Node* newNode = new Node; newNode->data = value; newNode->next = nullptr; return newNode; } void insertAfterPosition(Node* head, int position, char value) { Node* newNode = createNode(value); Node* current = head; for (int i = 0; i < position; i++) { if (current->next == nullptr) { std::cout << "Invalid position." << std::endl; delete newNode; return; } current = current->next; } newNode->next = current->next; current->next = newNode; } void displayList(Node* head) { Node* current = head->next; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } void deleteList(Node* head) { Node* current = head; while (current != nullptr) { Node* temp = current; current = current->next; delete temp; } } int main() { Node* head = createNode('\0'); // Grounded header node int n; char value; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> value; insertAfterPosition(head, i, value); } int position; std::cin >> position; std::cin >> value; insertAfterPosition(head, position, value); std::cout << "Updated list: "; //awasthi displayList(head); deleteList(head); return 0; }

You are working on a text editing application, and you need to implement a feature that allows users to insert a character at a specific index in the text. You decide to implement this feature using a grounded header linked list to efficiently manage the text. Note: This is a sample question asked in a Capgemini interview. Input format : The first line of input consists of an integer n, representing the number of characters. The second line consists of n space-separated characters, representing the initial characters in the list. The third line consists of an integer index representing the position for insertion. The fourth line consists of a character to be inserted at the specified index.

#include <iostream> #include <string> using namespace std; struct Node { string data; Node* next; }; typedef struct Node Node; Node* createNode(const string& value) { Node* newNode = new Node; newNode->data = value; newNode->next = nullptr; return newNode; } void insertEnd(Node*& head, const string& value) { Node* newNode = createNode(value); if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } void deleteLastNode(Node*& head) { if (head == nullptr) { return; } else if (head->next == nullptr) { delete head; head = nullptr; } else { Node* current = head; while (current->next->next != nullptr) { current = current->next; } delete current->next; current->next = nullptr; } } void displayList(Node* head) { Node* current = head; while (current != nullptr) { cout << current->data << " "; current = current->next; } cout << endl; } int main() { int n; cin >> n; cin.ignore(); Node* head = nullptr; for (int i = 0; i < n; i++) { string value; getline(cin, value); insertEnd(head, value); } deleteLastNode(head); //awasthi displayList(head); while (head != nullptr) { Node* temp = head; head = head->next; delete temp; } return 0; }

You are given an integer n representing the number of nodes in a singly linked list. Each node contains a string value. Your task is to implement a program that creates a singly linked list with the given number of nodes and string values and then deletes the last node from the list. Finally, you need to print the contents of the modified linked list. Note: This is a sample question asked in a HCL interview. Input format : The first line of input consists of an integer n, representing the number of nodes in the singly linked list. The next n lines of input consist of n strings, where each line represents the string value of a node in the linked list. Output format : The output should print the elements of the singly linked list after deleting the last node. The elements should be separated by space.

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; typedef struct Node Node; Node* createNode(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { printf("Memory allocation failed.\n"); exit(1); } newNode->data = value; newNode->next = NULL; return newNode; } void insertEnd(Node** head, int value) { Node* newNode = createNode(value); if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } void removeGreaterThanX(Node** head, int x) { while (*head != NULL && (*head)->data > x) { Node* temp = *head; *head = (*head)->next; free(temp); } if (*head == NULL) { return; } Node* current = *head; while (current->next != NULL) { if (current->next->data > x) { Node* temp = current->next; current->next = temp->next; free(temp); } else { current = current->next; } } } void displayList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { int n, value, x; Node* head = NULL; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &value); insertEnd(&head, value); } scanf("%d", &x); printf("Original Linked List: "); displayList(head); removeGreaterThanX(&head, x); printf("Modified Linked List: "); displayList(head); //awasthi while (head != NULL) { Node* temp = head; head = head->next; free(temp); } return 0; }

Madhev wants to remove all nodes with values greater than a specified value 'x' from a singly linked list. He needs your help to write a program that takes the size of the linked list, the elements of the linked list, and the value 'x' as input. Additionally, he wants to insert new nodes at the end of the linked list. The program should then delete all nodes with values greater than 'x' from the linked list and display the modified linked list. Write a program to solve Madhev's problem. Note: This is a sample question asked in a Cocubes interview. Input format : The first line of input consists of the size of the linked list n (an integer). The second line of input consists of the elements of the linked list arr (a sequence of space-separated integers). The last line of input consists of the value 'x' (an integer) to compare against the nodes. Output format : The output displays the original linked list. The modified linked list after removing nodes with values greater than 'x'.

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; typedef struct Node Node; Node* createNode(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { printf("Memory allocation failed.\n"); exit(1); } newNode->data = value; newNode->next = NULL; return newNode; } void insertEnd(Node** head, int value) { Node* newNode = createNode(value); if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } void deleteSecondToLast(Node* head) { if (head == NULL head->next == NULL head->next->next == NULL) { return; } Node* current = head; while (current->next->next->next != NULL) { current = current->next; } Node* secondToLast = current->next; current->next = secondToLast->next; free(secondToLast); } void displayList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { int size, value; Node* head = NULL; scanf("%d", &size); for (int i = 0; i < size; i++) { scanf("%d", &value); insertEnd(&head, value); } printf("Original Linked List: "); displayList(head); deleteSecondToLast(head); printf("Updated Linked List: "); displayList(head); //awasthi while (head != NULL) { Node* temp = head; head = head->next; free(temp); } return 0; }

Vennila is a student. She is learning data structure and a singly linked list. She wants to write a program to delete the second-to-last node of the linked list and also implement a program that deletes the second-to-last node of a singly linked list. Define a struct Node with two members: data to store the integer value and next to store the pointer to the next node in the list. Note: This is a sample question asked in a mPhasis interview. Input format : The input consists of the following: The first line contains an integer size, representing the number of elements in the linked list. The second line contains arr space-separated integers, representing the elements of the linked list, and inserts nodes at the end.

#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; typedef struct Node Node; Node* createNode(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); if (newNode == NULL) { printf("Memory allocation failed.\n"); exit(1); } newNode->data = value; newNode->next = NULL; return newNode; } void insertEnd(Node** head, int value) { Node* newNode = createNode(value); if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } void removeDuplicates(Node* head) { if (head == NULL) { return; } Node* current = head; while (current->next != NULL) { if (current->data == current->next->data) { Node* duplicate = current->next; current->next = duplicate->next; free(duplicate); } else { current = current->next; } } } void displayList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { int n, value; Node* head = NULL; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &value); insertEnd(&head, value); } printf("Original Linked List: "); displayList(head); removeDuplicates(head); printf("Linked List after removing duplicates: "); displayList(head); //awasthi while (head != NULL) { Node* temp = head; head = head->next; free(temp); } return 0; }

Sita wants to remove duplicate nodes from a sorted, singly linked list. Implement a program that takes a sorted linked list as input and removes any duplicate nodes, resulting in a modified linked list. Note: This is a sample question asked in an Accenture interview. Input format : The first line of input contains an integer representing the number of nodes in the linked list. The second line contains a series of space-separated integers, representing the values of the nodes in the sorted linked list. Output format : The output displays the modified linked list after removing duplicate nodes. Refer to the sample output for formatting specifications.

#include <iostream> using namespace std; struct Node { int data; Node* next; }; Node* createNode(int data) { Node* newNode = new Node; if (newNode == NULL) { cout << "Memory allocation failed!"; exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } Node* insertNode(Node* head, int data) { if (head == NULL) head = createNode(data); else { Node* temp = head; while (temp->next != NULL) temp = temp->next; temp->next = createNode(data); } return head; } Node* reverseList(Node* head) { Node* prev = NULL; Node* current = head; Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return prev; } Node* addTwoLists(Node* first, Node* second) { Node* res = NULL; Node* prev = NULL; Node* temp = NULL; int carry = 0, sum; while (first != NULL || second != NULL) { sum = carry + (first ? first->data : 0) + (second ? second->data : 0); carry = sum / 10; sum = sum % 10; temp = createNode(sum); if (res == NULL) res = temp; else prev->next = temp; prev = temp; if (first) first = first->next; if (second) second = second->next; } if (carry > 0) temp->next = createNode(carry); return reverseList(res); } void displayList(Node* head) { if (head == NULL) { cout << "Empty List!"; return; } Node* temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } cout << endl; } void deleteList(Node* head) { Node* temp; while (head != NULL) { temp = head; head = head->next; delete temp; } } int main() { int N, M; cin >> N; Node* first = NULL; for (int i = 0; i < N; i++) { int value; cin >> value; first = insertNode(first, value); } cin >> M; Node* second = NULL; for (int i = 0; i < M; i++) { int value; cin >> value; second = insertNode(second, value); } cout << "First linked list: "; displayList(first); cout << "Second linked list: "; displayList(second); // Reverse the linked lists to perform addition Node* reversedFirst = reverseList(first); Node* reversedSecond = reverseList(second); Node* result = addTwoLists(reversedFirst, reversedSecond); cout << "Total Sales: "; displayList(result); // Delete the linked lists deleteList(first); deleteList(second); deleteList(result); return 0; }//awasthi

You are working on a sales data management system. The sales data for each day is represented by two linked lists, where each node contains the sales values. Your task is to calculate the total sales by adding the sales values from the two linked lists and returning the result in a new linked list. Note: This is the sample question asked in Amazon's recruitment Input format : The first line of input consists of an integer N, representing the number of nodes in the first linked list. The second line consists of N space-separated integers, representing the sales values for each node in the first linked list. The third line consists of an integer M, representing the number of nodes in the second linked list. The fourth line consists of M space-separated integers, representing the sales values for each node in the second linked list. Output format : The first line of output prints the sales values of the first linked list, separated by space. The second line of output prints the sales values of the second linked list, separated by space. The third line prints the total sales, separated by space.

#include <iostream> using namespace std; // Node structure struct Node { int data; Node* next; }; // Function to create a new node Node* createNode(int data) { Node* newNode = new Node; newNode->data = data; newNode->next = NULL; return newNode; } // Function to insert a node at the end of the linked list Node* insertNode(Node* head, int data) { if (head == NULL) head = createNode(data); else { Node* temp = head; while (temp->next != NULL) temp = temp->next; temp->next = createNode(data); } return head; } // Function to left-shift the linked list by k nodes Node* leftShiftLinkedList(Node* head, int k) { if (head == NULL || k == 0) return head; Node* current = head; int count = 1; while (count < k && current != NULL) { current = current->next; count++; } if (current == NULL) return head; Node* kthNode = current; while (current->next != NULL) current = current->next; current->next = head; head = kthNode->next; kthNode->next = NULL; return head; } // Function to print the linked list void printList(Node* head) { Node* temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } cout << endl; } int main() { int N, k; cin >> N; Node* head = NULL; for (int i = 0; i < N; i++) { int value; cin >> value; head = insertNode(head, value); } cin >> k; cout << "Original Linked List: "; printList(head); head = leftShiftLinkedList(head, k); //awasthi cout << "Modified Linked List after left shift: "; printList(head); return 0; }

You are developing a Shift Schedule Management System for a company. The system maintains a list of employees and their shift assignments. Each employee is represented as a node in a linked list, where each node contains an integer representing the shift number. Your task is to implement a feature that allows left-shifting of the shift schedule by a given number of shifts. This is required when there are changes in the company's work schedule or staffing requirements. Note: This is the sample question asked in Microsoft recruitment. Input format : The first line contains an integer 'N' representing the number of shifts in the schedule. The next N space integer represents the shift numbers for each shift. The last line contains an integer 'k' representing the number of shifts to left-shift the schedule. Output format : Tje first line of output should display the original linked list representing the shift schedule. The next line should display the modified linked list after left-shifting the schedule by 'k' shifts.

#include <iostream> using namespace std; struct Node { int digit; Node* next; Node(int val) : digit(val), next(nullptr) {} }; Node* reverseList(Node* head) { Node* prev = nullptr; Node* current = head; Node* nextNode = nullptr; while (current != nullptr) { nextNode = current->next; current->next = prev; prev = current; current = nextNode; } return prev; } Node* addOneToBarcode(Node* head) { if (head == nullptr) { return new Node(1); // Create a new node with value 1 } head = reverseList(head); Node* current = head; int carry = 1; while (current != nullptr) { int sum = current->digit + carry; current->digit = sum % 10; carry = sum / 10; if (carry == 0) { break; // No need to continue if there's no carry } current = current->next; } if (carry > 0) { // Add a new node for the carry if necessary Node* newNode = new Node(carry); current->next = newNode; } return reverseList(head); } void printBarcode(Node* head) { while (head != nullptr) { cout << head->digit << " "; head = head->next; } cout << endl; } int main() { int n; cin >> n; Node* head = nullptr; Node* tail = nullptr; for (int i = 0; i < n; i++) { int digit; cin >> digit; Node* newNode = new Node(digit); if (head == nullptr) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } }//awasthi head = addOneToBarcode(head); printBarcode(head); return 0; }

You are working on a program for an inventory management system in a retail store. The store uses barcodes to label and track its products. Each barcode is represented as a linked list, where each digit of the barcode is stored in a separate node. Your task is to write a function that adds 1 to the barcode value and updates the linked list accordingly. For example, 1999 is represented as (1-> 9-> 9 -> 9), and adding 1 to it should change it to (2->0->0->0) Note: This is a sample question asked in the Flipkart interview. Input format : The first line of input contains an integer n, indicating the number of digits in the barcode. The second line of input contains n space-separated integers, representing the digits of the barcode. Output format : The output prints the linked list representing the modified barcode, after adding 1 to its value. Code constraints : The barcode represents a non-negative integer. The number of digits in the barcode can vary.

#include <iostream> using namespace std; struct Node { int price; Node* next; Node(int val) : price(val), next(nullptr) {} }; int sumOfLastMItems(Node* head, int m) { int n = 0; // Total number of nodes Node* current = head; // Calculate the total number of nodes in the linked list while (current != nullptr) { n++; current = current->next; } // Reset the current pointer to the head current = head; // Traverse to the (n - m)-th node for (int i = 0; i < n - m; i++) { current = current->next; } int sum = 0; // Sum the prices of the last m nodes for (int i = 0; i < m && current != nullptr; i++) { sum += current->price; current = current->next; } return sum; } int main() { int n; cin >> n; Node* head = nullptr; Node* tail = nullptr; for (int i = 0; i < n; i++) { int price; cin >> price; Node* newNode = new Node(price); if (head == nullptr) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } //awasthi int m; cin >> m; int result = sumOfLastMItems(head, m); cout << result << endl; return 0; }

Imagine you are a cashier working at a grocery store. As customers come to your register, you scan each item and add it to a linked list to keep track of the purchases. Each node in the linked list represents an item, and it contains the price of the item and a reference to the next item. At the end of the day, you need to calculate the sum of the prices of the last m items sold. This information is important for inventory management and financial reporting purposes. By knowing the sum of the last m items, you can keep track of the total revenue generated and ensure accurate stock management. Given a linked list and a number m. Find the sum of the last m nodes of the linked list. Note: This question is asked in Adobe. Input format : The first line of input consists of the size n. The second line of input consists of n elements, separated by space. The third line of input consists of the value of m. Output format : The output prints the sum of the last m nodes of the given linked list.