Data Structure trainer 02 two-way linked list implementation

Source: Internet
Author: User

This evening, I used a template to implement a two-way linked list. Due to my carelessness, I shook my hand in the insert statement and converted tail-> prev into tail-> next, because the error occurred during the execution of the drop function, I have been calling the drop function for a long time and the error is still the same. Finally, my system monitored the change of the value in, finally, an insert error occurs. It seems that programmers cannot do without debugging.

In addition, the template output overload I mentioned yesterday was added to the implementation of youyuan, but today I tested it in gcc and said that no matching function can be found, as a result, the compilation fails, and it really hurts. The difference between vs and g ++ is really big. It seems that we have to study the template output overload in another day. If you know it, I hope you can tell me.

The implementation of the data structure is actually very simple. The idea is:

1. Define the node Structure class, including the data domain and pointer domain

2. Define the Linked List (tree, graph) structure, which encapsulates several nodes and serves as a data interface, because the node definition points to its own type pointer, therefore, all subsequent operations are centered on this data interface.

For a two-way linked list, the node definition is very simple. A data field, a next pointer, indicates that it will point to the next node in the future; a prev pointer indicates that it will refer to the forward node.

template<typename T>**


Because the nodes added to the chain structure must be new and the node class contains data fields, you need to write the constructor for the node class. Generally, there are two, if a node with data fields (new nodes used in the linked list later) has no parameters by default (for new nodes to the head and tail), The Destructor will no longer need to be used, because the pointer field points to something new from the table structure, the operation should be left to the table.

After the node class is defined, we define the Linked List class. The main part is to include the head pointer and tail pointer.

In addition, all tables (tree, graph structure) contain "data indicating the length", such as size. In this way, the length () operation only requires the complexity of O (1, otherwise, we need to traverse the entire linked list. The complexity is O (n.

, Head-> prev points to itself or NULL;

, Tail-> next points to itself or NULL; this defines the head, after tail, the subsequent operations will be very smooth, we can end with next/prev, haha.

NULL condition: size = 0 or head-> next = NULL or tail-> prev = NULL.

There are some addition, deletion, modification, and query operations. Don't shake your hand and write an error to point to the link. In addition, you can add a location judgment to see which side is less moving from the beginning or end.

Suggestions for writing functions are as follows:

If only the access is not modified, the member function is const;

To operate the class type, use & or const &;

Okay, go directly to the Code:

First the header file, pay attention to the last line;

MY_CHAIN_H # include <ostream> template <T> Node * Node (T & template <T> ~ Find (pos, T & hold) search (T & x) length () isEmpty () dList <T> & drop (pos, T & dList <T> & insert (pos, T & dList <T> & push_back (T & dList <T> & push_front (T & [] (show (std: ostream & OS) friend ostream & <> (ostream & OS, dList & Node <T> * # includeView Code

The following is the implementation file:

MY_CHAIN_CPP <ostream> <cassert> template <T> :: node (T & = prev = <T> = prev = template <T> = Node <T>-> next =-> prev =-> next =-> prev = template <typename T> <T>: :~ (Head) {Node <T> * p = head-> <T> * (p! === P-> = NULL; tail = <T> dList <T>: isEmpty () size = <T> dList <T>: find (pos, T & hold) (pos <| pos> size) <T> * (pos <= (size>) {p = count = (count --) {p = p-> next ;}}= tail; count = size-(count --) {p = p-> prev ;} = p-> <T> dList <T>: search (T & x) <T> * p = head-> count = (p! = NULL) & (p-> datum! = P-> ++ (p = <T> dList <T>: length () <T> & dList <T> :: push_front (T & x) <T> * p = Node <T> (size =-> next =-> prev =-> next = head-> next -> prev =-> next = ++ * <T> & dList <T>:: push_back (T & x) <T> * p = Node <T> (tail-> prev =-> next =-> prev-> next = -> prev = tail-> next =-> prev = ++ * <T> & dList <T>:: insert (pos, T & x) (pos = (size + <T> * = N Ode <T> * (pos <= (size/= t = (t --) {p = p-> = t = size-(t --) {p = p-> next =-> prev = p-> prev; p-> prev-> next =-> prev = ++ * <T> & dList <T>: drop (pos, T & (pos <| pos> <T> * d = (pos = head-> = d-> (size =-> prev =-> next =-> next = d-> next-> prev = -- = * (pos = tail-> = d-> prev-> next = -- = * (pos <= (size/c = (c --) {d = d-> c = size-= (c --) {d = d-> prev-> next = d-> next-> prev = D-> -- = * <T> & dList <T> :: [] (= pos + (pos <| pos> <T> * p = (pos <= (size> t = (t --) {p = p-> t = size-= (t --) {p = p-> <T> dList <T >:: show (ostream & OS) <T> * p = head-> t = (p! = <P-> datum <= p-> ++ = <T> & <> (ostream &, dList <T> &View Code

Finally, the test file is:

# Include <iostream> dList <> x = dl. insert (, cout <dl <cout <dl. length () <dl. push_front (cout <dl <cout <dl. length () <dl. push_back (cout <dl <cout <dl. length () <dl. insert (, cout <dl <cout <dl. length () <dl. find (dl. drop (cout <dl <cout <dl. length () <cout <dl [] <dl [] = cout <dl [] <cout <dl <}View Code

The result is shown in:

Supplement: in fact, more operators can be reloaded. If you are in a mood, you can add them by yourself, such as ++ (int) and ++ operations. You can even add an iterator class to make it easier to use and implement it with time.

In addition, do not shake your hands. Give it up!

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.