Single-chain table c ++ implementation, using templates

Source: Internet
Author: User

Single-chain table c ++ implementation, using templates

Node Construction


  using namespace std;template
   class list;template
    class list_node{friend class list<_Ty>;public:list_node():next(NULL){}list_node(const _Ty item, list_node<_Ty>* tail=NULL):data(item),next(tail){}~list_node(){}private:_Ty data;list_node *next;};

// List method implementation



# Include "list_node.h" template
Class list {public: list (): head (new list_node <_ Ty> ()){}~ List () {delete head;} public: void Insert (const _ Ty x, int I) // insert data x to position I {list_node <_ Ty> * phead = head; list_node <_ Ty> * p = new list_node <_ Ty> (x ); while (-- I & phead) {phead = phead-> next;} p-> data = x; p-> next = phead-> next; phead-> next = p;} void Show () const {list_node <_ Ty> * phead = head-> next; while (phead! = NULL) {cout <
Data <"->"; phead = phead-> next;} cout <
* P = head-> next; while (-- I) {p = p-> next;} return p-> data;} _ Ty Remove (int I) // Delete the node at the I position and return the data of the deleted node. {List_node <_ Ty> * p = head; list_node <_ Ty> * q; while (-- I & p) {p = p-> next ;} q = p-> next; p-> next = q-> next; _ Ty tmp = q-> data; delete q; return tmp;} void RemoveAll (_ Ty item) // delete all nodes {list_node <_ Ty> * p = head-> next; int I = 1; while (p! = NULL) {if (p-> data = item) {p = p-> next; // record the next node of p, next time, start querying Remove (I) ;}else {I ++; p = p-> next ;}} void Clear () from p-> next () {list_node <_ Ty> * p = head-> next; while (p! = NULL) {p = p-> next; Remove (1) ;}} size_t size () const // No head node is added {int I = 0; list_node <_ Ty> * p = head-> next; while (p! = NULL) {I ++; p = p-> next;} return I;} private: list_node <_ Ty> * head ;};


// Test the instance


# Include "list. h" void main () {list
St; for (int I = 1; I <10; ++ I) st. insert (I, I); // st. insert (5, 5); // st. insert (2, 2); // st. show (); // st. remove (2); // st. show (); // st. removeAll (2); cout <
In the interview book --- find the last K elements of a single linked list


There are three main implementation ideas:

1. Use two pointers: fast and slow to traverse K nodes first. fast and slow can traverse at the same time. When fast traverses to NULL nodes, that is, the request ---> (this method only traverses once)


_Ty Find2(int K){list_node<_Ty> *fast = head->next;list_node<_Ty> *slow = head->next;while(K--){fast = fast->next;}while(fast != NULL){fast = fast->next;slow = slow->next;}return slow->data;}
2. First, traverse all nodes to calculate the number and size of nodes. You need to find the last K elements, that is, find the positive number, size-K, and 1 element. Just traverse suze-K + 1 again.



_ Ty Find (int K) // Find the last K elements {list_node <_ Ty> * p = head-> next; int I = 1; while (p-> next! = NULL) {I ++; // count the total number of elements p = p-> next;} int j = I-K + 1; // It is equivalent to finding the j-th element p = head-> next; while (-- j & p! = NULL) {p = p-> next;} return p-> data ;}
3. Use two pointers, one for traversing and one for record traversing the pointer of the previous node. First, traverse K nodes to determine whether the current node is empty,


If it is null, it indicates that the previous node is traversed (that is, the first node is required). If it is not null, The Record Pointer Points to the next node and is assigned to the traversal pointer. The traversal pointer continues to traverse K nodes, and so on...

_Ty Find3(int K){list_node<_Ty> *p = head->next;list_node<_Ty> *q = head->next;while(p != NULL){while(K--){p = p->next;}if(p == NULL)return q;else{q = q->next;p = q;}}}


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: 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.