Hvad er en sammenkædet liste?
En sammenkædet liste er en lineær datastruktur, der består af en sekvens af noder, hvor hver node gemmer et stykke data og en reference (pointer) til den næste node i listen. Sammenkædede lister er nyttige til lagring af datasamlinger, når samlingens størrelse ikke er kendt på forhånd, eller når dataene ofte indsættes eller slettes.
I C++ kan vi oprette en linket liste ved at definere en nodeklasse og en linket listeklasse. Nodeklassen vil repræsentere en enkelt node i listen og indeholde et datafelt og en pointer til den næste node. Den sammenkædede listeklasse vil indeholde en hovedpointer til den første knude på listen og forskellige metoder til at indsætte, slette og krydse knudepunkterne på listen.
Her er et eksempel på en nodeklasse i C++:
class Node { public: int data; Node *next; Node(int data) { this->data = data; this->next = nullptr; } };
Denne nodeklasse har offentlige datafeltdata af typen int og en offentlig pointer ved siden af den næste node på listen. Den har også en konstruktør, der initialiserer datafeltet og sætter den næste pointer til nullptr.
Her er et eksempel på, hvordan man bruger node- og linkede listeklasser til at oprette og manipulere en linket liste i C++:
#include using namespace std; // Node class class Node { public: int data; Node *next; Node(int data) { this->data = data; this->next = nullptr; } }; // Linked list class class LinkedList { private: Node *head; public: LinkedList() { this->head = nullptr; } void insertAtBeginning(int data) { Node *newNode = new Node(data); newNode->next = head; head = newNode; } void insertAtEnd(int data) { Node *newNode = new Node(data); if (head == nullptr) { head = newNode; return; } Node *temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } void deleteAtBeginning() { if (head == nullptr) { return; } Node *temp = head; head = head->next; delete temp; } void deleteAtEnd() { if (head == nullptr) { return; } if (head->next == nullptr) { delete head; head = nullptr; return; } Node *temp = head; while (temp->next->next != nullptr) { temp = temp->next; } delete temp->next; temp->next = nullptr; } void printList() { Node *temp = head; while (temp != nullptr) { cout <data <next; } cout << endl; }; int main() { create a linked list linkedlist list; insert some nodes at the beginning of list.insertatbeginning(3); list.insertatbeginning(2); list.insertatbeginning(1); end list.insertatend(4); list.insertatend(5); list.insertatend(6); print 'original list: '; list.printlist(); delete node list.deleteatbeginning(); again 'list after deleting beginning: list.deleteatend(); end: return 0; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/94/create-linked-list-c.webp" alt="Create Linked List In C++"> <p> <strong>Explanation:</strong> </p> <p>This linked list class has a private field head that points to the first node in the List and various public methods for inserting and deleting nodes at the beginning and end of the List and for printing the List to the console. This program will create a linked list, insert some nodes at the beginning and end of the List, delete a node at the beginning and end of the List, and print the List to the console.</p> <p> <strong>Here is an example of how to create a linked list in C++ using a template class:</strong> </p> <pre> #include template class Node { public: T data; Node *next; Node(T data) { this->data = data; this->next = nullptr; } }; template class LinkedList { private: Node *head; public: LinkedList() { this->head = nullptr; } void insertAtBeginning(T data) { Node *newNode = new Node(data); newNode->next = head; head = newNode; } void insertAtEnd(T data) { Node *newNode = new Node(data); if (head == nullptr) { head = newNode; return; } Node *temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; } void deleteAtBeginning() { if (head == nullptr) { return; } Node *temp = head; head = head->next; delete temp; } void deleteAtEnd() { if (head == nullptr) { return; } if (head->next == nullptr) { delete head; head = nullptr; return; } Node *temp = head; while (temp->next->next != nullptr) { temp = temp->next; } delete temp->next; temp->next = nullptr; } void printList() { Node *temp = head; while (temp != nullptr) { std::cout <data <next; } std::cout << std::endl; }; int main() { create a linked list linkedlist list; insert some nodes at the beginning of list.insertatbeginning(3); list.insertatbeginning(2); list.insertatbeginning(1); end list.insertatend(4); list.insertatend(5); list.insertatend(6); print 'original list: '; list.printlist(); delete node list.deleteatbeginning(); again 'list after deleting beginning: list.deleteatend(); end: return 0; < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/c-tutorial/94/create-linked-list-c-2.webp" alt="Create Linked List In C++"> <p> <strong>Explanation:</strong> </p> <p>In the above code, we have defined a template class Node that represents a single node in a linked list. The Node class has a public data field data of type T (where T is a template parameter) and a public pointer next to the next node in the List. It also has a constructor that initializes the data field and sets the next pointer to nullptr.</p> <p>We have also defined a template class, LinkedList, that represents a linked list and contains a private field head that points to the first node in the List. The LinkedList class has various public methods for inserting and deleting nodes at the beginning and end of the List and for printing the List to the console.</p> <p>The insertAtBeginning method creates a new node with the given data and inserts it at the beginning of the List by updating the head pointer. The insertAtEnd method creates a new node with the given data and inserts it at the end of the List by traversing it and updating the last node's next pointer. The deleteAtBeginning method deletes the first node in the List by updating the head pointer and freeing up the memory used by the node. The deleteAtEnd method deletes the last node in the List by traversing the List and updating the next pointer of the second to the last node. The printList method traverses the List and prints the data of each node to the console.</p> <p>In the main function, we create an instance of the LinkedList class and call the various methods to insert and delete nodes and print the List. The output shows the original List, the List after deleting a node at the beginning, and the List after deleting a node at the end.</p> <hr></data></pre></data>