Professional Documents
Culture Documents
1. Construct linked list L1 as follow: L1 = {9, 5, 3, 8, 7} by inserting new nodes to the front of the linked
list.
// Define the structure for a node in the linked list head = newNode;
struct Node { }
int data;
Node* next; // Function to display the linked list
}; void display() {
Node* current = head;
// Define the LinkedList class while (current != nullptr) {
class LinkedList { std::cout << current->data << " -> ";
private: current = current->next;
Node* head; }
public: std::cout << "nullptr" << std::endl;
// Constructor to initialize an empty linked list }
LinkedList() { };
head = nullptr;
} int main(){
LinkedList l;
// Function to insert a new node at the beginning of l.insertHead(3);
the linked list l.insertHead(4);
void insertHead(int value) { l.insertHead(5);
Node* newNode = new Node; l.display();
newNode->data = value; }.
newNode->next = head;
2. Implement method insert to add a new node with value ‘data’ at a given index ‘idx’.
Algorithm:
1. If the linked list is empty, return NULL.
2. If the node to be deleted is the head node, set the head node to the next node and
delete the original head node.
3. Otherwise, traverse the linked list from the head node until the node to be deleted is
found.
4. If the node to be deleted is not found, return NULL.
5. Otherwise, set the previous node’s next pointer to the node after the node to be
deleted (preNode->next = currNode->next).
6. Delete the node to be deleted.
7. Return the head node of the linked list.
void removeNode(int x) { prevNode->next = currNode->next;
Node* prevNode = NULL; delete currNode;
Node* currNode = head; }
else {//remove node at head
//move forward if not found head = currNode->next;
while (currNode && currNode->data != x) { delete currNode;
prevNode = currNode; }
currNode = currNode->next; }
} }
if (currNode) {//found node in the middle
if (prevNode) {
if (currNode)//found node
return currNode;
else
return nullptr;
}
while (currNode){
head = head->next;
delete currNode;
currNode = head;
}
}
class Node {
public:
int data;
Node* prev;
Node* next;
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() : head(nullptr), tail(nullptr) {}
// Function to insert a node at the end of the list
void insertFront(int val){
Node* newNode = new Node(val);
if (head == nullptr) {
head = newNode;
tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
void displayForward();
};
return 0;
}
Create a new class DoublyLinkedList and implement the corresponding method insertFront and
insertTail
Tham Khảo
Linked List C Code
=======================================================
#include <stdio.h>
#include <stdlib.h>
int main() {
// Initialize an empty linked list
struct Node* head = NULL;
return 0;
}
=======================================================
public:
// Constructor to initialize an empty linked list
LinkedList() {
head = nullptr;
}
// Function to delete a node with a specific value from the linked list
void deleteNode(int value) {
if (head == nullptr) {
return; // List is empty
}
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* current = head;
while (current->next != nullptr && current->next->data != value) {
current = current->next;
}
if (current->next != nullptr) {
Node* temp = current->next;
current->next = current->next->next;
delete temp;
}
}
int main() {
LinkedList myList;
myList.insertAtBeginning(3);
myList.insertAtBeginning(2);
myList.insertAtBeginning(1);
if (myList.search(2)) {
std::cout << "Value 2 found in the linked list." << std::endl;
} else {
std::cout << "Value 2 not found in the linked list." << std::endl;
}
myList.deleteNode(2);
return 0;