ar
Feedback
inactive

inactive

قناة بسيطة

...

إظهار المزيد
3 849
المشتركون
لا توجد بيانات24 ساعات
-297 أيام
-12730 أيام
أرشيف المشاركات
#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> class Node { public: int data; Node* next; Node(int data) : data(data), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void append(int data) { Node* new_node = new Node(data); if (!head) { head = new_node; } else { Node* current = head; while (current->next) { current = current->next; } current->next = new_node; } } int sumLastMNodes(int m) { if (!head || m == 0) { return 0; } Node* current = head; int count = 0; while (current) { count++; current = current->next; } if (m > count) { m = count; } current = head; for (int i = 0; i < count - m; i++) { current = current->next; } int sum = 0; while (current) { sum += current->data; current = current->next; } return sum; } }; int main() { LinkedList list; int n, m, data; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> data; list.append(data); } std::cin >> m; int result = list.sumLastMNodes(m); std::cout << result << std::endl; return 0; } //awasthi

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.

#include <iostream> class Node { public: int data; Node* next; Node(int data) : data(data), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void append(int data) { Node* new_node = new Node(data); if (!head) { head = new_node; } else { Node* current = head; while (current->next) { current = current->next; } current->next = new_node; } } Node* reverseK(Node* head, int k) { Node* current = head; Node* prev = nullptr; Node* next = nullptr; int count = 0; while (current && count < k) { next = current->next; current->next = prev; prev = current; current = next; count++; } if (next && count >= k) { head->next = reverseK(next, k); } return prev; } void display() { Node* current = head; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } }; int main() { LinkedList list; int n, data, k; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> data; list.append(data); } std::cin >> k; std::cout << "Original Linked List: "; list.display(); list.head = list.reverseK(list.head, k); std::cout << "Modified Linked List: "; list.display(); return 0; } //awasthi

You are working on a data processing system for a manufacturing company. The company has a production line where items are produced and each item is represented by a node in a linked list. The linked list represents the order in which the items are produced. Your task is to develop a module that can reverse the order of production for a specific number of items at a time. This will help in optimizing the production process by grouping and processing items in batches. Note: This is the sample question asked in Paypal recruitment. Input format : The first line contains an integer N representing the number of nodes in the linked list. The second line contains the values of the nodes separated by spaces. The third line contains an integer representing the value of k. Output format : The first line displays the original linked list. The second line displays the modified linked list after reversing every k node.

#include <iostream> struct Node { int data; Node* next; Node(int data) : data(data), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void append(int data) { Node* new_node = new Node(data); if (!head) { head = new_node; } else { Node* current = head; while (current->next) { current = current->next; } current->next = new_node; } } void swapPairs() { Node* current = head; while (current && current->next) { std::swap(current->data, current->next->data); current = current->next->next; } } void display() { Node* current = head; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } }; int main() { LinkedList list; int n, data; std::cin >> n; for (int i = 0; i < n; ++i) { std::cin >> data; list.append(data); } std::cout << "Linked list before swapping pairwise: "; list.display(); list.swapPairs(); std::cout << "Linked list after swapping pairwise: "; list.display(); return 0; } //awasthi

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> class Node { public: int data; Node* next; Node(int value) : data(value), next(nullptr) {} }; class LinkedList { public: Node* head; LinkedList() : head(nullptr) {} void insertAtEnd(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 display() { if (!head) { std::cout << "Linked List is empty." << std::endl; return; } 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.insertAtEnd(value); } linkedList.display(); 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> #include <string> using namespace std; // A linked list node struct Node { string data; Node* next; }; // Given a reference (pointer to pointer) to the head of a list and a string, inserts a new node at the front of the list. void push(Node** head_ref, string new_data) { // Create a new node Node* new_node = new Node(); new_node->data = new_data; // Make the new node point to the current head new_node->next = (*head_ref); // Update the head to point to the new node (*head_ref) = new_node; } // Given a reference (pointer to pointer) to the head of a list and a string, appends a new node at the end void append(Node** head_ref, string new_data) { // Create a new node Node* new_node = new Node(); new_node->data = new_data; // Store the head reference in a temporary variable Node* last = *head_ref; // Set the next pointer of the new node as NULL since it will be the last node new_node->next = NULL; // If the Linked List is empty, make the new node as the head and return if (*head_ref == NULL) { *head_ref = new_node; return; } // Else traverse till the last node while (last->next != NULL) { last = last->next; } // Change the next pointer of the last node to point to the new node last->next = new_node; } // This function prints the contents of the linked list starting from the head void printList(Node* node) { while (node != NULL) { cout << " " << node->data; node = node->next; } } int main() { // Start with an empty list Node* head = NULL; int n; cin >> n; for (int i = 0; i < n; i++) { string str; cin >> str; push(&head, str); } cout << "Document:"; printList(head); string new_string; cin >> new_string; // Insert the new string at the end append(&head, new_string); cout << "\nUpdated Document:"; printList(head); return 0; }//awasthi

You are developing a text editor application that allows users to manage a document. The application uses a linked list data structure to represent the document content. Each node of the linked list contains a string representing a line of text. The application supports two operations: inserting new string values at the beginning of the document and appending a new string at the end of the document. If no input string list is created and if no value is appended to the list, then an empty list should be returned as output. 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 lines in the document. The next n lines of input consist of the lines of text that constitute the document. The last line of input consists of a string, s, which needs to be appended at the end of the document. Output format : The first line of output should print the initial document content, which inserts the given n values at the beginning. The second line of output should print the final document content, which appends the given value s at the end of the document.

#include <iostream> class Node { public: int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class LinkedList { private: Node* head; public: LinkedList() : head(nullptr) {} void appendLeft(int value) { Node* newNode = new Node(value); newNode->next = head; head = newNode; } void appendRight(int value) { Node* newNode = new Node(value); if (!head) { head = newNode; return; } Node* current = head; while (current->next) { current = current->next; } current->next = newNode; } void print() { Node* current = head; std::cout << "Linked List: "; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } ~LinkedList() { Node* current = head; while (current) { Node* temp = current; current = current->next; delete temp; } } }; int main() { LinkedList linkedList; while (true) { int choice; std::cin >> choice; if (choice == 1) { int value; std::cin >> value; linkedList.appendLeft(value); } else if (choice == 2) { int value; std::cin >> value; linkedList.appendRight(value); } else if (choice == 3) { linkedList.print(); } else if (choice == 4) { break; } else { std::cout << "Invalid choice" << std::endl; } } return 0; } //awasthi

Vijay wants to create a program that allows him to manipulate a linked list. He wants to be able to perform the following operations: 1: Append Left: Append a node at the beginning(left) of the linked list. 2: Append Right: Append a node at the end(right) of the linked list. 3: Print: Print the contents of the linked list. 4: Exit: Exit the program. Note: This is a sample question asked in Accenture recruitment. Input format : For inserting a node at the beginning of the linked list, input: 1 followed by the value For inserting at the end of the linked list, input: 2 followed by the value To display the current linked list, input: 3 To exit the program, input: 4 Output format : The program displays the following outputs based on the inputs provided: If the choice is 3: "Linked List: [values separated by space]" If the choice is 4: The program exits. If the choice is invalid: "Invalid choice"

#include <iostream> class Node { public: int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; class SortedLinkedList { private: Node* head; public: SortedLinkedList() : head(nullptr) {} void insertSorted(int value) { Node* newNode = new Node(value); if (!head || value <= head->data) { newNode->next = head; head = newNode; return; } Node* current = head; while (current->next && current->next->data < value) { current = current->next; } newNode->next = current->next; current->next = newNode; } void display() { Node* current = head; while (current) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } }; int main() { int n; std::cin >> n; SortedLinkedList sortedList; for (int i = 0; i < n; i++) { int value; std::cin >> value; sortedList.insertSorted(value); } int newValue; std::cin >> newValue; sortedList.insertSorted(newValue); sortedList.display(); return 0; } //awasthi

Lisa wants to create a linked list sorted in ascending order. She wants to insert nodes in such a way that the linked list remains sorted after insertion. Write a program that takes the number of nodes to be inserted, followed by their values in non-decreasing order. The program should then ask for a new value and insert a node with that value at the appropriate position to maintain the sorted order. Finally, the program should print the updated linked list. Example Input: 5 1 3 5 7 9 4 Output: 1 3 4 5 7 9

// You are using GCC #include <iostream> using namespace std; // Define the structure for a doubly linked list node struct Node { int data; Node* next; Node* prev; }; // Function to insert a new element at the front of the doubly linked list void insertFront(Node*& head, int data) { Node* newNode = new Node; newNode->data = data; newNode->next = head; newNode->prev = nullptr; if (head != nullptr) { head->prev = newNode; } head = newNode; } // Function to delete a node from the front of the doubly linked list void deleteFront(Node*& head) { if (head == nullptr) { return; // List is empty, nothing to delete } Node* temp = head; head = head->next; if (head != nullptr) { head->prev = nullptr; } delete temp; } // Function to delete a node from the end of the doubly linked list void deleteEnd(Node*& head) { if (head == nullptr) { return; // List is empty, nothing to delete } if (head->next == nullptr) { // Only one node in the list delete head; head = nullptr; } else { Node* current = head; while (current->next->next != nullptr) { current = current->next; } Node* temp = current->next; current->next = nullptr; delete temp; } } // Function to display the elements in the doubly linked list void displayList(Node* head) { Node* current = head; while (current != nullptr) { cout << current->data << " "; current = current->next; } cout << endl; } int main() { int n; cin >> n; Node* head = nullptr; for (int i = 0; i < n; i++) { int data; cin >> data; insertFront(head, data); } // Display the elements after inserting at the front displayList(head); // Delete a node from the front and display the updated list deleteFront(head); displayList(head); // Delete a node from the end and display the final list deleteEnd(head); displayList(head); return 0; } //awasthi

You are tasked with developing a program for a task management system that utilizes a doubly linked list to track and manage tasks. Each node in the doubly linked list represents a task with a unique ID. The program should allow for inserting tasks at the front, deleting tasks from the front and end of the list, and displaying the updated list of tasks after each operation. Implement a doubly linked list where elements can be inserted at the front and deleted from the front and end of the list. Note: This is a sample question asked in an Infosys interview. Input format : The first line contains an integer n, representing the number of elements to insert. The next line contains n space-separated integers representing the elements to be inserted at the front of the linked list.

// You are using GCC #include <iostream> #include <fstream> #include <unordered_set> using namespace std; // Define the structure for a doubly linked list node struct Node { string data; Node* next; Node* prev; }; // Function to insert a new session ID at the end of the doubly linked list void insert(Node*& head, string data) { Node* newNode = new Node; newNode->data = data; newNode->next = nullptr; if (head == nullptr) { newNode->prev = nullptr; head = newNode; } else { Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } newNode->prev = temp; temp->next = newNode; } } // Function to count unique user interactions using a hash set int countUniqueInteractions(Node* head) { unordered_set<string> uniqueInteractions; int count = 0; Node* current = head; while (current != nullptr) { if (uniqueInteractions.insert(current->data).second) { // Insertion into the set was successful, indicating a unique interaction count++; } current = current->next; } return count; } int main() { int n; cin >> n; Node* head = nullptr; // Read session IDs from the input and insert them into the doubly linked list for (int i = 0; i < n; i++) { string sessionID; cin >> sessionID; insert(head, sessionID); } // Calculate the number of unique user interactions int uniqueInteractions = countUniqueInteractions(head); // Output the result cout << "Number of unique user interactions: " << uniqueInteractions << endl; return 0; } //awasthi

You are developing a web analytics tool that tracks user engagement on a website. The tool requires functionality to determine the number of unique user interactions recorded in a log file. Each user interaction is represented by a unique session ID. Implement a program that allows the user to input a log file containing session IDs representing user interactions. The program should create a doubly linked list using the session IDs from the log file and calculate the total count of unique user interactions in the list. The program should read the log file, which consists of one session ID per line. It will then create a doubly linked list using the session IDs as the data. Next, the program will traverse the list and count the number of unique session IDs present. Finally, it will display the count as the output. The web analytics tool will provide valuable insights to website administrators by accurately determining the number of unique user interactions, enabling them to analyze user engagement and make data-driven decisions to enhance the user experience. Note: This is a sample question asked in an Amazon interview. Input format : The input begins with an integer N, representing the number of session IDs in the log file. This is followed by N lines of strings, each containing a session ID. Output format : The program should output a single line containing the number of unique user interactions.