You are on page 1of 9

Lab01- Linked List

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’.

e.g. // L2 = {1, 3, 6, 2}, L2.insert(3, 4) // data = 4, idx = 3


// L2 = {1, 3, 4, 6, 2}.

GV: Nguyễn Mạnh Cương, Nha Trang University 1


//insert an element
void insert(int idx, int value) {
Node* current = head;
while ((current != nullptr) && (current->data !=idx))
current = current->next;
if(current){//found
Node *newNode = new Node;
newNode->data = value;
newNode->next = current->next;
current->next= newNode;
}
}

3. Implement method remove a node with the value ‘data’

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) {

4. Implement method search to find a node with value ‘data’.


e.g. // L3 = {1, 3, 2, 5, 6}
Node *target = L3.search(5), // target->data is 5
Node* search(int x) {
Node* currNode = head;

GV: Nguyễn Mạnh Cương, Nha Trang University 2


//move forward if not found
while (currNode && currNode->data != x)
currNode = currNode->next;

if (currNode)//found node
return currNode;
else
return nullptr;
}

5. Implement method remove to delete ALL nodes with value ‘data’.


e.g. // L4 = {1, 3, 2, 5, 6}
L4.remove(3), // L4 = {1, 2, 5, 6}
void removeAll(){
Node* currNode = head;

while (currNode){
head = head->next;
delete currNode;
currNode = head;
}
}

6. Implement method extend to join two linked list.


e.g. // L6a = {1, 4, 7}, // L6b = {9, 6, 5}
L6a.extend(L6b) // L6a = {1, 4, 7, 9, 6, 5}
Node* headNode(){
return head;
}

void extend(LinkedList l2){


Node* head2 = l2.headNode();
if(head == nullptr) head = head2;
else{
Node *pTail = head;
//run all the way to the last node
while(pTail->next!=nullptr) pTail = pTail->next;
//connext pTail to head2
pTail->next = head2;
}
}

7. Reverse a linked list


e.g. // L7 = {1, 4, 7}, L7.reverse() // L7 = {7, 1, 4}
void reverse(){
Node* currNode = head;
Node* newNode= nullptr;
head = nullptr;
while (currNode){
newNode = currNode;
currNode = currNode->next;

GV: Nguyễn Mạnh Cương, Nha Trang University 3


newNode->next = head;
head = newNode;
}
}

8. The following struct is used to form DoublyLinkedList

class Node {
public:
int data;
Node* prev;
Node* next;

Node(int val) : data(val), prev(nullptr), next(nullptr) {}


};

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();
};

GV: Nguyễn Mạnh Cương, Nha Trang University 4


int main() {
DoublyLinkedList dll;
dll.append(1);
dll.append(2);
dll.append(3);
dll.prepend(0);
std::cout << "Doubly Linked List (forward): ";
dll.displayForward();

std::cout << "Doubly Linked List (backward): ";


dll.displayBackward();

return 0;
}

Create a new class DoublyLinkedList and implement the corresponding method insertFront and
insertTail

9. Extra work: Implement method remove a node for DoublyLinkedList.

Tham Khảo
Linked List C Code
=======================================================
#include <stdio.h>
#include <stdlib.h>

GV: Nguyễn Mạnh Cương, Nha Trang University 5


// Define the structure for a node in the linked list
struct Node {
int data;
struct Node* next;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Function to insert a new node at the beginning of the linked list


void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
}

// Function to print the linked list


void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

// Function to free the memory used by the linked list


void freeList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
struct Node* temp = current;
current = current->next;
free(temp);
}
}

int main() {
// Initialize an empty linked list
struct Node* head = NULL;

// Insert elements at the beginning of the linked list


insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);

// Print the linked list


printf("Linked List: ");
printList(head);

GV: Nguyễn Mạnh Cương, Nha Trang University 6


// Free the memory used by the linked list
freeList(head);

return 0;
}
=======================================================

Linked list C++ code


#include <iostream>

// Define the structure for a node in the linked list


struct Node {
int data;
Node* next;
};

// Define the LinkedList class


class LinkedList {
private:
Node* head;

public:
// Constructor to initialize an empty linked list
LinkedList() {
head = nullptr;
}

// Function to insert a new node at the beginning of the linked list


void insertAtBeginning(int value) {
Node* newNode = new Node;
newNode->data = value;
newNode->next = head;
head = newNode;
}

// Function to display the linked list


void display() {
Node* current = head;
while (current != nullptr) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "nullptr" << std::endl;
}

// Function to search for a value in the linked list


bool search(int value) {
Node* current = head;
while (current != nullptr) {
if (current->data == value) {
return true; // Value found
}
current = current->next;
}
return false; // Value not found

GV: Nguyễn Mạnh Cương, Nha Trang University 7


}

// 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;
}
}

// Destructor to free memory when the linked list is destroyed


~LinkedList() {
Node* current = head;
while (current != nullptr) {
Node* temp = current;
current = current->next;
delete temp;
}
}
};

int main() {
LinkedList myList;

myList.insertAtBeginning(3);
myList.insertAtBeginning(2);
myList.insertAtBeginning(1);

std::cout << "Linked List: ";


myList.display();

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);

std::cout << "Linked List after deleting 2: ";


myList.display();

return 0;

GV: Nguyễn Mạnh Cương, Nha Trang University 8


}

GV: Nguyễn Mạnh Cương, Nha Trang University 9

You might also like