Java data structures and algorithms-learning 3

Source: Internet
Author: User

Chapter 5Linked List

Single-chain table:The linklist class has only one data item, that is, the reference to the first link node in the linked list, called first. It is the only permanent information that needs to be maintained in the linked list. It is used to locate all other chain nodes. Starting from first, you can find other link nodes by following the next field of each link node in the linked list.

Insert a new node in the linked list Header

Public void insertfirst (int id, double dd)

{

Link newlink = new link (ID, DD );

Newlink. Next = first; // newlink --> old first

First = newlink; // first --> newlink

}

Delete a node in the chain table header.

Public link deletefirst ()

{

Link temp = first; // save reference to link

First = first. Next; // delete it: first --> old next

Return temp;

}

Search for the specified link node, that is, all nodes in the traversal table, and determine whether the value of the node is set.

Public link find (INT key) // find link with given key

{

Link current = first;

While (current. idata! = Key) // start at 'first'

{

If (current. Next = NULL) // didn't find it

Return NULL;

Else

Current = current. Next; // goto next link

}

Return Current; // found it

}

Deleting a specified link node is similar to the search operation, but it also records the reference of the previous node of the current node, deleting the current node connects the previous node to the next node. When deleting a table, you must also identify a special case. That is, if the current node to be deleted is the first node of the linked list, you only need to point the first field to first. Next.

Public link Delete (INT key) // Delete link with given key

{

Link current = first; // search for Link

Link previous = first;

While (current. idata! = Key)

{

If (current. Next = NULL) // didn't find it

Return NULL;

Else

{

Previous = current; // go to next link

Current = current. Next;

}

} // Found it

If (current = first) // If first link

First = first. Next; // change first

Else

Previous. Next = current. Next; // bypass it

Return Current;

}

Double-ended linked list:It is basically similar to the traditional linked list, but has a new feature: that is, the reference to the last node, just like the reference to the first node, the last Pointer Points to its last node. You can access the end of a table just like you can access the header directly with first. This feature makes the double-ended linked list more suitable for some common linked list inconvenient operations, such as queue implementation. Pay attention to the differences between double-ended linked lists and two-way linked lists.

The insert/delete operation of the double-ended linked list in the header is similar to that of the common linked list. Note that when the inserted node is the first node, the last pointer must be directed to the node, if the last node is deleted, do not forget to set last to null. In addition, you can easily insert a node insertlast (Code omitted) at the end of the table. Also, pay attention to the special case where the linked list is empty before insertion. In this case, set first to null.

Linked List efficiency is higher than array EfficiencyAlthough the comparison times are O (n), the insert/delete operation of the linked list does not need to move anything. You only need to change the reference point. Another important aspect is that the array size is fixed when defined, but the linked list can allocate any size or even all available memory.

ABSTRACT Data Type

ADT, in short, is a way to consider the data structure: focus on what it does, and ignore how it does it. Both stack and queue are examples of ADT. They can be implemented not only using arrays, but also using linked lists. Stack can be implemented using a common linked list, while queue can be implemented using a double-ended linked list.

Data types are data items with specific features and permitted operations on data, such as int type. In object-oriented programming, you can use classes to create your own data types. Then, fields represent data. The method is to perform operations on these fields.

Abstract means "not considering the details of the description and implementation ." In object-oriented programming, an abstract data type is a class without considering its implementation. It only describes the data in the class and the operations that can be performed and how to perform these operations. Users do not know how to implement it or how data is stored. For example, the stack users only know the existence of the push and pop methods and how to use them. It is not important for the stack to use arrays or linked lists. The Methods visible to users such as push and pop are called "interfaces ".

Use ADT for software design. If you need to store data, consider the operations on the data. What is the last inserted data code accessed by a rough waist? Or the first one ?...? Returning to these questions will lead to the ADT definition. The implementation details are considered only after the ADT is defined. The advantage is to simplify the design process and modify the implementation without interfering with the user code, because the user only accesses the ADT interface.

Ordered linked list

In some applications, it is useful to keep data items in the linked list in order. In an ordered linked list, data is ordered by key values. Ordered linked lists are more efficient than ordered arrays, but their implementations are more complex. Ordered linked list application: sorting data, of course, can also implement priority queue.

In an ordered linked list, when inserting a data item, the algorithm must first search for the linked list until a proper position is found: it is right before the first data item that is larger than it. After the algorithm finds the insertion position, it points the next field of the new link node to the next link node, and then changes the next field of the previous link point to the new link node. Special cases should be considered: the link node may be inserted in the header or at the end of the table.

Method public void insert (long key) {omitted ..}

The ordered linked list can locate or delete the minimum value in O (1) because it is always in the header. All ordered linked lists are applicable to scenarios where the minimum items are frequently accessed and no need to be inserted quickly. For example, priority queue.

An ordered linked list can be used for sorting. Suppose there is an unordered array. If you extract data from the data and insert the sorted linked list one by one, they are automatically arranged in order. Delete them from the ordered table and add them to the array again. Then the order of the array will be sorted. This sorting method is more efficient than insert sorting in the array, because this method moves less times and the insert sorting in the array moves O (N * n). In this sort, you only need 2 * n copies. But there are also disadvantages, that is, to open up almost twice the space.

Two-way linked list

It provides the forward and backward traversal capabilities of the linked list. The secret is that each link node has two references pointing to another link node, instead of one. The first node points to the next node. The second is the forward link node.

Its disadvantage is that each time a link node is inserted or deleted, it must be referenced by four links, instead of two. Because two more references are provided, the space occupied is larger.

Dual-end queues can be implemented based on two-way linked lists.

Iterator

The iterator class contains references to data items in the data structure to traverse objects in these structures. The initial definition is as follows:

Class listiterator ()

{

Private Link current;

......

}

The current field contains a reference to the link point currently pointed to by the iterator.

When using the iterator, you can create a linked list, which can also input necessary information. The iterator is associated with the linked list, but it is not the same as the linked list or link node. All the operations previously implemented in the Linked List class related to the iteration, such as insertafter (), can now be implemented by the iterator. Which tasks are implemented by the iterator and the linked list itself? It is best to implement iteration in the iterator, while the insertbefore () method always inserts a new link node in the chain table header, which is more suitable for implementation in the chain table class.

When designing the iterator, a problem is that after different operations, where should the iterator point?

Iterator code (Omitted ...).

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.