[Java data structure] 2.5 connection and Determination of single-cycle linked list

Source: Internet
Author: User

Features

The cyclic linked list feature is that you do not need to increase the storage capacity, but only slightly change the table link method, which makes table processing more convenient and flexible.

Linked List connection:

Example: Two Linear tables a1, a2,... are implemented on the linked list ,..., An) and b1, b2 ,..., Bm) concatenates a linear table a1 ,..., An, b1 ,... Bm. Analysis: If you perform this link operation on a single-cycle table indicated by a single-chain table or a header pointer, You need to traverse the first linked list, locate the node an, and then link node b1 to the end of, the execution time is O (n ). If it is implemented on the single-loop linked list indicated by the tail pointer, you only need to modify the pointer and do not need to traverse it. The execution time is O (1 ). The algorithm is as follows:
LinkListConnect (LinkListA, LinkListB) {// assume that A and B are non-empty. next; // ① Save the position of the header node of Table. next = B. next. next; // ② link the Start Node of Table B to the end of Table A free (B. next); // ③ release the header Node B of Table B. next = p ;}
Determine whether a linked list has a circular connection

Write two java classes: Linkitem. java and LinklistTest. java:


// Linkitem. javapublic class Linkitem {Linkitem previous; String data; Linkitem next; public Linkitem () {} public Linkitem (Linkitem previous, String data, Linkitem next) {super (); this. previous = previous; this. data = data; this. next = next;} public Linkitem getPrevious () {return previous;} public void setPrevious (Linkitem previous) {this. previous = previous;} public String getData () {retur N data;} public void setData (String data) {this. data = data;} public Linkitem getNext () {return next;} public void setNext (Linkitem next) {this. next = next ;}} **************************************** * ************ // LinklistTest. javaimport java. util. arrayList; public class LinklistTest {public static void main (String [] args) {// initialize the linked list item Linkitem a = new Linkitem (null, "a", null ); linkitem B = n Ew Linkitem (null, "B", null); Linkitem c = new Linkitem (null, "c", null); Linkitem d = new Linkitem (null, "d ", null);. setPrevious (d);. setNext (B); B. setPrevious (a); B. setNext (c); c. setPrevious (B); c. setNext (d); d. setPrevious (c); d. setNext (a); // create an array list ArrayList <Linkitem> list = new ArrayList <Linkitem> (); // Add a list of linked list items. add (a); list. add (B); list. add (c); list. add (d); // determines whether the variable boolean li is the judge variable of the circular linked list. Nk = false; Linkitem start = a; Linkitem current = a; // judge the cyclic linked list for (int I = 0; I <list. size (); I ++) {current = current. next; if (start. data. equals (current. data) {link = true ;}/// output if (link = true) {System. out. println ("This linked list is a circular linked list. ");} Else {System. out. println (" This linked list is not a circular linked list. ");}}}

Output result:


The linked list is a circular linked list.



If you have any questions, feel free to contact us!

This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1288753

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.