banner
cos

cos

愿热情永存,愿热爱不灭,愿生活无憾
github
tg_channel
bilibili

Template Class Encapsulation (1) - Singly Linked List

Added the ReverseList function to reverse the singly linked list operation.
Fixed a bug that specially handles the case of a single node when reversing the linked list.
Fixed several bugs, specially handling the case of an empty linked list.


Try using the template class with data structures.
There will definitely still be omissions, corrections are welcome.
Without further ado, here is the code.
ps: nullptr is a C++11 feature, please pay attention when using it.

Node Definition#

Linked List Operation Class Definition#

Constructor and Destructor#

The head node does not hold data, all operations start from head->next.

Operation Class Implementation#

(1) Find by Index#

Find the Kth element in the linked list, returns a pointer to the node if found, returns nullptr if not found or if the position is invalid.

(2) Find by Value#

Find element data, returns a pointer to the node if found, returns nullptr if not found.

(3) Delete Operation#

Delete the node at position pos in the linked list, defaults to deleting the first element.

(4) Insert Operation#

Defaults to head insertion, inserts data at the pos-th element.

(5) Get the Length of the Linked List#

(6) Display the Linked List#

(7) Reverse the Linked List#

General idea:
Insert image description here
Code:

Complete Code#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.