uk
Feedback
inactive

inactive

Закритий канал

...

Показати більше
3 849
Підписники
Немає даних24 години
-297 днів
-12730 днів
Архів дописів
#include <iostream> struct Node { std::string data; Node* prev; Node* next; }; void insertAtEnd(Node** head, const std::string& newData) { Node* newNode = new Node; newNode->data = newData; newNode->prev = nullptr; newNode->next = nullptr; if (*head == nullptr) { *head = newNode; } else { Node* current = *head; while (current->next != nullptr) { current = current->next; } current->next = newNode; newNode->prev = current; } } void searchAndReplace(Node* head, const std::string& searchData, const std::string& newValue) { Node* current = head; while (current != nullptr) { if (current->data == searchData) { current->data = newValue; } current = current->next; } } void printList(Node* head) { Node* current = head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { Node* head = nullptr; int n; std::string searchData, newValue; std::cin >> n; // Create the doubly linked list for (int i = 0; i < n; i++) { std::string data; std::cin >> data; insertAtEnd(&head, data); } std::cin >> searchData >> newValue; // Search and replace element in the list searchAndReplace(head, searchData, newValue); std::cout << "Modified List: "; printList(head); return 0; }//awasthi

Implement a software tool that analyzes customer feedback for a product. The tool requires a feature to search for a specific feedback entry in a linked list and replace it with an updated version. Each feedback entry is represented by a unique identifier. Implement a program that allows the user to create a doubly linked list of feedback entries with insertion at the end and perform a search operation to find a specific entry. If the entry is found, the program should replace it with an updated version provided by the user. Finally, the program should display the modified list of feedback entries. The doubly linked list is implemented as a list with insertion at the end. New entries are appended to the end of the list. The search operation will start from the beginning of the list and traverse forward until the entry is found. Note: This is a sample question asked in a Paypal interview. Input format : The first line contains an integer, N, representing the number of feedback entries. N lines follow, each containing a string representing a feedback entry identifier. The (N+1)-th line contains a string, searchId, representing the feedback entry identifier to search for, and a string, updatedId, representing the updated feedback entry identifier. Output format : The program should output a single line displaying the modified list of feedback entries, separated by a space.

#include <iostream> using namespace std; // Define a node structure for the doubly-linked list struct Node { char data; // store the vehicle ID Node* next; // pointer to the next node Node* prev; // pointer to the previous node }; // Define a class for the parking system class ParkingSystem { private: Node* head; // pointer to the head of the list Node* tail; // pointer to the tail of the list int size; // store the number of vehicles in the list public: // Constructor to initialize the list ParkingSystem() { head = NULL; tail = NULL; size = 0; } // Destructor to delete the list ~ParkingSystem() { Node* temp = head; while (temp != NULL) { Node* next = temp->next; delete temp; temp = next; } } // Method to insert a vehicle at the end of the list void insert(char data) { Node* newNode = new Node(); // create a new node newNode->data = data; // assign the vehicle ID newNode->next = NULL; // set the next pointer to NULL newNode->prev = tail; // set the previous pointer to the current tail if (head == NULL) { // if the list is empty head = newNode; // set the head to the new node } else { // if the list is not empty tail->next = newNode; // set the next pointer of the current tail to the new node } tail = newNode; // set the tail to the new node size++; // increment the size of the list } // Method to rotate the list counter-clockwise by k positions void rotate(int k) { if (head == NULL k == 0 k == size) { // if the list is empty or k is zero or equal to size, no rotation is needed return; } k = k % size; // if k is larger than size, take the modulo of k and size Node* current = head; // start from the head of the list int count = 1; // initialize a counter variable while (count < k && current != NULL) { // loop until count reaches k or current reaches NULL current = current->next; // move current to the next node count++; // increment count } if (current == NULL) { // if current is NULL, no rotation is needed return; } Node* newHead = current->next; // set the new head to be the next node of current newHead->prev = NULL; // set the previous pointer of new head to NULL current->next = NULL; // set the next pointer of current to NULL tail->next = head; // set the next pointer of tail to head head->prev = tail; // set the previous pointer of head to tail head = newHead; // update head to new head tail = current; // update tail to current } // Method to print the list from head to tail void print() { Node* temp = head; while (temp != NULL) { cout << temp->data << "-->"; temp = temp->next; } cout << "NULL" << endl; } }; // Main function to test the code int main() { int n, k; // declare variables for number of vehicles and rotation number cin >> n; // read n from input ParkingSystem ps; // create an object of ParkingSystem class for (int i = 0; i < n; i++) { char c; cin >> c; ps.insert(c); } cin >> k; cout << "Current Parking Positions: "; ps.print(); ps.rotate(k); cout << "Updated Parking Positions: "; ps.print(); return 0; } //awasthi

Implement a vehicle parking management system that supports rotating the parking positions of the vehicles using a doubly-linked list. Given the current parking positions of the vehicles and a rotation number, update the parking positions by rotating the vehicles counter-clockwise. Note: This is a sample question asked in the Amazon recruitment. Input format : The input consists of the following: The number of vehicles parked in the parking lot, n (integer). n characters representing the unique identification of each vehicle. The rotation number, k (integer). Output format : The output consists of the following:

// 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. Output format : The output consists of three parts: After inserting all the elements at the front, display the elements in the linked list. After deleting the node at the front, display the updated linked list. After deleting the node at the end, display the final 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.

#include <iostream> struct Node { std::string data; Node* prev; Node* next; }; void insertAtEnd(Node** head, const std::string& newData) { Node* newNode = new Node; newNode->data = newData; newNode->prev = nullptr; newNode->next = nullptr; if (*head == nullptr) { *head = newNode; } else { Node* current = *head; while (current->next != nullptr) { current = current->next; } current->next = newNode; newNode->prev = current; } } void searchAndReplace(Node* head, const std::string& searchData, const std::string& newValue) { Node* current = head; while (current != nullptr) { if (current->data == searchData) { current->data = newValue; } current = current->next; } } void printList(Node* head) { Node* current = head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } int main() { Node* head = nullptr; int n; std::string searchData, newValue; std::cin >> n; // Create the doubly linked list for (int i = 0; i < n; i++) { std::string data; std::cin >> data; insertAtEnd(&head, data); } std::cin >> searchData >> newValue; // Search and replace element in the list searchAndReplace(head, searchData, newValue); std::cout << "Modified List: "; printList(head); return 0; }//awasthi

Implement a software tool that analyzes customer feedback for a product. The tool requires a feature to search for a specific feedback entry in a linked list and replace it with an updated version. Each feedback entry is represented by a unique identifier. Implement a program that allows the user to create a doubly linked list of feedback entries with insertion at the end and perform a search operation to find a specific entry. If the entry is found, the program should replace it with an updated version provided by the user. Finally, the program should display the modified list of feedback entries. The doubly linked list is implemented as a list with insertion at the end. New entries are appended to the end of the list. The search operation will start from the beginning of the list and traverse forward until the entry is found. Note: This is a sample question asked in a Paypal interview. Input format : The first line contains an integer, N, representing the number of feedback entries. N lines follow, each containing a string representing a feedback entry identifier. The (N+1)-th line contains a string, searchId, representing the feedback entry identifier to search for, and a string, updatedId, representing the updated feedback entry identifier. Output format : The program should output a single line displaying the modified list of feedback entries, separated by a space.

#include <iostream> using namespace std; // Define a node structure for the doubly-linked list struct Node { char data; // store the vehicle ID Node* next; // pointer to the next node Node* prev; // pointer to the previous node }; // Define a class for the parking system class ParkingSystem { private: Node* head; // pointer to the head of the list Node* tail; // pointer to the tail of the list int size; // store the number of vehicles in the list public: // Constructor to initialize the list ParkingSystem() { head = NULL; tail = NULL; size = 0; } // Destructor to delete the list ~ParkingSystem() { Node* temp = head; while (temp != NULL) { Node* next = temp->next; delete temp; temp = next; } } // Method to insert a vehicle at the end of the list void insert(char data) { Node* newNode = new Node(); // create a new node newNode->data = data; // assign the vehicle ID newNode->next = NULL; // set the next pointer to NULL newNode->prev = tail; // set the previous pointer to the current tail if (head == NULL) { // if the list is empty head = newNode; // set the head to the new node } else { // if the list is not empty tail->next = newNode; // set the next pointer of the current tail to the new node } tail = newNode; // set the tail to the new node size++; // increment the size of the list } // Method to rotate the list counter-clockwise by k positions void rotate(int k) { if (head == NULL k == 0 k == size) { // if the list is empty or k is zero or equal to size, no rotation is needed return; } k = k % size; // if k is larger than size, take the modulo of k and size Node* current = head; // start from the head of the list int count = 1; // initialize a counter variable while (count < k && current != NULL) { // loop until count reaches k or current reaches NULL current = current->next; // move current to the next node count++; // increment count } if (current == NULL) { // if current is NULL, no rotation is needed return; } Node* newHead = current->next; // set the new head to be the next node of current newHead->prev = NULL; // set the previous pointer of new head to NULL current->next = NULL; // set the next pointer of current to NULL tail->next = head; // set the next pointer of tail to head head->prev = tail; // set the previous pointer of head to tail head = newHead; // update head to new head tail = current; // update tail to current } // Method to print the list from head to tail void print() { Node* temp = head; while (temp != NULL) { cout << temp->data << "-->"; temp = temp->next; } cout << "NULL" << endl; } }; // Main function to test the code int main() { int n, k; // declare variables for number of vehicles and rotation number cin >> n; // read n from input ParkingSystem ps; // create an object of ParkingSystem class for (int i = 0; i < n; i++) { char c; cin >> c; ps.insert(c); } cin >> k; cout << "Current Parking Positions: "; ps.print(); ps.rotate(k); cout << "Updated Parking Positions: "; ps.print(); return 0; } //awasthi

Implement a vehicle parking management system that supports rotating the parking positions of the vehicles using a doubly-linked list. Given the current parking positions of the vehicles and a rotation number, update the parking positions by rotating the vehicles counter-clockwise. Note: This is a sample question asked in the Amazon recruitment. Input format : The input consists of the following: The number of vehicles parked in the parking lot, n (integer). n characters representing the unique identification of each vehicle. The rotation number, k (integer). Output format : The output consists of the following: The current parking positions of the vehicles before rotation. The updated parking positions of the vehicles after rotating them counter-clockwise by k positions.

#include <iostream> using namespace std; // Define the structure for a circular linked list node struct Node { int data; Node* next; }; // Function to insert a new node at the end of the circular linked list void insert(Node*& head, int data) { Node* newNode = new Node; newNode->data = data; newNode->next = head; if (head == nullptr) { newNode->next = newNode; // Make it point to itself if it's the first node head = newNode; } else { Node* temp = head; while (temp->next != head) { temp = temp->next; } temp->next = newNode; } } // Function to perform pairwise swapping of adjacent elements void pairwiseSwap(Node*& head) { if (head == nullptr || head->next == head) { return; // Empty list or only one element, nothing to swap } Node* curr = head; do { swap(curr->data, curr->next->data); // Swap adjacent elements curr = curr->next->next; } while (curr != head); } // Function to print the circular linked list void printList(Node* head) { if (head == nullptr) { return; } Node* temp = head; do { cout << temp->data << " "; temp = temp->next; } while (temp != head); cout << endl; } int main() { int n; cin >> n; Node* head = nullptr; for (int i = 0; i < n; i++) { int data; cin >> data; insert(head, data); } cout << "Original linked list: "; printList(head); pairwiseSwap(head); cout << "Linked list after pairwise swapping: "; printList(head); return 0; } //awasthi

#include <iostream> using namespace std; // Structure for a seat node struct SeatNode { int seatNumber; SeatNode* next; SeatNode(int number) { seatNumber = number; next = nullptr; } }; // Circular header-linked list for managing seats class SeatList { private: SeatNode* head; int size; public: SeatList() { head = nullptr; size = 0; } // Function to insert a new seat at the front of the list void insertFront(int seatNumber) { SeatNode* newNode = new SeatNode(seatNumber); if (head == nullptr) { head = newNode; head->next = head; // Circular link } else { newNode->next = head->next; head->next = newNode; } size++; } // Function to insert a new seat at the specified position void insertAtPosition(int seatNumber, int position) { if (position < 1 || position > size + 1) { cout << "Invalid position." << endl; return; } SeatNode* newNode = new SeatNode(seatNumber); if (position == 1) { newNode->next = head->next; head->next = newNode; head = newNode; } else { SeatNode* current = head; for (int i = 1; i < position - 1; i++) { current = current->next; } newNode->next = current->next; current->next = newNode; } size++; } // Function to insert a new seat at the middle position (floor value) void insertAtMiddle(int seatNumber) { int middle = (size + 1) / 2; insertAtPosition(seatNumber, middle); } // Function to display the total number of available seats and print the circular list void displaySeats() { if (size == 0) { cout << "No available seats." << endl; return; } cout << "After inserting at middle:" << endl; SeatNode* current = head; do { cout << current->seatNumber << " "; current = current->next; } while (current != head); cout << endl; } }; int main() { SeatList seatList; int n; cin >> n; for (int i = 0; i < n; i++) { int seatNumber; cin >> seatNumber; seatList.insertFront(seatNumber); } int middleSeat; cin >> middleSeat; seatList.insertAtMiddle(middleSeat); int position, newSeat; cin >> position >> newSeat; seatList.insertAtPosition(newSeat, position); seatList.displaySeats(); return 0; } //awasthi

You have been assigned to develop a program for a ticketing system at a concert venue. The system needs to maintain a circular header-linked list to manage the available seats in the venue. Each seat is represented by a node in the list, containing the seat number (an integer) and a link to the next seat. The program should provide the following functionality: Insertion of a new seat at the front of the list, representing a newly available seat. Insertion of a new seat at a specified position in the list, representing a reserved seat. Insertion of a new seat at the middle position (take the floor value) of the list when additional seats become available due to a change in the seating arrangement. Displaying the total number of available seats in the venue and printing the seat numbers in a circular manner, starting from the head seat. Note: This is a sample question asked in a Capgemini interview. Input format : The first line contains an integer n representing the number of seats to be inserted at the front of the list. The next line contains n seat numbers to be inserted at the front of the list. The next line contains an integer representing the seat number to be inserted at the middle position. The next line contains an integer position representing the position at which a seat needs to be inserted. The next line contains an integer representing the seat number to be inserted at the specified position. Output format : If the list is empty or an invalid position is given, the program outputs "Invalid position.". The program outputs "After inserting at middle:" followed by the number and list of seat numbers in a circular manner. Take the floor value while inserting it in the middle. After inserting a seat at a specified position, the program outputs "After inserting at position:" followed by the number and list of seat numbers in a circular manner.

#include <iostream> #include <string> using namespace std; // Definition for singly-linked list struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; // Function to insert a node at the end of the linked list void insert(ListNode*& head, int val) { ListNode* newNode = new ListNode(val); if (!head) { head = newNode; head->next = head; // Make it a circular linked list } else { ListNode* current = head; while (current->next != head) { current = current->next; } current->next = newNode; newNode->next = head; } } // Function to check if a circular header linked list is a palindrome bool isPalindrome(ListNode* head) { if (!head) return true; string forward, backward; ListNode* current = head; // Traverse the linked list in forward direction and create a string do { forward += to_string(current->val); current = current->next; } while (current != head); current = head; // Traverse the linked list in backward direction and create a string do { backward = to_string(current->val) + backward; current = current->next; } while (current != head); // Check if the forward and backward strings are the same return forward == backward; } int main() { int n; cin >> n; ListNode* head = nullptr; for (int i = 0; i < n; i++) { int val; cin >> val; insert(head, val); } if (isPalindrome(head)) { cout << "Linked list is a palindrome." << endl; } else { cout << "Linked list is not a palindrome." << endl; } return 0; }//awasthi

Alice is a detective investigating a mysterious case involving a secret code. She discovered a circular header linked list containing characters. Alice believes that this linked list might be a palindrome, meaning it reads the same forwards and backward. To validate her hypothesis, Alice needs your help to write a program that checks whether the circular header linked list is indeed a palindrome when considering the entire list in both forward and backward directions. Write a program that takes the input of a circular header linked list and determines if it is a palindrome. The program should output "Palindrome" if the linked list is a palindrome and "Not Palindrome" otherwise. Note: This is a sample question asked in Capgemini recruitment. Input format : The first line represents the number of elements in the linked list n. The next n line represents the elements in a linked list. Output format : The output represents the palindrome or not.

// You are using GCC #include <iostream> using namespace std; // Define the structure for a node in the linked list struct Node { float data; Node* next; }; // Function to insert a new node at the end of the linked list void insertEnd(Node*& head, float value) { Node* newNode = new Node; newNode->data = value; newNode->next = nullptr; if (head == nullptr) { // If the list is empty, make the new node the head and point to itself head = newNode; newNode->next = head; } else { // Otherwise, traverse to the end of the list and insert the new node Node* current = head; while (current->next != head) { current = current->next; } current->next = newNode; newNode->next = head; } } // Function to display the linked list void displayList(Node* head) { if (head == nullptr) { cout << "Linked List is empty." << endl; return; } Node* current = head; do { cout << current->data << " "; current = current->next; } while (current != head); cout << endl; } int main() { int n; cin >> n; Node* head = nullptr; for (int i = 0; i < n; i++) { float value; cin >> value; insertEnd(head, value); } displayList(head); return 0; }//awasthi

A scientific research project requires storing and managing a collection of experimental data. The data consists of measured values recorded as floating-point numbers. The project team decides to use a Circular Header Linked List to store and organize the data efficiently. Write a program for inserting a new experimental data value (float) at the end of the list. Note: This is a sample question asked in TCS recruitment. Input format : The first line represents the size of the list n The next n lines represent the float elements inside the list.

// You are using GCC #include <iostream> using namespace std; class Node { public: char data; Node* next; }; class CircularHeaderLinkedList { private: Node* head; public: CircularHeaderLinkedList() { head = nullptr; } void insertEnd(char value) { Node* newNode = new Node(); newNode->data = value; if (head == nullptr) { head = newNode; newNode->next = head; } else { Node* temp = head; while (temp->next != head) { temp = temp->next; } temp->next = newNode; newNode->next = head; } } void displayList() { if (head == nullptr) { cout << "Linked List is empty." << endl; return; } Node* current = head; do { cout << current->data << " "; current = current->next; } while (current != head); cout << endl; } }; int main() { CircularHeaderLinkedList list; int n; cin >> n; for (int i = 0; i < n; i++) { char element; cin >> element; list.insertEnd(element); } list.displayList(); return 0; }//awasthi