ar
Feedback
inactive

inactive

قناة بسيطة

...

إظهار المزيد
3 849
المشتركون
لا توجد بيانات24 ساعات
-297 أيام
-12730 أيام
أرشيف المشاركات
#include <iostream> using namespace std; struct Node { int data; Node* next; }; Node* head = nullptr; void insertAtBeginning(int item) { Node* newNode = new Node; newNode->data = item; newNode->next = head; head = newNode; cout << "Node inserted" << endl; } void traverseLinkedList() { Node* current = head; cout << "Linked List: "; while (current != nullptr) { cout << current->data << " "; current = current->next; } cout << endl; } int main() { int choice, item; do { cin >> item; insertAtBeginning(item); cin >> choice; } while (choice == 0); //awasthi traverseLinkedList(); cout << "Node ended"; return 0; }

Kathir wants to create a program that allows him to build a linked list by inserting nodes at the beginning. He wants to be able to input the data for each node and specify when to stop inserting nodes (defined as 0). After inserting the nodes, he wants to display the contents of the linked list. Help Kathir by providing the required input and output formats for the code. Note: This is a sample question asked in a Wipro interview. Input format : The first input consists of an integer, which is the element inserted in the node (n). The second input consists of the following: if choice 0 is entered, continue to insert the element in the node; otherwise, end the node inserted. Output format : For each node inserted at the beginning, the output displays the message "Node inserted" on a new line. After inserting all the nodes, the output displays the contents of the linked list in a space-separated format on a new line, preceded by the text "Linked List: ". Finally, the output displays the message "Node ended" on a new line.

#include <iostream> using namespace std; struct Node { int data; Node* next; }; void push(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void append(Node** head_ref, int new_data) { Node* new_node = new Node(); new_node->data = new_data; Node* last = *head_ref; new_node->next = NULL; if (*head_ref == NULL) { *head_ref = new_node; return; } while (last->next != NULL) { last = last->next; } last->next = new_node; } void printList(Node* node) { while (node != NULL) { cout << " " << node->data; node = node->next; } } int main() { Node* head = NULL; int num_of_nodes, new_val; cin >> num_of_nodes; for (int i = 0; i < num_of_nodes; i++) { int val; cin >> val; push(&head, val); } cout << "Created Linked list:"; printList(head); cin >> new_val; append(&head, new_val); cout << "\nFinal list:"; printList(head); return 0; }//awasthi

Kamal wants to create a linked list and perform the following operations on it: Insert a node at the beginning of the linked list. Append a node at the end of the linked list. Print the final linked list. Write a program that takes the number of nodes to be inserted, followed by their values, as input. After inserting the nodes, the program should ask for a new value and append a node with that value at the end of the linked list. Finally, the program should print the contents of the linked list. Example Input: 5 1 2 3 4 5 6 Output: Created Linked list: 5 4 3 2 1 Final list: 5 4 3 2 1 6

#include <iostream> class Node { public: int data; Node* next; Node(int value) { data = value; next = nullptr; } }; class LinkedList { public: Node* head; LinkedList() { head = nullptr; } void insertNode(int position, int value) { Node* newNode = new Node(value); if (position == 1) { newNode->next = head; head = newNode; } else { Node* current = head; int currentPosition = 1; while (currentPosition < position - 1 && current) { current = current->next; currentPosition++; } if (current) { newNode->next = current->next; current->next = newNode; } } } void displayList() { Node* current = head; while (current) { std::cout << current->data << ' '; current = current->next; } std::cout << std::endl; } }; int main() { int n; std::cin >> n; LinkedList linkedList; for (int i = 0; i < n; i++) { int value; std::cin >> value; linkedList.insertNode(i + 1, value); }//awasthi int position, newValue; std::cin >> position >> newValue; linkedList.insertNode(position, newValue); linkedList.displayList(); return 0; }

Dhanush is in the process of studying data structures, particularly linked lists. To begin, he initiates the development of a program geared towards handling a singly linked list. The program's primary aim is to enable the user to input data for creating a linked list and facilitate the insertion of a new element at a specified position within the list. Input format : The first line of input consists of an integer n, representing the number of nodes in the linked list. The second line consists of n space-separated integers, representing the nodes. The third line consists of the position where the new element has to be added. The fourth line consists of the value of the element. Output format : The output prints the linked list after inserting the given element at the specified position. Code constraints : 1 <= n <= 100

#include <iostream> class Node { public: int data; Node* next; Node(int value) { data = value; next = nullptr; } }; class LinkedList { public: Node* head; LinkedList() { head = nullptr; } void insertNode(int value) { Node* newNode = new Node(value); if (!head) { head = newNode; } else { Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } } void displayList() { if (!head) { std::cout << "Linked List is empty." << std::endl; } else { std::cout << "Linked List: "; Node* current = head; while (current) { std::cout << current->data << ' '; current = current->next; } std::cout << std::endl; } } }; int main() { LinkedList linkedList; int value; while (true) { std::cin >> value; if (value < 0) { break; } linkedList.insertNode(value); } linkedList.displayList(); return 0; } //awasthi

Uma wants to create a program that allows her to build a linked list by inserting nodes at the end. She wants to be able to input the data for each node and specify when to stop inserting nodes (a negative value is entered, indicating the end of the input). After inserting the nodes, she wants to display the contents of the linked list. Note: This is a sample question asked in a Wipro interview. Input format : The input consists of an integer value for each node to be inserted at the end of the linked list. After inserting each node, enter a non-negative integer indicating the value of the next node to be inserted. If a negative integer is entered, it indicates the end of node insertion. The input terminates when a negative integer is entered. Output format : If the linked list is empty, the output displays the message "Linked List is empty." on a new line. If the linked list is not empty, the output displays the contents of the linked list in a space-separated format on a new line, preceded by the text "Linked List: ". Each node's data is displayed in the order it was inserted.

#include <iostream> // Node structure for Grounded Header Linked List struct Node { int data; Node* next; }; // Function to insert a node at the beginning of the linked list void insertAtBeginning(Node*& header, int newData) { // Create a new node Node* newNode = new Node; newNode->data = newData; // Update the pointers newNode->next = header->next; header->next = newNode; } // Function to display the linked list void displayList(const Node* header) { // Start from the first node after the header Node* current = header->next; // Traverse and print the linked list while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { // Create the grounded header node Node* header = new Node; header->next = nullptr; int newData; while (true) { std::cin >> newData; if (newData == -1) { break; } // Insert node at the beginning insertAtBeginning(header, newData); } displayList(header); return 0; } //awasthi

#include <iostream> #include <vector> using namespace std; class Node { public: int data; int row; int col; Node* next_row; Node* next_col; Node(int data, int row, int col) { this->data = data; this->row = row; this->col = col; this->next_row = nullptr; this->next_col = nullptr; } }; class SparseMatrix { private: int rows; int cols; std::vector<Node*> header_row; std::vector<Node*> header_col; public: SparseMatrix(int rows, int cols) { this->rows = rows; this->cols = cols; this->header_row.resize(rows, nullptr); this->header_col.resize(cols, nullptr); } void insert_element(int data, int row, int col) { if (row >= this->rows || col >= this->cols) { throw std::out_of_range("Invalid matrix position"); } Node* new_node = new Node(data, row, col); if (this->header_row[row] == nullptr) { this->header_row[row] = new_node; } else { Node* current = this->header_row[row]; while (current->next_row != nullptr) { current = current->next_row; } current->next_row = new_node; } if (this->header_col[col] == nullptr) { this->header_col[col] = new_node; } else { Node* current = this->header_col[col]; while (current->next_col != nullptr) { current = current->next_col; } current->next_col = new_node; } } void display() { for (int i = 0; i < this->rows; i++) { for (int j = 0; j < this->cols; j++) { Node* current = this->header_row[i]; while (current != nullptr && current->col < j) { current = current->next_row; } if (current != nullptr && current->col == j) { std::cout << current->data << " "; } else { std::cout << "0 "; } } std::cout << std::endl; } } }; int main() { int r,c,i,j; cin>>r>>c; SparseMatrix matrix(r, c); for(i =0; i < r;i++) { for(j = 0; j < c;j++) { int data; cin>>data; matrix.insert_element(data, i, j); } } matrix.display(); 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.

Problem Statement 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> struct Node { int data; Node* next; }; void deleteNode(Node* head, int value) { Node* curr = head->next; Node* prev = head; // Traverse the linked list to find the node to be deleted while (curr != nullptr) { if (curr->data == value) { prev->next = curr->next; delete curr; break; } prev = curr; curr = curr->next; } } int main() { // Create the grounded header node Node* head = new Node(); head->next = nullptr; // Add nodes to the list based on user input int value; while (std::cin >> value && value != -1) { // Create a new node Node* newNode = new Node(); newNode->data = value; // Insert the new node at the beginning of the list newNode->next = head->next; head->next = newNode; } while (std::cin >> value && value != -1) { deleteNode(head, value); } // Print the updated list Node* current = head->next; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } //awasthi return 0; }

Suppose you are working on a student management system for a university, and you need to implement a deletion functionality for a grounded header linked list to manage student records. Write a program to implement deletion logic for a Grounded Header Linked List with multiple deletions based on user input. Example Input: 10 20 30 40 50 -1 20 30 -1 Output: 50 40 10 Explanation: The elements of the linked list: 10 20 30 40 50 The elements to be deleted: 20 30 So, after deleting the given elements, the linked list will be 10 40 50. Print the linked list in the reverse order: 50 40 10. Note: This is a sample question asked in a Capgemini interview.

// You are using GCC #include <iostream> #include <vector> using namespace std; // Node structure for the linked list struct Node { int data; Node* next; Node(int val) : data(val), next(NULL) {} }; // Function to rotate the lineup Node* rotateLineup(Node* header, int k) { if (!header->next || k == 0) { return header; // No rotation needed } // Calculate the length of the list int length = 0; Node* current = header->next; while (current) { length++; current = current->next; } // Calculate the actual rotation amount k = k % length; if (k == 0) { return header; // No rotation needed } // Find the new tail and head of the rotated list Node* tail = header->next; Node* new_head = header->next; for (int i = 0; i < length - k - 1; i++) { tail = tail->next; } new_head = tail->next; // Update the pointers to perform the rotation tail->next = NULL; current = new_head; while (current->next) { current = current->next; } current->next = header->next; header->next = new_head; return header; } // Function to print the lineup void printLineup(Node* header) { Node* current = header->next; while (current) { cout << current->data << " "; current = current->next; } cout << endl; } int main() { int n; cin >> n; // Create the grounded header linked list Node* header = new Node(-1); // Header node Node* current = header; for (int i = 0; i < n; i++) { int num; cin >> num; current->next = new Node(num); current = current->next; } int k; cin >> k; // Print the original list cout << "Original List: "; printLineup(header); // Rotate the lineup header = rotateLineup(header, k); // Print the rotated list cout << "Rotated List: "; printLineup(header); return 0; } //awasthi

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.

// You are using GCC #include <iostream> using namespace std; class Node { public: int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insert(int val) { Node* newNode = new Node(val); if (!head) { head = newNode; } else { Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } } void removeDuplicates() { if (!head) { cout << "List is empty" << endl; return; } Node* current = head; while (current && current->next) { if (current->data == current->next->data) { Node* temp = current->next; current->next = temp->next; delete temp; } else { current = current->next; } } } void display() { Node* current = head; //cout << "Original Linked List: "; while (current) { cout << current->data << " "; current = current->next; } cout << endl; } }; int main() { int n; cin >> n; LinkedList list; for (int i = 0; i < n; i++) { int val; cin >> val; list.insert(val); } cout<<"Original Linked list: "; list.display(); list.removeDuplicates(); cout << "Linked List after removing duplicates: "; list.display(); //awasthi 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. Code constraints : The linked list should be sorted in ascending order. 1 <= number of nodes <= 100 -50000 <= values of nodes <= 50000

#include <iostream> #include <string> using namespace std; class Node { public: string data; Node* next; Node(string val) : data(val), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insert(string val) { Node* newNode = new Node(val); if (!head) { head = newNode; } else { Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } } void deleteAlternateNodes() { if (!head) { cout << "List is empty" << endl; return; } Node* current = head; Node* prev = nullptr; bool deleteNext = false; while (current) { if (deleteNext) { prev->next = current->next; delete current; current = prev->next; } else { prev = current; current = current->next; } deleteNext = !deleteNext; } } void display() { Node* current = head; //cout << "Linked list data: "; while (current) { cout << current->data << " "; current = current->next; } cout << endl; } }; int main() { int n; cin >> n; LinkedList list; for (int i = 0; i < n; i++) { string val; cin >> val; list.insert(val); } cout<<"Linked list data: "; list.display(); list.deleteAlternateNodes(); cout << "After deleting alternate node:"; list.display(); return 0; } //awasthi

Your task is to write a program that takes input for the number of elements in the linked list and the corresponding string values for each element. Based on this input, your program should create a linked list and then delete the alternate nodes from it. Note: This is a sample question asked in a HCL interview. Input format : The first line contains an integer n, the number of elements in the linked list. The second line contains n space-separated strings representing the elements of the linked list. Output format : If the linked list is empty, output "List is empty". If the linked list is not empty, output the following: The first line should display the elements of the original linked list, separated by a space. The second line should display the elements of the linked list after deleting the alternate nodes, separated by a space.