logo

C++에서 연결리스트 만들기

연결리스트란 무엇인가요?

연결된 목록은 일련의 노드로 구성된 선형 데이터 구조이며, 각 노드는 목록의 다음 노드에 대한 참조(포인터)와 데이터 조각을 저장합니다. 연결 목록은 컬렉션 크기를 미리 알 수 없거나 데이터가 자주 삽입되거나 삭제되는 경우 데이터 컬렉션을 저장하는 데 유용합니다.

100만 자릿수

C++에서는 노드 클래스와 연결 리스트 클래스를 정의하여 연결 리스트를 만들 수 있습니다. 노드 클래스는 목록의 단일 노드를 나타내며 데이터 필드와 다음 노드에 대한 포인터를 포함합니다. 연결된 목록 클래스에는 목록의 첫 번째 노드에 대한 헤드 포인터와 목록의 노드를 삽입, 삭제 및 순회하는 다양한 메서드가 포함됩니다.

다음은 C++의 노드 클래스 예입니다.

 class Node { public: int data; Node *next; Node(int data) { this->data = data; this->next = nullptr; } }; 

이 노드 클래스에는 int 유형의 공개 데이터 필드 데이터와 목록의 다음 노드 옆에 공개 포인터가 있습니다. 또한 데이터 필드를 초기화하고 다음 포인터를 nullptr로 설정하는 생성자가 있습니다.

다음은 노드 및 연결 목록 클래스를 사용하여 C++에서 연결 목록을 만들고 조작하는 방법에 대한 예입니다.

 #include using namespace std; // Node class class Node { public: int data; Node *next; Node(int data) { this-&gt;data = data; this-&gt;next = nullptr; } }; // Linked list class class LinkedList { private: Node *head; public: LinkedList() { this-&gt;head = nullptr; } void insertAtBeginning(int data) { Node *newNode = new Node(data); newNode-&gt;next = head; head = newNode; } void insertAtEnd(int data) { Node *newNode = new Node(data); if (head == nullptr) { head = newNode; return; } Node *temp = head; while (temp-&gt;next != nullptr) { temp = temp-&gt;next; } temp-&gt;next = newNode; } void deleteAtBeginning() { if (head == nullptr) { return; } Node *temp = head; head = head-&gt;next; delete temp; } void deleteAtEnd() { if (head == nullptr) { return; } if (head-&gt;next == nullptr) { delete head; head = nullptr; return; } Node *temp = head; while (temp-&gt;next-&gt;next != nullptr) { temp = temp-&gt;next; } delete temp-&gt;next; temp-&gt;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-&gt;data = data; this-&gt;next = nullptr; } }; template class LinkedList { private: Node *head; public: LinkedList() { this-&gt;head = nullptr; } void insertAtBeginning(T data) { Node *newNode = new Node(data); newNode-&gt;next = head; head = newNode; } void insertAtEnd(T data) { Node *newNode = new Node(data); if (head == nullptr) { head = newNode; return; } Node *temp = head; while (temp-&gt;next != nullptr) { temp = temp-&gt;next; } temp-&gt;next = newNode; } void deleteAtBeginning() { if (head == nullptr) { return; } Node *temp = head; head = head-&gt;next; delete temp; } void deleteAtEnd() { if (head == nullptr) { return; } if (head-&gt;next == nullptr) { delete head; head = nullptr; return; } Node *temp = head; while (temp-&gt;next-&gt;next != nullptr) { temp = temp-&gt;next; } delete temp-&gt;next; temp-&gt;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&apos;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>