Academy Online Learning Notes
Concept of linked list and node class template
Linear population of sequential access--list class
A linked list is a dynamic data structure that can be used to represent a linear group of sequential accesses;
Linked lists are composed of series nodes, which can be dynamically generated at runtime;
Each node includes a data field and a pointer to the next node in the list (that is, the address of the next node). If there is only one pointer to the successor node in each node of the list, the linked list is called a single linked list;
Node class templates for single-linked lists
Template <class t>void node<t>::insertafter (node<t> *p) { // p node pointer field points to the successor node of the current node p->next = Next; = P; // the pointer field of the current node points to P}
To delete a node after a node:
Node<t> *node<t>::d eleteafter (void) { Node<T> *tempptr = Next; if 0 ) return0; = Tempptr->Next; return tempptr;}
//Node.h#ifndef Node_h#defineNode_h//definition of a class templateTemplate <classT>classnode{Private: Node<T> *next;//pointers to subsequent nodes Public: T data;//data fieldsNode (ConstT &data, node<t> *next =0);//constructor Function voidInsertAfter (node<t> *p);//insert a similar node after this node P .Node<t> *deleteafter ();//Delete the subsequent node of this node and return its addressNode<t> *nextnode ();//get the address of the successor node. ConstNode<t> *nextnode ()Const;//get the address of the successor node.};//implementation part of the class//constructors, initializing data and pointer membersTemplate <classT> node<t>:: Node (ConstT &data,node<t> *next =0): Data (data), next (next) {}//returns the pointer to the subsequent node.Template <classT> node<t> *node<t>:: NextNode () {returnnext;}//returns the pointer to the subsequent node.Template <classT>ConstNode<t> *node<t>::nextnode ()Const{ returnnext;}//inserts a node after the current node PTemplate <classT>voidNode<t>:: InsertAfter (node<t> *p) {P->next = Next;//p node Pointer field points to the successor node of the current node .Next = p;//the pointer field of the current node points to P}//deletes the subsequent node of the current node and returns its addressTemplate <classT> node<t> *node<t>::d eleteafter () {Node<T> *tempptr = next;//Save the address of the node you want to delete to Tempptr if(Next = =0)//returns a null pointer if the current node has no subsequent nodes return 0; Next= tempptr->next;//point the Pointer field of the current node to the successor of the Tempptr returnTempptr;//returns the address of the node that was deleted}#endif // ! Node_h
C + + Learning Note 47: Linked List concepts and node class templates