3 849
订阅者
无数据24 小时
-297 天
-12730 天
帖子存档
3 849
// 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 deleteEvenPositionNodes() {
if (!head) {
cout << "Empty Linked List" << endl;
return;
}
Node* current = head;
Node* prev = nullptr;
int count = 1;
while (current) {
if (count % 2 == 0) {
// Even position node, delete it
prev->next = current->next;
delete current;
current = prev->next;
} else {
prev = current;
current = current->next;
}
count++;
}
}
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.deleteEvenPositionNodes();
cout << "Final Linked List: ";
list.display();
return 0;
}
//awasthi
3 849
Elsa wants to delete nodes with even positions in a singly linked list. She needs your help to write a program that takes the size of the linked list and its elements as input and uses insertion at the end to add nodes to the linked list and delete nodes with even positions from the linked list. She wants to see the original linked list before deletion and the final linked list after the deletion process.
Write a program to solve Elsa's problem.
Note: This is a sample question asked in a TCS interview.
Input format :
The first line of the input consists of the size (n) of the linked list (an integer).
The second line of the input consists of the elements of the linked list (a sequence of space-separated integers).
Output format :
The output displays the original linked list before the deletion process.
The final linked list after deleting nodes with even positions.
3 849
// 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 deleteMiddleNode() {
if (!head) {
cout << "Empty Linked List" << endl;
return;
}
Node* slowPtr = head;
Node* fastPtr = head;
Node* prev = nullptr;
while (fastPtr && fastPtr->next) {
fastPtr = fastPtr->next->next;
prev = slowPtr;
slowPtr = slowPtr->next;
}
if (prev) {
prev->next = slowPtr->next;
delete slowPtr;
} else {
cout << "Linked List has only one node" << endl;
}
}
void display() {
Node* current = head;
//cout << "Original Linked List: ";
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
int numNodes;
cin >> numNodes;
LinkedList list;
for (int i = 0; i < numNodes; i++) {
int val;
cin >> val;
list.insert(val);
}
cout << "Original Linked List: ";
list.display();
list.deleteMiddleNode();
cout << "Updated Linked List: ";
list.display();
return 0;
}
//awasthi
3 849
Prackya wants to implement a function to delete the middle node of a singly linked list. She wants you to write a program that takes user input to construct a linked list, inserts nodes at the end, and then deletes the middle node if it exists. If the linked list has an even number of nodes, the function should delete the second middle node.
For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5.
For example, if the given linked list is 1->2->3->4->5->6, then it should be modified to 1->2->3->5->6.
Note: This is a sample question asked in an AMCAT interview.
Input format :
The first line of input consists of the number of nodes in the linked list (numNodes)
The second line of input consists of the values for each node, inserted at the end of the list.
3 849
// You are using GCC
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int value) : data(value), 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 printList() {
Node* current = head;
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) {
std::cout << "Linked List: ";
linkedList.printList();
} else if (choice == 4) {
break;
} else {
std::cout << "Invalid choice" << std::endl;
}
}
return 0;
}
//awasthi
3 849
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
3 849
#include <iostream>
using namespace std;
// A linked list node
struct Node {
int data;
Node* next;
};
// Given a reference (pointer to pointer) to the head of a list and an int, inserts a new node on the front of the list.
void insertAtFront(Node** head_ref, int new_data)
{
// 1. Allocate node
Node* new_node = new Node();
// 2. Put in the data
new_node->data = new_data;
// 3. Make next of new node as head
new_node->next = (*head_ref);
// 4. Move the head to point to the new node
(*head_ref) = new_node;
}
// Function to insert element in LL
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;
}
// This function prints contents of linked list starting from head
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << "\n";
}
int main()
{
// Start with the empty list
Node* head = NULL;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int data;
cin >> data;
push(&head, data);
}
cout << "Created Linked list: ";
printList(head);
//awasthi
// Insert element at the beginning
int newData;
cin >> newData;
insertAtFront(&head, newData);
cout <<"Final List: " ;
printList(head);
return 0;
}
3 849
Alice is learning about linked lists and wants to practice creating one by inserting elements at the front. She decides to write a program that allows her to do so.
The program will prompt Alice to enter the number of elements she wants to insert and then ask her to input the values of those elements. After receiving the inputs, the program will insert the elements at the front of the linked list and display the final linked list to Alice.
Alice is excited to see how her program will work and hopes it will help solidify her understanding of linked lists. She prepares herself to input the number of elements and their respective values, eagerly anticipating the final result.
Note: This is a sample question asked in a HCL interview.
Input format :
The first line of input consists of an integer n, representing the number of elements to be inserted.
The second line of input consists of n space-separated integers, representing the elements to be inserted in the linked list.
The third line of input consists of an integer representing the value to be inserted at the front of the linked list.
3 849
// You are using GCC
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node(int value) : data(value),next(nullptr){}
};
class LinkedList{
private:
Node* head;
public:
LinkedList() : head(nullptr){}
void insertAtPosition(int position,int value)
{
Node* newNode = new Node(value);
if(position == 1){
newNode->next = head;
head = newNode;
return;
}
Node* current = head;
for(int i=1;i<position-1 && current !=nullptr;++i){
current = current->next;
}
if (current == nullptr){
cout<<"Invalid position"<<endl;
delete newNode;
return;
}
newNode->next = current->next;
current->next = newNode;
}
void printList(){
Node* current = head;
while(current){
cout<<current->data<<" ";
current = current->next;
}
cout<<endl;
}
};
int main()
{
int n;
cin>>n;
LinkedList linkedList;
for(int i=0;i<n;++i)
{
int value;
cin>>value;
linkedList.insertAtPosition(i+1,value);
}
int position,newValue;
cin>>position>>newValue;
linkedList.insertAtPosition(position,newValue);
linkedList.printList();
}//awasthi
3 849
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.
3 849
#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;
}
3 849
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
3 849
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void append(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 deleteEvenPositionNodes() {
if (!head) {
return;
}
Node* current = head;
Node* prev = nullptr;
bool deleteNext = false;
while (current) {
if (deleteNext) {
prev->next = current->next;
delete current;
current = prev->next;
deleteNext = false;
} else {
prev = current;
current = current->next;
deleteNext = true;
}
}
}
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;
LinkedList linkedList;
for (int i = 0; i < n; ++i) {
int element;
std::cin >> element;
linkedList.append(element);
}
std::cout << "Original Linked List: ";
linkedList.display();
linkedList.deleteEvenPositionNodes();
std::cout << "Final Linked List: ";
linkedList.display();
return 0;
}
//awasthi
3 849
Elsa wants to delete nodes with even positions in a singly linked list. She needs your help to write a program that takes the size of the linked list and its elements as input and uses insertion at the end to add nodes to the linked list and delete nodes with even positions from the linked list. She wants to see the original linked list before deletion and the final linked list after the deletion process.
Write a program to solve Elsa's problem.
Note: This is a sample question asked in a TCS interview.
Input format :
The first line of the input consists of the size (n) of the linked list (an integer).
The second line of the input consists of the elements of the linked list (a sequence of space-separated integers).
Output format :
The output displays the original linked list before the deletion process.
The final linked list after deleting nodes with even positions.
3 849
#include <iostream>
class Node {
public:
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void append(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 deleteFirstNode() {
if (head) {
Node* temp = head;
head = head->next;
delete temp;
}
}
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;
LinkedList studentRecords;
for (int i = 0; i < n; ++i) {
int studentID;
std::cin >> studentID;
studentRecords.append(studentID);
}
studentRecords.deleteFirstNode();
studentRecords.display();
return 0;
}
//awasthi
3 849
You are tasked with developing a program to manage a student records system using a singly linked list. Each node in the linked list represents a student and contains their unique ID. The program should allow for creating a list of student records, deleting the first student's record, and printing the remaining student IDs.
Write a program to implement the student records system according to the following specifications:
The program should prompt the user to enter the number of students n for which records need to be created.
For each student, the program should prompt the user to enter their student ID (an integer value).
The program should create a singly linked list with n nodes, where each node represents a student and contains their student ID.
After creating the linked list, the program should delete the first node from the list.
Finally, the program should traverse the modified linked list and print the student IDs of the remaining students.
Note: This is a sample question asked in a Capgemini interview.
Input format :
The first line of input consists of an integer n, representing the number of nodes in the linked list.
The second line of input consists of n space-separated integers, representing the values of each node in the linked list.
Output format :
The output prints the space-separated values of the linked list after deleting the first node.
3 849
#include <iostream>
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void append(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 display() {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
void deleteSecondToLastNode() {
if (!head !head->next !head->next->next) {
std::cout << "List has less than two nodes. Cannot delete second-to-last node." << std::endl;
return;
}
Node* current = head;
while (current->next->next->next) {
current = current->next;
}
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
};
int main() {
int size;
std::cin >> size;
LinkedList linkedList;
for (int i = 0; i < size; ++i) {
int element;
std::cin >> element;
linkedList.append(element);
}
std::cout << "Original Linked List: ";
linkedList.display();
linkedList.deleteSecondToLastNode();
std::cout << "Updated Linked List: ";
linkedList.display();
return 0;
}
//awasthi
3 849
Vennila is a student. She is learning data structure and a singly linked list. She wants to write a program to delete the second-to-last node of the linked list and also implement a program that deletes the second-to-last node of a singly linked list. Define a struct Node with two members: data to store the integer value and next to store the pointer to the next node in the list.
Note: This is a sample question asked in a mPhasis interview.
Input format :
The input consists of the following:
The first line contains an integer size, representing the number of elements in the linked list.
The second line contains arr space-separated integers, representing the elements of the linked list, and inserts nodes at the end.
Output format :
The output consists of the following:
The first line should display the elements of the original linked list.
The second line should display the elements of the linked list after deleting the second-to-last node.
3 849
#include <iostream>
#include <string>
class Node {
public:
std::string data;
Node* next;
Node(const std::string& data) : data(data), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void append(const std::string& 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 display() {
Node* current = head;
while (current) {
std::cout << current->data << " ";
current = current->next;
}
}
void deleteAlternateNodes() {
if (!head) {
std::cout << "List is empty" << std::endl;
return;
}
Node* current = head;
while (current && current->next) {
Node* temp = current->next;
current->next = temp->next;
delete temp;
current = current->next;
}
}
};
int main() {
int n;
std::cin >> n;
LinkedList linkedList;
for (int i = 0; i < n; ++i) {
std::string element;
std::cin >> element;
linkedList.append(element);
}
if (n == 0) {
std::cout << "List is empty" << std::endl;
} else {
std::cout << "Linked list data: ";
linkedList.display();
std::cout << std::endl;
linkedList.deleteAlternateNodes();
std::cout << "After deleting alternate node: ";
linkedList.display();
std::cout << std::endl;
}
return 0;
}
//awasthi
3 849
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.
