Basic data structure (2) -- linked list

Source: Internet
Author: User

The linked list was developed from 1955-56 and was developed by Allen Newell and cliff of the Rand Corporation (English: RAND Corporation ).
Shaw and Herbert Simon are written as raw data types in their information processing language (IPL. IPL was used by authors to develop several early artificial intelligence programs, including logic inference engines, a general-purpose problem solver, and a computer chess program.

 

The forms of linked lists mainly include single-chain tables, two-way linked lists, and circular linked lists.

 

1 single-chain table

One-way linked list is the simplest one. It contains two fields, one information field and one pointer field. This link points to the next node in the list, and the last node points to a null value.

Let's look at the implementation of a single-chain table:

template <class T>class LinkedList;template <class T>class ListElement{T datum;ListElement* next;ListElement (T const&, ListElement*);public:T const& Datum () const;ListElement const* Next () const;friend LinkedList<T>;};template <class T>class LinkedList{ListElement<T>* head;ListElement<T>* tail;public:LinkedList ();~LinkedList ();LinkedList (LinkedList const&);LinkedList& operator = (LinkedList const&);ListElement<T> const* Head () const;ListElement<T> const* Tail () const;bool IsEmpty () const;T const& First () const;T const& Last () const;void Prepend (T const&);void Append (T const&);void Extract (T const&);void Purge ();void InsertAfter (ListElement<T> const*, T const&);void InsertBefore (ListElement<T> const*, T const&);};template <class T>ListElement<T>::ListElement (T const& _datum, ListElement<T>* _next) :datum (_datum), next (_next){}template <class T>T const& ListElement<T>::Datum () const{ return datum; }template <class T>ListElement<T> const* ListElement<T>::Next () const{ return next; }template <class T>LinkedList<T>::LinkedList () :head (0),tail (0){}template <class T>void LinkedList<T>::Purge (){while (head != 0){ListElement<T>* const tmp = head;head = head->next;delete tmp;}tail = 0;}template <class T>LinkedList<T>::~LinkedList (){ Purge (); }template <class T>ListElement<T> const* LinkedList<T>::Head () const{ return head; }template <class T>ListElement<T> const* LinkedList<T>::Tail () const{ return tail; }template <class T>bool LinkedList<T>::IsEmpty () const{ return head == 0; }template <class T>T const& LinkedList<T>::First () const{if (head == 0){throw domain_error ("list is empty");}return head->datum;}template <class T>T const& LinkedList<T>::Last () const{if (tail == 0){throw domain_error ("list is empty");}return tail->datum;}template <class T>void LinkedList<T>::Prepend (T const& item){ListElement<T>* const tmp = new ListElement<T> (item, head);if (head == 0){tail = tmp;}head = tmp;}template <class T>void LinkedList<T>::Append (T const& item){ListElement<T>* const tmp = new ListElement<T> (item, 0);if (head == 0){head = tmp;}else{tail->next = tmp;}tail = tmp;}template <class T>LinkedList<T>::LinkedList (LinkedList<T> const& linkedList) :head (0),tail (0){ListElement<T> const* ptr;for (ptr = linkedList.head; ptr != 0; ptr = ptr->next){Append (ptr->datum);}}template <class T>LinkedList<T>& LinkedList<T>::operator = (LinkedList<T> const& linkedList){if (&linkedList != this){Purge ();ListElement<T> const* ptr;for (ptr = linkedList.head; ptr != 0; ptr = ptr->next){Append (ptr->datum);}}return *this;}template <class T>void LinkedList<T>::Extract (T const& item){ListElement<T>* ptr = head;ListElement<T>* prevPtr = 0;while (ptr != 0 && ptr->datum != item){prevPtr = ptr;ptr = ptr->next;}if (ptr == 0)throw invalid_argument ("item not found");if (ptr == head)head = ptr->next;elseprevPtr->next = ptr->next;if (ptr == tail)tail = prevPtr;delete ptr;}template <class T>void LinkedList<T>::InsertAfter ( ListElement<T> const* arg, T const& item){ListElement<T>* ptr = const_cast<ListElement<T>*> (arg);if (ptr == 0)throw invalid_argument ("invalid position");ListElement<T>* const tmp =new ListElement<T> (item, ptr->next);ptr->next = tmp;if (tail == ptr)tail = tmp;}template <class T>void LinkedList<T>::InsertBefore (  ListElement<T> const* arg, T const& item){ListElement<T>* ptr = const_cast<ListElement<T>*> (arg);if (ptr == 0)throw invalid_argument ("invalid position");ListElement<T>* const tmp = new ListElement<T> (item, ptr);if (head == ptr)head = tmp;else{ListElement<T>* prevPtr = head;while (prevPtr != 0 && prevPtr->next != ptr)prevPtr = prevPtr->next;if (prevPtr == 0)throw invalid_argument ("invalid position");prevPtr->next = tmp;}

 

2 two-way linked list

A more complex linked list is "two-way linked list" or "double-sided linked list ". Each node has two connections: one refers to the previous node (when this "connection" is the first "connection", it points to a null value or an empty list); the other points to the next node, (when this "connection" is the last "connection", it points to a null value or an empty list)

 

 

A two-way linked list is also called a double-link table. A two-way linked list not only has a pointer to the next node, but also a pointer to the previous node. In this way, you can access the previous node from any node. Of course, you can also access the next node and the entire linked list. It is generally used when a large volume of additional data needs to be stored in the position of the linked list. Two-way linked lists can also be used with the extension of other linked lists below.

 

Because a pointer to the content of the linked list is stored and adjacent nodes may be modified, the first node may be deleted or a new node may be added before. Modify the pointer to the first node. There is a convenient way to eliminate this special situation by storing a virtual node that will never be deleted or moved after the last node and before the first node, form a circular linked list. The node after this virtual node is the real first node. In this case, the virtual node can be used to directly represent the linked list. If the linked list exists in an array separately, you can also directly use this array to represent the linked list and use the 0th or-1 (if supported by the compiler) node to fixed the virtual node.

 

The implementation of a two-way linked list is simpler than that of a single-chain table. The implementation code is not listed here.

 

3. Cyclic linked list

In a circular linked list,
The first and last nodes are connected together. This method can be implemented in one-way and two-way linked lists. To convert a circular linked list, you start from any node and then go in any direction of the List until the returned node starts. Let's look at another method. The circular linked list can be regarded as "headless and tailless ". This list is helpful for saving data storage cache. Assume that you have an object in a list and want all other objects to iterate under a non-special arrangement.

A pointer to the entire list can be called an access pointer.

 

The first node in the circular linked list is the last node, and vice versa. The borderless circular linked list makes it easier to design an algorithm on such a linked list than a common one. The newly added nodes should be flexible after the first node or after the last node according to actual requirements, with little difference. Of course, if only data is inserted at the end (or only before), it is easy to process.

 

Another kind of simulated cyclic linked list is to manually jump to the first node after accessing the last node. The same is true when accessing the first node. In this way, you can also implement the Circular linked list function, which can be used when directly using the circular linked list is more troublesome or may cause problems.

 

 

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.