Share linked list c++,

heavygorilla768

New member
Danh sách #Linked, #C ++, cấu trúc #data, #Programming, #algorithms ## Danh sách được liên kết trong C ++

Danh sách được liên kết là cấu trúc dữ liệu tuyến tính bao gồm một chuỗi các nút, mỗi nút chứa một phần tử dữ liệu và tham chiếu (hoặc con trỏ) đến nút tiếp theo trong danh sách.Danh sách được liên kết là một cấu trúc dữ liệu đa năng có thể được sử dụng để thực hiện nhiều cấu trúc dữ liệu và thuật toán khác nhau.

### Thực hiện danh sách được liên kết trong C ++

Mã sau đây cho thấy cách triển khai danh sách được liên kết trong C ++:

`` `C ++
#include <Istream>

sử dụng không gian tên STD;

// Định nghĩa nút
Nút cấu trúc {
dữ liệu int;
Nút* tiếp theo;
};

// Định nghĩa danh sách được liên kết
struct linkedList {
Nút* đầu;
Nút* đuôi;
};

// Tạo một danh sách được liên kết mới
LinkedList* crepelinkedList () {
LinkedList* list = new LinkedList ();
Danh sách-> head = null;
Danh sách-> đuôi = null;
danh sách trả lại;
}

// Chèn một nút mới ở đầu danh sách được liên kết
void insertNodeathead (linkedList* list, int data) {
Nút* newNode = new node ();
newnode-> data = data;
newnode-> next = list-> head;
Danh sách-> head = newNode;

if (list-> đuôi == null) {
Danh sách-> đuôi = newNode;
}
}

// Chèn một nút mới ở cuối danh sách được liên kết
void insertNodeatTail (linkedList* list, int data) {
Nút* newNode = new node ();
newnode-> data = data;
newnode-> next = null;

if (list-> head == null) {
Danh sách-> head = newNode;
} khác {
Danh sách-> đuôi-> next = newNode;
}

Danh sách-> đuôi = newNode;
}

// Xóa một nút khỏi danh sách được liên kết
void deletenode (linkedList* list, int data) {
Nút* currentNode = list-> head;
Nút* Trước đóNode = null;

// Tìm nút bị xóa
while (currentNode! = null && currentNode-> data! = data) {
trước đóNode = currentNode;
currentNode = currentNode-> tiếp theo;
}

// nếu tìm thấy nút, hãy xóa nó
if (currentNode! = null) {
if (currentNode == list-> head) {
Danh sách-> head = currentNode-> tiếp theo;
} if if (currentNode == list-> đuôi) {
Danh sách-> đuôi = trướcNode;
} khác {
Trước đó-> next = currentNode-> next;
}

Xóa hiện tạiNode;
}
}

// In nội dung của danh sách được liên kết
void printLinkedList (linkedList* list) {
Nút* currentNode = list-> head;

while (currentNode! = null) {
cout << currentNode-> data << "";
currentNode = currentNode-> tiếp theo;
}

cout << endl;
}

int main () {
// Tạo một danh sách được liên kết
LinkedList* list = createLinkedList ();

// Chèn một số nút vào danh sách được liên kết
ChènNodeathead (Danh sách, 10);
ChènNodeathead (Danh sách, 20);
ChènNodeattail (danh sách, 30);

// In nội dung của danh sách được liên kết
printLinkedList (danh sách);

// Xóa một nút khỏi danh sách được liên kết
Deletenode (Danh sách, 20);

// in lại nội dung của danh sách được liên kết
printLinkedList (danh sách);

trả lại 0;
}
`` `

### Ưu điểm và nhược điểm của danh sách được liên kết

Danh sách được liên kết có một số lợi thế so với các cấu trúc dữ liệu khác, chẳng hạn như mảng.Những lợi thế này bao gồm:

*** Tính linh hoạt: ** Danh sách được liên kết có thể dễ dàng sửa đổi và mở rộng.Các nút mới có thể được thêm vào
=======================================
#Linked List, #C++, #data Structure, #Programming, #algorithms ## Linked List in C++

A linked list is a linear data structure that consists of a sequence of nodes, each of which contains a data element and a reference (or pointer) to the next node in the list. Linked lists are a versatile data structure that can be used to implement a variety of different data structures and algorithms.

### Linked List Implementation in C++

The following code shows how to implement a linked list in C++:

```c++
#include <iostream>

using namespace std;

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

// Linked list definition
struct LinkedList {
Node* head;
Node* tail;
};

// Create a new linked list
LinkedList* createLinkedList() {
LinkedList* list = new LinkedList();
list->head = NULL;
list->tail = NULL;
return list;
}

// Insert a new node at the beginning of a linked list
void insertNodeAtHead(LinkedList* list, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = list->head;
list->head = newNode;

if (list->tail == NULL) {
list->tail = newNode;
}
}

// Insert a new node at the end of a linked list
void insertNodeAtTail(LinkedList* list, int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;

if (list->head == NULL) {
list->head = newNode;
} else {
list->tail->next = newNode;
}

list->tail = newNode;
}

// Delete a node from a linked list
void deleteNode(LinkedList* list, int data) {
Node* currentNode = list->head;
Node* previousNode = NULL;

// Find the node to be deleted
while (currentNode != NULL && currentNode->data != data) {
previousNode = currentNode;
currentNode = currentNode->next;
}

// If the node was found, delete it
if (currentNode != NULL) {
if (currentNode == list->head) {
list->head = currentNode->next;
} else if (currentNode == list->tail) {
list->tail = previousNode;
} else {
previousNode->next = currentNode->next;
}

delete currentNode;
}
}

// Print the contents of a linked list
void printLinkedList(LinkedList* list) {
Node* currentNode = list->head;

while (currentNode != NULL) {
cout << currentNode->data << " ";
currentNode = currentNode->next;
}

cout << endl;
}

int main() {
// Create a linked list
LinkedList* list = createLinkedList();

// Insert some nodes into the linked list
insertNodeAtHead(list, 10);
insertNodeAtHead(list, 20);
insertNodeAtTail(list, 30);

// Print the contents of the linked list
printLinkedList(list);

// Delete a node from the linked list
deleteNode(list, 20);

// Print the contents of the linked list again
printLinkedList(list);

return 0;
}
```

### Advantages and Disadvantages of Linked Lists

Linked lists have a number of advantages over other data structures, such as arrays. These advantages include:

* **Flexibility:** Linked lists can be easily modified and expanded. New nodes can be added to the
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top