Implementation of two-way cyclic linked list

Source: Internet
Author: User

 

Implementation of two-way cyclic linked list
09:29:04 I want to stick to the ideal sentence _ face the reality
Add to favorites I want to contribute
When the linked list is used to solve the Joseph problem, the circular linked list is used. Cyclic linked lists are further divided into one-way cyclic linked lists and two-way cyclic linked lists. The single-way cyclic linked lists can be used to solve the problem, but the large defects of one-way linked lists still exist, that is, two side-by-side pointers must be synchronized during deletion. The two-way linked list can solve this problem, because each node in the two-way linked list has two pointer fields pointing to its precursor and successor respectively. In this way, you don't need two pointers in the traversal chain table. You can use one directly. After locking the position, you can retrieve its precursor and save the current position for deletion. The node type remains unchanged. There are two pointers: [CPP] # pragma once # include <iostream> using namespace STD; Template <class T> class linknode // node class {public: // All are public and convenient t data operations; // a data linknode of the template type <t> * Next; // a pointer to this node, used to point to the next linknode <t> * Prior; // used to point to the front public: linknode (linknode <t> * PTR = NULL) // only initializes the pointer's constructor, and leave the pointer field empty {next = PTR; prior = PTR;} linknode (const T & item, linknode <t> * PTR = NULL) // The constructor initialized with the data and pointer. The default parameter of the pointer field is null. {DATA = it Em; next = PTR; prior = PTR ;}~ Linknode () {}}; the key to the circular linked list is the formation of the ring, and the best way is to form a ring when creating the linked list. The author uses a linked list with a header node, therefore, you can create a ring in the constructor: [CPP] template <class T> List <t>: List () {First = new linknode <t>; // In the constructor, the first-> prior = first; // The frontend and backend pointers to the First-> next = first ;} after each insertion or deletion, you need to connect the beginning and end. Other operations can be seen as common linked lists. # Pragma once # include "linknode. H "# include <iostream> using namespace STD; Template <class T> // during the inheritance process, the derived class is still the class template class list {protected: linknode <t> * First; // encapsulate public: List (); List (list <t> & L );~ List (); void makeempty (); // destroy the space int length () const of each node in sequence; // search the number of current table nodes linknode <t> * gethead () const; // return the header pointer linknode <t> * search (t x) const; // search and return the pointer linknode <t> * locate (int I) const; // locate and return the pointer bool getdata (int I, T & X) const; // return the element value of node I in the referenced form bool setdata (int I, T & X); // modify the element value bool insert (int I, T & X) of node I ); // Insert the new node element value linknode <t> * remove (int I, T & X) after node I; // Delete the node bool isempty () const; // empty void sort (); // Sort void input (T endtag); // create a table and input void output (); // output table void operator = (list <t> & L ); // copy overload}; Template <class T> List <t>: List () {First = new linknode <t>; // In the constructor, the first-> prior = first; // The frontend and backend pointers to the First-> next = first ;} template <class T> List <t>: List (list <t> & L) {linknode <t> * l_head = L. gethead (); linknode <t> * srcptr = l_head-> next; // get the header pointer for Traversing linknode <t> * destptr = first = ne W linknode <t>; // create a header node to initialize linknode <t> * newnode; t value; while (srcptr! = L_head) // until the loop reaches the first pointer W is the end {value = srcptr-> data; newnode = new linknode <t> (value); newnode-> prior = destptr; // insert destptr-> next = newnode; srcptr = srcptr-> next; // move destptr = destptr-> next;} destptr-> next = first; // create a loop first-> prior = destptr;} template <class T> List <t> ::~ List () {makeempty ();} template <class T> void list <t >:: makeempty () // destroy all pointer resources {linknode <t> * Current = first-> next; linknode <t> * del; while (current! = First) {del = current; current = Current-> next; Delete del ;}} template <class T> int list <t >:: length () const {int COUNT = 0; linknode <t> * Current = first; // a copy of the header pointer is used to traverse the while (current-> next! = First) // The traversal control condition is the same as that of the single-chain table. Whether the current node is left blank after the end? {current = Current-> next; count ++;} return count ;} template <class T> linknode <t> * List <t>: gethead () const {return first;} template <class T> linknode <t> * List <t>:: Search (t x) const {linknode <t> * Current = first-> next; while (current! = NULL) // The control condition at this time is that the next Of a node knows whether it is null {If (current-> DATA = x) return current; // If direct function return is queried, no useless operation is required (if there are multiple X element values in the linked list, the first value prevails to return) Current = Current-> next ;} return NULL;} template <class T> linknode <t> * List <t>: locate (int I) const {if (I <0) // The location can be 0th, that is, the header node pointer return NULL; linknode <t> * Current = first; int K = 0; while (current! = NULL & K <I) // double-condition control the latter for more functions {current = Current-> next; k ++;} return current; // pointer to element I} template <class T> bool list <t>: getdata (int I, T & X) if the const // parameter has a lower mark value, it is generally used to determine whether the lower mark value is valid {if (I <= 0) return false; linknode <t> * Current = locate (I ); if (current = NULL) return false; X = Current-> data; retrun true;} template <class T> bool list <t >:: setdata (int I, T & X) {if (I <= 0) return false; linknode <t> * Cu Rrent = locate (I); If (current = NULL) return false; Current-> DATA = x; return true;} template <class T> bool list <t> :: insert (int I, T & X) {if (I <0) // It can be inserted before the first node. The code with the benefit of the header node is consistent with return false; linknode <t> * Current = locate (I); Return false; linknode <t> * newnode = new linknode <t> (x); If (newnode = NULL) return false; newnode-> prior = current; // The Key four steps for inserting a two-way linked list. The sequence cannot be changed. The last sentence must be followed by newnode-> next = Current-> Ne. XT; Current-> next-> prior = newnode; Current-> next = newnode; return true;} template <class T> linknode <t> * List <t> :: remove (int I, T & X) {if (I <= 0) return NULL; linknode <t> * Current = locate (I ); // unlike a single-linked list, a two-way linked list has a forward pointer that can point to the front-end. Therefore, it is better to locate the I-th If (current = NULL) return NULL; current-> next-> prior = Current-> prior; // The two-way linked list delete operation is relatively simple and clear current-> prior-> next = Current-> next; // relink x = Current-> data; delete current; // destroy Pointer resource} template <class T> bool list <t>: isempty () const {return (first-> next = NULL )? True: false);} template <class T> void list <t >:: input (T endtag) {makeempty (); // clear the linked list linknode <t> * newnode, * Last = first; t value; CIN> value; while (value! = Endtag) {newnode = new linknode <t> (value); newnode-> prior = last; last-> next = newnode; last = newnode; CIN> value ;} last-> next = first; // connect first and end again first-> prior = last;} template <class T> void list <t >:: output () // output {cout <"two-way linked list output:" <Endl; linknode <t> * Current = first-> next; int COUNT = 0; while (current! = First) {cout <"#" <count + 1 <":" <Current-> data <Endl; current = Current-> next; count ++ ;}}template <class T> void list <t >:: sort () // minimum selection sorting {linknode <t> * current1, * current2; // The next pointer is used continuously to control the outer loop at the last vertex for (current1 = first-> next; current1-> next! = First; current1 = current1-> next) {for (current2 = current1-> next; current2! = First; current2 = current2-> next) {If (current1-> DATA> current2-> data) {T temp; temp = current1-> data; current1-> DATA = current2-> data; current2-> DATA = temp; }}} template <class T> void list <t> :: operator = (list <t> & L) {makeempty (); // first destroy all pointer resources linknode <t> * l_head = L. gethead (); // obtain the first pointer to traverse the termination condition linknode <t> * srcptr = l_head-> next; // obtain the header pointer for Traversing linknode <t> * destptr = first; // = new linknode <t>; // It is not used for value assignment initialization. It is only used for copying. You do not need to create a new header node linknode <t> * newnode; t value; while (srcptr! = L_head) // end with a null pointer until the end of the last node {value = srcptr-> data; newnode = new linknode <t> (value ); newnode-> prior = destptr; // insert destptr-> next = newnode; srcptr = srcptr-> next; // move destptr = destptr-> next ;} destptr-> next = first; // first-and-tail-connected loop first-> prior = destptr ;}

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.