Implementation of two-way linked list in C ++

Source: Internet
Author: User

A two-way linked list adds a forward link based on a one-way linked list. Many of the processing methods are similar. For example, you can find the length of a linked list or a node. All similar parts are not implemented one by one. If you are interested, refer to the previous blog. The following two-way linked list is implemented in VC6 ). Correct the error. 1. Code Organization 2. dll. h dual-chain table and node description

# Ifndef _ DLL_H _ # define dataType int # define endOfData 0 class node {public: dataType data; // node data node * next; // backward pointer (close to the end node) node * pre; // forward pointer (close to the head)}; class dll {public: dll ();~ Dll (); void create (); // construct a two-way linked list void print (); // print the linked list bool insert (int pos, dataType num ); // Insert the num node in the position pos. return true if the insertion is complete; otherwise, return false void del (dataType num); // Delete All num nodes in the linked list private: int getLength (); // obtain the number of nodes contained in the linked list node * head; // The linked list header pointer}; # endif

 

3. dll. cpp double-linked table functions
# Include <iostream> # include "dll. h" using namespace std; dll: dll () {head = NULL;} dll ::~ Dll () {node * ptrNode = head; node * ptrNodeTmp = NULL; while (ptrNode! = NULL) {ptrNodeTmp = ptrNode-> next; delete ptrNode; ptrNode = ptrNodeTmp ;}} void dll: create () {// The first node * ptrNode = new node; ptrNode-> pre = NULL; ptrNode-> data = endOfData; head = ptrNode; bool firstNode = true; // whether it is the first node dataType num = endOfData; while (1) {cout <"enter a node data:"; cin> num; if (num = endOfData) {ptrNode-> next = NULL; break ;} if (firstNode = false) // all subsequent nodes require new node {nod E * ptrNodeTmp = new node; ptrNodeTmp-> data = num; ptrNode-> next = ptrNodeTmp; ptrNodeTmp-> pre = ptrNode; ptrNode = ptrNodeTmp;} if (firstNode = true) // The first node does not need new node {ptrNode-> data = num; firstNode = false; }}if (head-> data = endOfData) // No data exists in the linked list, you need to delete the first node {delete head; head = NULL ;}} void dll: print () {node * ptrNode = head; while (ptrNode! = NULL) {cout <ptrNode-> data <""; ptrNode = ptrNode-> next;} cout <endl;} int dll: getLength () {node * ptrNode = head; int num = 0; while (ptrNode! = NULL) {num ++; ptrNode = ptrNode-> next;} return num;} bool dll: insert (int pos, dataType num) {if (pos <0 | pos> = getLength () // The inserted pos is incorrect (0-getLength ()-1) {return false ;} // find the pointer to the pos to be inserted and the next pointer node * ptrNodeAhead = head; node * ptrNodeFellow = NULL; int count = 0; while (1) {if (count ++ = pos) break; ptrNodeFellow = ptrNodeAhead; ptrNodeAhead = ptrNodeAhead-> next;} node * ptr = new node; ptr-> Data = num; ptr-> pre = ptrNodeFellow; ptr-> next = ptrNodeAhead; if (ptrNodeFellow! = NULL) // not the insert header node {ptrNodeFellow-> next = ptr;} else // The insert header node. The head must be changed to {head = ptr ;} ptrNodeAhead-> pre = ptr; return true;} void dll: del (dataType num) {node * ptrNodeAhead = head; // ptrNodeAhead points to the node to be deleted * ptrNodeFellow = NULL; // ptrNodeFellow points to the previous node (node closest to the head) of the node to be deleted while (ptrNodeAhead! = NULL) {while (ptrNodeAhead-> data! = Num & ptrNodeAhead-> next! = NULL) // locate the num node or end the search {ptrNodeFellow = ptrNodeAhead; ptrNodeAhead = ptrNodeAhead-> next;} if (ptrNodeAhead-> data = num) // find the num node {node * ptrNode = ptrNodeAhead-> next; if (ptrNodeAhead! = Head) // not delete the header node {ptrNodeFellow-> next = ptrNode;} else // Delete the header node. Note the head pointer value {head = ptrNodeAhead-> next ;} if (ptrNodeAhead-> next! = NULL) // not delete the end node {ptrNode-> pre = ptrNodeFellow;} delete ptrNodeAhead; // release the num node ptrNodeAhead = ptrNode ;} else // search end {ptrNodeAhead = NULL ;}}}

 

4. mian. cpp Test File
# Include <iostream> # include "dll. h "using namespace std; int main () {dll exp; // create a linked list and print exp. create (); exp. print (); // insert 99999 in position 2 and print exp. insert (2,99999); exp. print (); // Delete the node at location 2 and print exp. del (2); exp. print (); return 0 ;}

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.