Java analog single-linked list

Source: Internet
Author: User

Tag: Data structure chain List List

Linear table:

Linear tables (also known as sequential tables) are the most basic, simplest, and most commonly used data structures.

The relationship between data elements in a linear table is a one-to-one relationship, that is, except for the first and last data elements, the other data elements are both end-to-tail.

The logic structure of linear table is simple and easy to implement and operate.

In practical applications, linear tables are used in the form of stacks, queues, strings and other special linear tables.

The basic characteristics of a linear structure are:

1. There must be a unique "first element" in the set;

2. There must be only one "last element" in the set;

3. Except for the last element, there is a unique successor (the latter);

4. In addition to the first element, there is a unique precursor (the preceding piece).


Linked list: Linked list

A linked list is a non-sequential, non-sequential storage structure on a physical storage unit, and the logical order of the data elements is achieved through the order of the pointers in the linked list.

Each data item is included in the link node.

A chain node is an object of a class that can be called link. There are many similar links in the list, and each link contains a field next to the next link node.

The list object itself holds a reference to the first link node. (cannot be located if first is not available)

A linked list cannot directly access a data item as an array (with subscript), but it needs to be positioned in relation to the data, that is, the next link node referenced by the link node, and then the next, until access to the required data

Single Linked list:

Linear table is called linear list (single linked list) with "sequence of nodes".

is a chain-access data structure that stores data elements in a linear table with an arbitrary set of storage units of addresses. (This set of storage cells can be either continuous or discontinuous)

Structure of the link node:┌────┬────┐

│data│next│

└────┴────┘

The data field that holds the node value, the Pointer field (chain field) that holds the reference to the node next

A linked list links the n nodes of a linear table to their logical order by the chain field of each node.

A linked list of only one link field per node is called a single Linked list, One Direction, and only references to subsequent nodules

/* Single linked list: head interpolation LIFO * The left side of the list is called the chain head, and the right is called the chain tail. * Head interpolation method to build a single linked list is the right end of the list as fixed, linked lists continue to extend to the left. * Head interpolation is the first to get the tail node */public class Singlelinkedlist<t> {private link<t> first;//first node public singlelinkedlist () {} public Boolean isEmpty () {return first = = null;} public void Insertfirst (T data) {//inserted into the chain header link<t> NewLink = new link<t> (data); newlink.next = first;//new node NEX T points to the previous node first = NewLink;} Public link<t> Deletefirst () {//delete chain header link<t> temp = First;first = First.next;//change First node, return temp for next node;} Public link<t> Find (T-t) {link<t> find = First;while (find! = null) {if (!find.data.equals (t)) {find = Find.ne XT;} else {break;}} return find; }public link<t> Delete (t t) {link<t> p = First; link<t> q = first;while (!p.data.equals (T)) {if (P.next = = null) {//indicates that no return null was found at the end of the chain;} else {q = P;p = P.next;} }q.next = P.next;return p;} public void Displaylist () {//Traverse System.out.println ("List (first-->last):"); Link<t> current = First;while (current! = null) {CurreNt.displaylink (); current = Current.next;}} Class Link<t> {//Link node T data;//data field link<t> next;//successor pointer, node link domain link (t data) {this.data = data;} void DisplayLink () {System.out.println ("The data is" + data.tostring ());}} public static void Main (string[] args) {singlelinkedlist<integer> list = new singlelinkedlist<integer> (); List.insertfirst (List.insertfirst); List.insertfirst (; list.insertfirst); List.displaylist (); List.deletefirst (); List.displaylist (); System.out.println ("Find:" + list.find (56)); System.out.println ("Find:" + list.find (33)); System.out.println ("Delete find:" + list.delete (99)); System.out.println ("Delete find:" + list.delete); List.displaylist ();}}

Print

List (first-->last): The data is 56the data are 24the data is 78the data are 33List (first-->last): The data is 24the da TA is 78the data is 33find:nullfind:[email protected]delete find:nulldelete find:[email protected]list (first-->last) : The data is 24the data is 33

/* Single linked list: Tail interpolation method FIFO * If the left end of the list is fixed, the list continues to the right, this method of establishing a linked list is called the tail interpolation method. * Tail interpolation method to establish a linked list, the head pointer is fixed, it is necessary to set up a tail pointer, to the right of the list extension, * tail interpolation method first obtained is the head node. */public class Singlelinkedlist2<t> {private link<t> head;//first node private link<t> rear;//tail pointer public SingleLinkedList2 () {}public Boolean isEmpty () {return head = = null;} public void Insertlast (T data) {//at end of chain insert link<t> NewLink = new link<t> (data); if (rear! = null) {Rear.next = new Link;} else {head = Newlink;head.next = rear;} Rear = NewLink; The next time you insert, insert}public link<t> deletelast () {//delete chain tail link<t> p = head from rear; link<t> q = head;while (P.next! = null) {//P's next node is not empty, Q equals the current p (that is, Q is the previous, p is the next) when the loop ends, Q equals the last second of the chain, q = P;p = P.next;} Deleteq.next = Null;return p;} Public link<t> Find (T-t) {link<t> find = Head;while (find! = null) {if (!find.data.equals (t)) {find = Find.nex t;} else {break;}} return find; }public link<t> Delete (t t) {link<t> p = head; link<t> q = head;while (!p.data.equals (T)) {if (P.next = = null) {//represented to chainThe tail has not found return null;} else {q = P;p = P.next;}} Q.next = P.next;return p;} public void Displaylist () {//Traverse System.out.println ("List (first-->last):"); Link<t> current = Head;while (current! = null) {Current.displaylink (); current = Current.next;}} Class Link<t> {//Link node T data;//data field link<t> next;//successor pointer, node link domain link (t data) {this.data = data;} void DisplayLink () {System.out.println ("The data is" + data.tostring ());}} public static void Main (string[] args) {singlelinkedlist2<integer> list = new singlelinkedlist2<integer> (); List.insertlast (List.insertlast); List.insertlast (; list.insertlast); List.displaylist (); List.deletelast (); List.displaylist (); System.out.println ("Find:" + list.find (56)); System.out.println ("Find:" + list.find (33)); System.out.println ("Delete find:" + list.delete (99)); System.out.println ("Delete find:" + list.delete); List.displaylist ();}}
Print

List (first-->last): The data is 33the data are 78the data is 24the data are 56List (first-->last): The data is 33the da TA is 78the data is 24find:nullfind:[email protected]delete find:nulldelete find:[email protected]list (first-->last) : The data is 33the data is 24


Java analog single-linked list

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.