3 849
مشترکین
اطلاعاتی وجود ندارد24 ساعت
-297 روز
-12730 روز
آرشیو پست ها
3 849
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
Node* insertAtFront(Node* head, int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = head;
newNode->prev = nullptr;
if (head != nullptr) {
head->prev = newNode;
}
return newNode;
}
Node* deleteFromFront(Node* head) {
if (head == nullptr) {
return nullptr;
}
Node* newHead = head->next;
delete head;
if (newHead != nullptr) {
newHead->prev = nullptr;
}
return newHead;
}
Node* deleteFromEnd(Node* head) {
if (head == nullptr) {
return nullptr;
}
Node* current = head;
while (current->next != nullptr) {
current = current->next;
}
if (current->prev != nullptr) {
current->prev->next = nullptr;
} else {
head = nullptr;
}
delete current;
return head;
}
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 value;
cin >> value;
head = insertAtFront(head, value);
}
displayList(head);
head = deleteFromFront(head);
displayList(head);
head = deleteFromEnd(head);
displayList(head);
//awasthi
return 0;
}
3 849
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.
3 849
#include <iostream>
#include <string>
using namespace std;
struct Item {
int id;
string name;
int quantity;
double price;
Item* next;
Item* prev;
};
bool searchItem(Item* head, int searchId) {
Item* current = head;
while (current != nullptr) {
if (current->id == searchId) {
return true;
}
current = current->next;
}
return false;
}
int main() {
int n;
cin >> n;
Item* head = nullptr;
for (int i = 0; i < n; i++) {
int id;
cin >> id;
Item* newItem = new Item;
newItem->id = id;
cin.ignore();
getline(cin, newItem->name);
cin >> newItem->quantity >> newItem->price;
newItem->next = nullptr;
newItem->prev = nullptr;
if (head == nullptr) {
head = newItem;
} else {
Item* current = head;
while (current->next != nullptr) {
current = current->next;
}
current->next = newItem;
newItem->prev = current;
}
}
int searchId;
cin >> searchId;
if (searchItem(head, searchId)) {
cout << "Item with ID " << searchId << " is present in the list." << endl;
} else {
cout << "Item with ID " << searchId << " is not found in the list." << endl;
}
// Clean up memory
Item* current = head;
while (current != nullptr) {
Item* temp = current->next;
delete current;
current = temp;
}
return 0;
}
//awasthi
3 849
You are working on a system that manages the inventory of a store. Each item in the inventory has a unique ID and associated information such as name, quantity, and price. The system requires a feature to search for an item by its ID. Implement a program that allows the user to create a doubly linked list of item records and perform a search operation to find a specific item by its ID.
The program should prompt the user to enter the number of items and their respective IDs. It will then create a doubly linked list with the entered item IDs. Next, the program will ask the user to enter an item ID to search for. It will perform the search operation using the searchElement function and display whether the item with the entered ID is present in the list or not.
Note: This is a sample question asked in an Amcat interview.
Input format :
The first line contains an integer n, representing the number of items in the inventory.
The next n lines contain the details of an item in the following format:
.
The next line contains an integer searchId, representing the ID of the item to search for.
3 849
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node* next;
struct Node* prev;
};
void append(struct Node** head_ref, char data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return;
}
struct Node* last = *head_ref;
while (last->next != NULL) {
last = last->next;
}
last->next = new_node;
new_node->prev = last;
}
void rotate(struct Node** head_ref, int k) {
if (*head_ref == NULL || k == 0) {
return;
}
struct Node* current = *head_ref;
int count = 1;
while (count < k && current != NULL) {
current = current->next;
count++;
}
if (current == NULL) {
return;
}
struct Node* kth_node = current;
while (current->next != NULL) {
current = current->next;
}
current->next = *head_ref;
(*head_ref)->prev = current;
*head_ref = kth_node->next;
(*head_ref)->prev = NULL;
kth_node->next = NULL;
}
// Function to display the list
void display(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%c-->", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
int n;
scanf("%d", &n);
struct Node* parking_lot = NULL;
char vehicle;
for (int i = 0; i < n; i++) {
scanf(" %c", &vehicle);
append(&parking_lot, vehicle);
}
int k;
scanf("%d", &k);
printf("Current Parking Positions:\n");
display(parking_lot);
rotate(&parking_lot, k);
printf("Updated Parking Positions:\n");
display(parking_lot);
return 0;
}
//awasthi
3 849
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.
Code constraints :
3 849
#include <iostream>
struct Node {
int transactionID;
float transactionAmount;
Node* next;
};
struct CircularLinkedList {
Node* header;
CircularLinkedList() {
header = new Node;
header->next = header;
}
void insertRecord(int transactionID, float transactionAmount) {
Node* newNode = new Node;
newNode->transactionID = transactionID;
newNode->transactionAmount = transactionAmount;
// Find the position to insert the new node in sorted order
Node* current = header->next;
Node* prev = header;
while (current != header && current->transactionID < transactionID) {
prev = current;
current = current->next;
}
newNode->next = current;
prev->next = newNode;
}
bool deleteRecord(int index) {
Node* current = header;
int currentIndex = -1;
while (current->next != header && currentIndex < index - 1) {
current = current->next;
currentIndex++;
}
if (currentIndex != index - 1) {
std::cout << "Invalid index." << std::endl;
return false;
}
Node* deletedNode = current->next;
current->next = deletedNode->next;
delete deletedNode;
return true;
}
void displayRecords() {
Node* current = header->next;
int index = 0;
while (current != header) {
std::cout << "Index " << index++ << ": Transaction ID: " << current->transactionID << ", Amount: " << current->transactionAmount << std::endl;
current = current->next;
}
}
~CircularLinkedList() {
Node* current = header->next;
while (current != header) {
Node* temp = current;
current = current->next;
delete temp;
}
delete header;
}
};
int main() {
CircularLinkedList transactionList;
int n;
int transactionID;
float transactionAmount;
int indexToDelete;
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> transactionID >> transactionAmount;
transactionList.insertRecord(transactionID, transactionAmount);
}
std::cin >> indexToDelete;
if (transactionList.deleteRecord(indexToDelete)) {
std::cout << "Transaction record at index " << indexToDelete << " has been successfully deleted." << std::endl;
std::cout << "Updated transaction records:" << std::endl;
transactionList.displayRecords();
}
return 0;
}//awasthi
3 849
Problem Statement
You are developing a financial management application that tracks transaction records. Your task is to implement a feature that allows users to delete a specific transaction record from the circular header linked list. The application should handle this operation efficiently while maintaining the circular structure of the linked list.
Implement a program that provides the following functionality using a circular header linked list:
Insert a transaction record into the circular header linked list, where each record contains a unique transaction ID (positive integer) and a transaction amount (positive or negative floating-point number).
Delete a transaction record from the linked list based on a user-provided index. Display an appropriate message if the index is invalid.
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 initial transaction records in the circular header linked list.
The next N lines follow, each containing an integer representing the transaction ID and a floating-point number representing the transaction amount.
The last line consists of an integer K, representing the index of the transaction record to delete.
Output format :
If the provided index K is valid, display a message indicating the successful deletion of the transaction record at that index. (index starts from 0)
Display the updated transaction records, listing each record's index, transaction ID, and amount.
If the index K is invalid (less than 0 or greater than or equal to the number of transaction records), display an error message.
3 849
#include <iostream>
#include <string>
using namespace std;
struct Message {
string content;
Message* next;
};
// Function to create a new message node
Message* createMessageNode(const string& content) {
Message* newMessage = new Message();
newMessage->content = content;
newMessage->next = nullptr;
return newMessage;
}
// Function to insert a message at the beginning of the queue
Message* insertMessage(Message* head, const string& content) {
Message* newMessage = createMessageNode(content);
if (head == nullptr) {
newMessage->next = newMessage;
} else {
newMessage->next = head->next;
head->next = newMessage;
}
return newMessage;
}
// Function to reverse all elements inserted at the beginning of the queue
Message* reverseInsertedMessages(Message* head) {
if (head == nullptr || head->next == head) {
return head;
}
Message* current = head;
Message* prev = nullptr;
Message* next = nullptr;
do {
next = current->next;
current->next = prev;
prev = current;
current = next;
} while (current != head);
head->next = prev;
return head;
}
// Function to display the queue of messages
void displayMessages(Message* head) {
if (head == nullptr) {
cout << "Message queue is empty!" << endl;
return;
}
Message* temp = head;
cout << "Message Queue: " << endl;
do {
cout << "- " << temp->content << endl;
temp = temp->next;
} while (temp != head);
cout << endl;
}
int main() {
Message* head = nullptr;
int numMessages;
cin >> numMessages;
cin.ignore();
for (int i = 0; i < numMessages; i++) {
string content;
getline(cin, content);
head = insertMessage(head, content);
}
head = reverseInsertedMessages(head);
displayMessages(head);
return 0;
}
//awasthi
3 849
Imagine you are developing a program to manage a queue of incoming messages in a messaging application using a circular header linked list. Each node in the linked list represents a message, with the message content stored as data in the node.
You have implemented the functionality to insert new messages at the beginning of the queue and display the current queue of messages. Now, you want to enhance your program by allowing users to reverse the order of all messages that were inserted at the beginning of the queue.
Write a program that provides the functionality to reverse the order of such messages in the circular header linked list. Your code should display the original queue of messages after the reversal operation. The message content should be taken as input from the user. Consider handling cases such as an empty queue or a queue with a single message.
Note: This is a sample question asked in Capgemini recruitment.
3 849
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void append(Node** headRef, int data) {
Node* newNode = new Node();
newNode->data = data;
if (*headRef == nullptr) {
*headRef = newNode;
newNode->next = *headRef;
return;
}
Node* temp = *headRef;
while (temp->next != *headRef) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *headRef;
}
bool isSorted(Node* head) {
if (head == nullptr) {
return true; // An empty list is considered sorted
}
Node* current = head;
do {
if (current->data > current->next->data) {
return false; // Not sorted
}
current = current->next;
} while (current->next != head);
return true; // Sorted
}
int main() {
Node* head = nullptr;
int n, data;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> data;
append(&head, data);
}
if (isSorted(head)) {
cout << "The list is sorted in non-decreasing order." << endl;
} else {
cout << "The list is not sorted in non-decreasing order." << endl;
}//awasthi
return 0;
}
3 849
Milton is working on a project that involves managing data using a circular header-linked list. The circular header linked list is a special type of linked list where the last node points back to the head node. Each node in the list contains a value and a pointer to the next node.
To ensure the accuracy and reliability of the data, Milton needs to implement a program that checks if the circular header linked list is sorted in non-decreasing order. This program will help Milton identify if any elements are out of order and take appropriate actions to rectify the issue.
Write a program that Milton can use to check if the circular header linked list is sorted in non-decreasing order.
Note: This is a sample question asked in TCS recruitment.
Input format :
The first line represents the number of elements in the linked list n.
The next line represents the n elements in a linked list.
3 849
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertFront(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = newNode;
} else {
struct Node* last = (*head)->next;
while (last->next != (*head)) {
last = last->next;
}
last->next = newNode;
newNode->next = *head;
*head = newNode;
}
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = newNode;
} else {
struct Node* last = (*head)->next;
while (last->next != (*head)) {
last = last->next;
}
last->next = newNode;
newNode->next = *head;
}
}
int countNodes(struct Node* head) {
int count = 0;
if (head != NULL) {
struct Node* current = head;
do {
count++;
current = current->next;
} while (current != head);
}
return count;
}
void displayScores(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
int totalNodes = countNodes(head);
printf("Number of nodes in the CLL is %d\n", totalNodes);
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
int n, m, score;
struct Node* head = NULL;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &score);
insertFront(&head, score);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &score);
insertEnd(&head, score);
}
displayScores(head);
return 0;
}
//awasthi
3 849
You are developing a gaming application that requires a program to manage the scores of players. The program should implement a circular header linked list to store the player's scores. Each node in the linked list represents a player's score, and the list is circular to maintain a continuous loop of scores.
The program should provide the following functionalities:
Adding Scores: The program should allow adding a player's score at the front or end of the linked list.
Displaying Scores: The program should display the player's scores stored in the linked list, along with the total number of scores.
Your task is to develop a program that implements the circular header linked list and provides the mentioned functionalities. Ensure that the program correctly manages the scores and displays them accurately.
Note: This is a sample question asked in a Capgemini interview.
Input format :
The first line contains an integer, N, representing the number of player scores to be added at the front of the list.
The second line contains N space-separated integers, representing the player scores to be added at the front of the list.
The third line contains an integer, M, representing the number of player scores to be added at the end of the list.
The fourth line contains M space-separated integers, representing the player scores to be added at the end of the list.
Output format :
If the linked list is empty, output "List is empty."
Otherwise, output the total number of nodes in the circular header linked list, followed by the node values.
Refer to the sample output for formatting specifications.
Code constraints :
The player scores are integers.
The number of player scores to be added at the front and end of the list is non-negative.
Sample test cases :
Input 1 :
3
10 20 30
2
40 50
3 849
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = newNode;
} else {
struct Node* current = *head;
while (current->next != *head) {
current = current->next;
}
current->next = newNode;
newNode->next = *head;
}
}
void displayList(struct Node* head) {
if (head == NULL) {
printf("Linked List is empty.\n");
return;
}
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
int n, data;
struct Node* head = NULL;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &data);
insertEnd(&head, data);
}//awasthi
displayList(head);
return 0;
}
3 849
You are assigned the task of implementing a program that operates on a circular header linked list, a unique type of linked list in which the last node points back to the header node, forming a circular structure.
Your objective is to create a program that inserts elements at the end of the circular linked list and displays its contents.
Note: This is a sample question asked in TCS recruitment.
Input format :
The first line of input consists of an integer n, representing the size of the list.
The second line consists of n space-separated integers, representing the elements to be inserted at the end of the list.
Output format :
The output prints the elements of the circular header linked list, separated by space.
If no elements are inserted, print "Linked List is empty".
Sample test cases :
Input 1 :
5
1 2 3 4 5
Output 1 :
1 2 3 4 5
Input 2 :
0
Output 2 :
Linked List is empty.
3 849
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
typedef struct Node Node;
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
void insertEnd(Node** head, int value) {
Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void deleteOccurrences(Node** head, int value) {
Node* current = *head;
Node* prev = NULL;
while (current != NULL) {
if (current->data == value) {
Node* temp = current;
if (prev == NULL) {
*head = current->next;
} else {
prev->next = current->next;
}
current = current->next;
free(temp);
} else {
prev = current;
current = current->next;
}
}
}
void displayList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
int n, deleteValue;
scanf("%d", &n);
Node* head = NULL;
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertEnd(&head, value);
}
scanf("%d", &deleteValue);
printf("Original List: ");
displayList(head);
deleteOccurrences(&head, deleteValue);
printf("List after deleting all occurrences of %d: ", deleteValue);
displayList(head);
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}
//awasthi
3 849
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
typedef struct Node Node;
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
void insertEnd(Node** head, int value) {
Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
int findLength(Node* head) {
int count = 0;
Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
int main() {
int n;
scanf("%d", &n);
Node* head = NULL;
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertEnd(&head, value);
}
int length = findLength(head);
printf("%d\n", length);
//awasthi
while (head != NULL) {
Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}
3 849
You are developing a program for a school to manage student attendance records. As part of the attendance management functionality, you need to implement a program that deletes all occurrences of duplicate student IDs from a grounded header linked list.
In this scenario, the school maintains an attendance list represented as a grounded header-linked list. Each node in the list represents a student's attendance record and contains the student's ID as data. The first node serves as the header node and does not contain any actual attendance data. The subsequent nodes hold the student attendance records.
Note: This is a sample question asked in CTS 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 to delete.
3 849
Write a program to find the length of a grounded header linked list without using the "length" attribute. The function should traverse the list and count the number of nodes.
Note: This is a sample question asked in TCS recruitment.
Input format :
The first line represents the size of element n
The next line stores the n values in it.
Output format :
The output represents the count of the number of nodes.
