ru
Feedback
inactive

inactive

Закрытый канал
3 849
Подписчики
Нет данных24 часа
-297 дней
-12730 дней
Архив постов
A Library online form application wants to implement an available book details feature using a Circular Header Linked List. The application allows users to add the books at the end. Write a program to add elements at the end of the circular header linked list. Note: This is a sample question asked in Accenture recruitment. Input format : The first line represents the size of the list n The next n lines represent the character elements inside the list. Output format : The output represents the circular Header linked list by insertion at the end. If no elements are inserted, print "Linked List is empty."

#include <iostream> using namespace std; // Define a structure for a node in the linked list struct Node { int row, col, value; Node* right; Node* down; }; // Define a structure for the linked list headers struct Header { Node* next; }; int main() { int r, c; cin >> r >> c; // Initialize row and column headers Header* rowHeaders = new Header[r]; Header* colHeaders = new Header[c]; // Initialize headers for (int i = 0; i < r; i++) { rowHeaders[i].next = nullptr; } for (int j = 0; j < c; j++) { colHeaders[j].next = nullptr; } // Parse input and create nodes for non-zero elements for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int value; cin >> value; if (value != 0) { Node* newNode = new Node(); newNode->row = i; newNode->col = j; newNode->value = value; newNode->right = nullptr; newNode->down = nullptr; // Insert node into row and column linked lists if (rowHeaders[i].next == nullptr) { rowHeaders[i].next = newNode; } else { Node* current = rowHeaders[i].next; while (current->right != nullptr) { current = current->right; } current->right = newNode; } if (colHeaders[j].next == nullptr) { colHeaders[j].next = newNode; } else { Node* current = colHeaders[j].next; while (current->down != nullptr) { current = current->down; } current->down = newNode; } } } } // Print the sparse matrix for (int i = 0; i < r; i++) { Node* current = rowHeaders[i].next; for (int j = 0; j < c; j++) { if (current != nullptr && current->col == j) { cout << current->value << " "; current = current->right; } else { cout << "0 "; } } cout << endl; } // Clean up memory for (int i = 0; i < r; i++) { Node* current = rowHeaders[i].next; while (current != nullptr) { Node* temp = current; current = current->right; delete temp; } } delete[] rowHeaders; delete[] colHeaders; return 0; }//awasthi

You are tasked with implementing a student grades matrix using the Sparse Matrix Representation with a grounded header linked list. The matrix will store the grades of students for different subjects. So, write logic to implement a sparse matrix using a Grounded header linked list. Note: This is a sample question asked in an Infosys interview. Input format : The first line represents a row r of the matrix. The second line represents column c of the matrix. The remaining line's, r*c values consist of matrix elements.

#include <iostream> using namespace std; // Define the structure for a node in the linked list struct Node { int data; Node* next; }; // Function to insert a node at the end of a linked list void insertNode(Node*& head, int data) { Node* newNode = new Node; newNode->data = data; newNode->next = nullptr; if (!head) { head = newNode; } else { Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } } // Function to split a linked list into even and odd lists void splitEvenOdd(Node* head, Node*& evenList, Node*& oddList) { Node* current = head; while (current) { if (current->data % 2 == 0) { insertNode(evenList, current->data); } else { insertNode(oddList, current->data); } current = current->next; } } // Function to print a linked list void printList(Node* head) { Node* current = head; while (current) { cout << current->data << " "; current = current->next; } } int main() { int n; cin >> n; Node* head = nullptr; for (int i = 0; i < n; i++) { int data; cin >> data; insertNode(head, data); } Node* evenList = nullptr; Node* oddList = nullptr; splitEvenOdd(head, evenList, oddList); cout << "Even List: "; printList(evenList); cout << endl; cout << "Odd List: "; printList(oddList); cout << endl; return 0; } //awasthi

You are developing a program for a company to manage employee records. As part of the record management functionality, you need to implement a function that splits a grounded header-linked list of employee records into two separate lists: one for employees with even employee IDs and another for employees with odd employee IDs. Implement a function to split a grounded header linked list into two separate lists, dividing the elements based on their parity (even and odd). Note: This is a sample question asked in Infosys 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. Output format : The first line of output prints the list of even elements. The second line of output prints the list of odd elements.

#include <iostream> using namespace std; // Define the structure of a linked list node struct Node { int data; Node* next; }; // Function to insert a new node at the beginning of the linked list Node* insertAtBeginning(Node* head, int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = head; return newNode; } // Function to print the linked list void printList(Node* head) { while (head != nullptr) { cout << head->data << " "; head = head->next; } cout << endl; } int main() { int n, value; Node* head = nullptr; cin >> n; while (n != -1) { head = insertAtBeginning(head, n); cin >> n; } // Print the updated list printList(head); return 0; }//awasthi

You are developing a contact management system for a mobile application. The system allows users to maintain a list of their contacts. Whenever a user adds a new contact, the system should add it at the front of the contact list. Write a program to implement the code to perform insertion at the beginning using Grounded Header Linked List. Note: This is a sample question asked in TCS recruitment. Input format : The first line represents the size of element n. The next n lines store the values in it. Enter -1 to stop.

#include <iostream> using namespace std; // Define a struct for the linked list node struct Node { char data; Node* next; Node(char val) : data(val), next(nullptr) {} }; // Function to insert a character at a specific index Node* insertAtIndex(Node* head, int index, char val) { if (index < 0) { cout << "Invalid position." << endl; return head; } Node* newNode = new Node(val); if (index == 0) { newNode->next = head; return newNode; } Node* prev = nullptr; Node* current = head; int currentIndex = 0; while (current != nullptr && currentIndex < index) { prev = current; current = current->next; currentIndex++; } if (currentIndex == index) { prev->next = newNode; newNode->next = current; } else { cout << "Invalid position." << endl; delete newNode; } return head; } // Function to print the linked list void printList(Node* head) { while (head != nullptr) { cout << head->data << " "; head = head->next; } cout << endl; } int main() { int n; cin >> n; Node* head = nullptr; for (int i = 0; i < n; i++) { char val; cin >> val; head = insertAtIndex(head, i, val); } int index; cin >> index; char charToInsert; cin >> charToInsert; head = insertAtIndex(head, index, charToInsert); cout << "Updated list: "; printList(head); return 0; } //awasthi

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. Output format : For each insertion operation, display the updated list after inserting the character, at the specified index. (index starts from 0) Print "Invalid position" for invalid input numbers.

// You are using GCC #include <iostream> struct Node { int data; Node* next; Node(int value) : data(value), next(nullptr) {} }; Node* deleteNodeAtPosition(Node* head, int position) { if (position <= 0 || !head) { return head; } if (position == 1) { Node* temp = head; head = head->next; delete temp; return head; } Node* current = head; Node* prev = nullptr; int currentPosition = 1; while (current && currentPosition < position) { prev = current; current = current->next; currentPosition++; } if (current) { prev->next = current->next; delete current; } return head; } void printLinkedList(Node* head) { Node* current = head; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { int n; std::cin >> n; Node* head = nullptr; for (int i = 0; i < n; ++i) { int value; std::cin >> value; Node* newNode = new Node(value); if (!head) { head = newNode; } else { Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } } int positionToDelete; std::cin >> positionToDelete; head = deleteNodeAtPosition(head, positionToDelete); printLinkedList(head); // Free memory by deleting nodes while (head) { Node* temp = head; head = head->next; delete temp; } return 0; } //awasthi

You are developing a program for an online shopping cart system. Each node in the singly linked list represents a product added to the cart. The program should allow for deleting a product from the cart at a specific position and displaying the updated cart. Write a program to implement the online shopping cart system according to the following specifications: The program should prompt the user to enter the number of products in the cart. For each product, the program should prompt the user to enter its product code (an integer value). The program should create a singly linked list with nodes representing the products in the cart, where each node contains the product code. The program should then prompt the user to enter the position of the product to be deleted from the cart. The program should delete the node at the specified position from the linked list. Finally, the program should traverse the updated linked list and print the product codes of the remaining products in the cart. Note: This is a sample question asked in a TCS interview. Input format : The first line of input consists of an integer n, the number of nodes in the linked list. The second line of input consists of n integers, representing the data value of each node in the linked list, separated by space. The third line of input consists of an integer x, representing the position of the node to be deleted. (1-based) Output format : The output consists of a single line containing the data values of the modified linked list after deleting the node at position x. The data values should be separated by a space. Code constraints : 1 ≤ n ≤ 10^5 (number of elements in the linked list) -10^9 ≤ value of each node ≤ 10^9 1 ≤ x ≤ n (position of the node to be deleted)

// You are using GCC #include <iostream> struct Node { int data; Node* next; Node(int value) : data(value), next(nullptr) {} }; Node* insertAtEnd(Node* head, int value) { Node* newNode = new Node(value); if (!head) { head = newNode; } else { Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } return head; } Node* deleteCommonNodes(Node* list1, Node* list2) { Node* current1 = list1; Node* prev1 = nullptr; while (current1) { Node* current2 = list2; bool found = false; while (current2) { if (current1->data == current2->data) { found = true; break; } current2 = current2->next; } if (found) { if (prev1) { prev1->next = current1->next; delete current1; current1 = prev1->next; } else { list1 = current1->next; delete current1; current1 = list1; } } else { prev1 = current1; current1 = current1->next; } } return list1; } void printLinkedList(Node* head) { Node* current = head; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { int n; std::cin >> n; Node* list1 = nullptr; for (int i = 0; i < n; ++i) { int value; std::cin >> value; list1 = insertAtEnd(list1, value); } int m; std::cin >> m; Node* list2 = nullptr; for (int i = 0; i < m; ++i) { int value; std::cin >> value; list2 = insertAtEnd(list2, value); } std::cout << "First Linked List before deletion: "; printLinkedList(list1); list1 = deleteCommonNodes(list1, list2); std::cout << "First Linked List after deletion: "; printLinkedList(list1); Node* temp = list1; bool allSame = true; int prevValue = (temp != nullptr) ? temp->data : 0; while (temp) { if (temp->data != prevValue) { allSame = false; break; } prevValue = temp->data; temp = temp->next; } if (allSame) { std::cout << "All elements in the first linked list are the same." << std::endl; } // Free memory by deleting nodes while (list1) { Node* temp = list1; list1 = list1->next; delete temp; } while (list2) { Node* temp = list2; list2 = list2->next; delete temp; } return 0; } //awasthi

Dharun is working on a program to manipulate linked lists. He wants to write a function that takes two linked lists as input, inserts nodes at the end, and deletes all the nodes from the first list that also appear in the second list. Dharun needs your help to implement this function. The function should take two linked lists, list1 and list2, as input, where each list is represented by its head node. Note: This is a sample question asked in a Capgemini interview. Input format : The first line contains an integer n, denoting the number of nodes in list1. The next line contains n space-separated integers, representing the values of the nodes in list1. The next line contains an integer m, denoting the number of nodes in list2. The next line contains m space-separated integers, representing the values of the nodes in list2. Output format : The first line of output displays the elements of the first linked list before the deletion, separated by a space. The second line of output displays the elements of the first linked list after the deletion, separated by a space. If all elements in the first linked list are the same after deletion, the third line will be displayed stating, "All elements in the first linked list are the same."

// You are using GCC #include <iostream> // Define the structure for a singly linked list node struct Node { int data; Node* next; Node(int value) : data(value), next(nullptr) {} }; // Function to create a linked list of student records Node* createLinkedList(int n) { if (n <= 0) { return nullptr; } Node* head = nullptr; Node* tail = nullptr; for (int i = 0; i < n; ++i) { int id; std::cin >> id; Node* newNode = new Node(id); if (!head) { head = newNode; tail = newNode; } else { tail->next = newNode; tail = newNode; } } return head; } // Function to delete the first node from the linked list Node* deleteFirstNode(Node* head) { if (!head) { return nullptr; } Node* newHead = head->next; delete head; return newHead; } // Function to print the linked list void printLinkedList(Node* head) { Node* current = head; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { int n; std::cin >> n; Node* head = createLinkedList(n); if (head) { head = deleteFirstNode(head); printLinkedList(head); } // Free memory by deleting remaining nodes Node* current = head; while (current) { Node* temp = current; current = current->next; delete temp; } return 0; } //awasthi

You are tasked with developing a program to manage a student records system using a singly linked list. Each node in the linked list represents a student and contains their unique ID. The program should allow for creating a list of student records, deleting the first student's record, and printing the remaining student IDs. Write a program to implement the student records system according to the following specifications: The program should prompt the user to enter the number of students n for which records need to be created. For each student, the program should prompt the user to enter their student ID (an integer value). The program should create a singly linked list with n nodes, where each node represents a student and contains their student ID. After creating the linked list, the program should delete the first node from the list. Finally, the program should traverse the modified linked list and print the student IDs of the remaining students. 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 nodes in the linked list. The second line of input consists of n space-separated integers, representing the values of each node in the linked list.

// You are using GCC #include <iostream> class Node { public: int value; Node* next; Node(int val) : value(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insert(int value) { Node* newNode = new Node(value); if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } void print() { Node* current = head; while (current != nullptr) { std::cout << current->value << " "; current = current->next; } std::cout << std::endl; } void deleteMiddleNode() { if (head == nullptr head->next == nullptr head->next->next == nullptr) { return; } Node* slow = head; Node* fast = head; Node* prev = nullptr; while (fast != nullptr && fast->next != nullptr) { fast = fast->next->next; prev = slow; slow = slow->next; } prev->next = slow->next; delete slow; } }; int main() { int numNodes, value; std::cin >> numNodes; LinkedList list; for (int i = 0; i < numNodes; ++i) { std::cin >> value; list.insert(value); } std::cout << "Original Linked List: "; list.print(); list.deleteMiddleNode(); std::cout << "Updated Linked List: "; list.print(); return 0; }

Prackya wants to implement a function to delete the middle node of a singly linked list. She wants you to write a program that takes user input to construct a linked list, inserts nodes at the end, and then deletes the middle node if it exists. If the linked list has an even number of nodes, the function should delete the second middle node. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5. For example, if the given linked list is 1->2->3->4->5->6, then it should be modified to 1->2->3->5->6. Note: This is a sample question asked in an AMCAT interview. Input format : The first line of input consists of the number of nodes in the linked list (numNodes) The second line of input consists of the values for each node, inserted at the end of the list. Output format : The output displays the original linked list before deleting the middle node. The updated linked list after deleting the middle node.

#include <iostream> struct Node { int data; Node* next; Node(int data) : data(data), next(nullptr) {} }; void swapPairs(Node* head) { Node* current = head; while (current != nullptr && current->next != nullptr) { // Swap the data of adjacent nodes int temp = current->data; current->data = current->next->data; current->next->data = temp; // Move current to the next even node current = current->next->next; } } void printLinkedList(Node* head) { Node* current = head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { int n; std::cin >> n; Node* head = nullptr; Node* tail = nullptr; for (int i = 0; i < n; ++i) { int value; std::cin >> value; Node* newNode = new Node(value); if (!head) { head = tail = newNode; } else { tail->next = newNode; tail = newNode; } } std::cout << "Linked list before swapping pairwise: "; printLinkedList(head); swapPairs(head); std::cout << "Linked list after swapping pairwise: "; printLinkedList(head); // Clean up memory while (head) { Node* temp = head; head = head->next; delete temp; } return 0; }

Imagine you are a teacher preparing seating arrangements for a classroom. You have a list of students' names, represented by a singly linked list. The linked list is arranged in a specific order, but you want to pair up the students in a different way for a group activity. To achieve this, you need to write a function that swaps elements pairwise in the linked list. Each pair of students will sit together during the activity, fostering collaboration and teamwork. By rearranging the linked list, you can create new pairs of students without changing their individual positions in the list. For example, if the linked list is 1->2->3->4->5 then the program should change it to 2->1->4->3->5. Note: This question is asked by Amazon, Microsoft, and Moonfrog Labs. Input format : The first line of input consists of the size n. The second line of input consists of n elements, separated by space. Output format : The first line of output prints the linked list before swapping pairwise. The second line of output prints the linked list after swapping pairwise.

#include <iostream> struct Node { int data; Node* next; Node(int data) : data(data), next(nullptr) {} }; int calculateDecimalValue(Node* head) { int decimalValue = 0; Node* current = head; while (current != nullptr) { decimalValue = decimalValue * 2 + current->data; current = current->next; } return decimalValue; } void printLinkedList(Node* head) { Node* current = head; if (current == nullptr) { std::cout << "Empty linked list"; return; } while (current != nullptr) { std::cout << current->data << " "; current = current->next; } } int main() { int n; std::cin >> n; Node* head = nullptr; Node* tail = nullptr; for (int i = 0; i < n; ++i) { int value; std::cin >> value; Node* newNode = new Node(value); if (!head) { head = tail = newNode; } else { tail->next = newNode; tail = newNode; } } std::cout << "Linked List: "; printLinkedList(head); std::cout << std::endl; int decimalValue = calculateDecimalValue(head); std::cout << "Decimal Value: " << decimalValue << std::endl; // Clean up memory while (head) { Node* temp = head; head = head->next; delete temp; } return 0; } //awasthi