Basic operations on a single-linked table
Please bypass
/**************** \ * Single-chain table C ++ implements * Created by Satan * Time: january 28, 2015 */# include <iostream> using namespace std; // The structure of the storage node, struct listElmt {int data; // store the data listElmt * next; // point to the next element listElmt (int tmp) {this-> data = tmp; this-> next = NULL ;}}; // linked List class List {private: listElmt * head; // head node pointer listElmt * tail; // End Node pointer int length; // linked List length public: /*********************** \ * constructor, initialize the linked List \ *********************/List (){ This-> head = NULL; this-> tail = NULL; this-> length = 0 ;} /************************ parameter: data address * return value: true if the element is successfully inserted, otherwise, false * is returned. Description: Insert a new node at the end of the linked list. The data of the node is the data transmitted by parameters. If the linked list is empty at this time, * the head pointer and tail pointer are directed to the node at the same time, if it is not null, the tail pointer is directed to the node \ *********************/bool insert (int data) {int priorLength = this-> length; // The length of the listElmt * newElmt = new listElmt (data); if (this-> length = 0) before the record operation) {// If the linked list is empty, point the header and tail pointer to this node this-> head = newElmt; this-> tail = newElmt; this-> length ++ ;} else {this-> tail-> next = newElmt; this-> tail = this-> tail-> next; this-> length ++ ;} return bool (this-> length-priorLength );}/********* * *********** \ * Parameter: data address * return value: Number of deleted elements * description: delete all elements that are the same as the specified data from the linked list. If the deleted node is a header node, the header Pointer Points to the node after the header node. * If the deleted node is an intermediate node, the previous node of the node should point to the next node of the node. If the last node is deleted, * The tail pointer should point to the previous node \ **************************/int remove (int data) {listElmt * currentElmt = this-> head; // int count = 0 for the node currently pointed to in the traversal chain table; // The number of deleted nodes if (currentElmt) {return count ;} if (currentElmt-> data = data) {// if you delete a header node, the header pointer moves this-> head = currentElmt-> next; count ++; delete (currentElmt);} while (currentElmt-> next! = NULL) {// traverse the node after the header node if (currentElmt-> next-> data = data) {listElmt * tmpElmt = currentElmt-> next; // temporarily store the node to be deleted if (tmpElmt-> next = NULL) {// if the last node is deleted, the last pointer moves forward to currentElmt-> next = NULL; this-> tail = currentElmt;} else {// if you delete the intermediate node currentElmt-> next = tmpElmt-> next;} count ++; delete (tmpElmt );}} return count;}/************************** \ * parameter: data address * return value: if an element is found, true is returned. Otherwise, false is returned. * Description: prepaid table, find the node with the same data and parameter data \************************ */Bool contains (int data) {listElmt * currentElmt = this-> head; if (currentElmt = NULL) {// return false for empty linked list ;} do {// enumerate the if (currentElmt-> data = data) {return true ;}} while (currentElmt = currentElmt-> next ); // jump out of the loop after the current node points to the end node and return false ;} /*********************** \ * parameter: void * return value: void * Description: permissionlist table, print the stored data \ *************************/void print () {listElmt * currentElmt = this-> head; if (currentElmt = NULL) {retu Rn;} do {cout <currentElmt-> data <'\ n';} while (currentElmt = currentElmt-> next );} /************************* \ * parameter: void * return value: chain table length * description: returns the chain table length \ ************************/int getLength () {return this-> length;}/************************* \ * destructor, destroy linked list \************************/~ List () {listElmt * currentElmt = this-> head; listElmt * nextElmt; do {nextElmt = currentElmt-> next; delete (currentElmt);} while (currentElmt );}};