Java simulated single-chain table

Source: Internet
Author: User

Java simulated single-chain table

Linear table:

A linear table (also called a sequence table) is the most basic, simple, and commonly used data structure.

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

The logical structure of a linear table is simple for implementation and operation.

In practice, linear tables are used in the form of special linear tables such as stacks, queues, and strings.

The basic features of a linear structure are:

1. A unique "first element" must exist in the collection ";

2. A unique "last element" must exist in the collection ";

3. Except for the last element, each element has a unique successor (post );

4. Apart from the first element, each element has a unique precursor (front part ).


Linked list: linked list

A linked list is a non-sequential storage structure of physical storage units. The logical order of data elements is achieved through the pointer link order in the linked list.

Each data item is included in the Link.

A Link is a class object, which can be called a Link. There are many similar Link nodes in the linked list. Each Link contains a field referenced by next Link.

The linked list object saves a reference first pointing to the first link. (If there is no first, it cannot be located)

A linked list cannot directly access data items like an array (using subscript). Instead, you need to use the relationship between the data to locate it, that is, the next link referenced by the Access link node, and then the next one, to access the required data

Single-chain table:

A linear table is called a Linear Linked List (single-chain table) in the form of "node sequence)

It is a chained data structure that stores data elements in a linear table with any storage unit of a group of addresses. (This set of storage units can be continuous or discontinuous)

Link node structure:

┌ ── ┬ ── ┐

│ Data │ next │

└ ── ┴ ── ┘

Data in the data field that stores the node value; the pointer field referenced by the node (chain field) next

The linked list links n nodes of a linear table in the logical order through the chain fields of each node.

Each node has only one Chain Domain Linked List called a Single Linked List. In one direction, only subsequent nodules are referenced.

/** Single-chain table: After the header is inserted, the chain head is called on the left side of the linked list, and the chain tail is called on the right side. * The header insertion method is used to create a single-chain table. the right end of the linked list is fixed, and the linked list is continuously extended to the left. * The first result obtained by the header insertion method is the end node */public class SingleLinkedList
 
  
{Private Link
  
   
First; // The first node public SingleLinkedList () {} public boolean isEmpty () {return first = null;} public void insertFirst (T data) {// insert to the Link
   
    
NewLink = new Link
    
     
(Data); newLink. next = first; // next of the new node points to first = newLink;} public Link
     
      
DeleteFirst () {// Delete the Link
      
        Temp = first; first = first. next; // change the first node, which is the next node return temp;} public Link
       
         Find (T t) {Link
        
          Find = first; while (find! = Null) {if (! Find. data. equals (t) {find = find. next;} else {break;} return find;} public Link
         
           Delete (T) {Link
          
            P = first; Link
           
             Q = first; while (! P. data. equals (t) {if (p. next = null) {// It indicates that return null has not been 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
            
              Current = first; while (current! = Null) {current. displayLink (); current = current. next ;}} public void displayListReverse () {// Link traversal in reverse order
             
               P = first, q = first. next, t; while (q! = Null) {// reverse pointer, reverse traversal data order t = q. next; // no3if (p = first) {// if it is the original header. next should be set to null p. next = null;} q. next = p; // no3-> no1 pointer reversep = q; // start is reverseq = t; // no3 start} // In the if column in the above loop, set first. when q is null and the loop is not executed, p is the most original data item. If q is null, p is assigned to firstfirst = p; displayList ();} class Link
              
                {// Link node T data; // Link of the data domain
               
                 Next; // next pointer, Link (T data) {this. data = data;} void displayLink () {System. out. println ("the data is" + data. toString () ;}} public static void main (String [] args) {SingleLinkedList
                
                  List = new SingleLinkedList
                 
                   (); List. insertFirst (33); list. insertFirst (78); list. insertFirst (24); list. insertFirst (22); list. insertFirst (56); 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 (78); list. displayList (); System. out. println ("---- reverse ----"); list. displayListReverse ();}}
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
Print

List (first-->last):the data is 56the data is 22the data is 24the data is 78the data is 33List (first-->last):the data is 22the data is 24the data is 78the data is 33find:nullfind:linked_list.SingleLinkedList$Link@35a8767delete find:nulldelete find:linked_list.SingleLinkedList$Link@2c6f7ce9List (first-->last):the data is 22the data is 24the data is 33----reverse----List (first-->last):the data is 33the data is 24the data is 22

/** Single-chain table: the tail insertion method is first-in-first-out. * If the left end of the chain table is fixed, the chain table continues to extend to the right. This method of creating a chain table is called the tail insertion method. * When a chain table is created by the tail plug method, the head pointer remains unchanged. Therefore, a tail pointer must be set up to extend to the right of the chain table. * The first result of the tail plug method is the head node. */Public class SingleLinkedList2
 
  
{Private Link
  
   
Head; // The first node private Link
   
    
Rear; // tail pointer public SingleLinkedList2 () {} public boolean isEmpty () {return head = null;} public void insertLast (T data) {// insert Link at the end of the chain
    
     
NewLink = new Link
     
      
(Data); if (rear! = Null) {rear. next = newLink;} else {head = newLink; head. next = rear;} rear = newLink; // insert from rear next time} public Link
      
        DeleteLast () {// Delete the Link at the end of the chain
       
         P = head; Link
        
          Q = head; while (p. next! = Null) {// The next node of p is not empty. q is equal to the current p (that is, q is the previous node and p is the next node). When the loop ends, q is equal to the second-to-last q = p; p = p. next;} // deleteq. next = null; return p;} public Link
         
           Find (T t) {Link
          
            Find = head; while (find! = Null) {if (! Find. data. equals (t) {find = find. next;} else {break;} return find;} public Link
           
             Delete (T) {Link
            
              P = head; Link
             
               Q = head; while (! P. data. equals (t) {if (p. next = null) {// It indicates that return null has not been 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 (head --> last):"); Link
              
                Current = head; while (current! = Null) {current. displayLink (); current = current. next ;}} public void displayListReverse () {// Link traversal in reverse order
               
                 P = head, q = head. next, t; while (q! = Null) {// reverse pointer, reverse traversal data order t = q. next; // no3if (p = head) {// if it is the original header. next should be set to null p. next = null;} q. next = p; // no3-> no1 pointer reversep = q; // start is reverseq = t; // no3 start} // In the if column in the above loop, place the head. when q is null and the loop is not executed, p is the most original data item. If q is null, p is assigned to headhead = p; displayList ();} class Link
                
                  {// Link node T data; // Link of the data domain
                 
                   Next; // next pointer, Link (T data) {this. data = data;} void displayLink () {System. out. println ("the data is" + data. toString () ;}} public static void main (String [] args) {SingleLinkedList2
                  
                    List = new SingleLinkedList2
                   
                     (); List. insertLast (33); list. insertLast (78); list. insertLast (24); list. insertLast (22); list. insertLast (56); 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 (78); list. displayList (); System. out. println ("---- reverse ----"); list. displayListReverse ();}}
                   
                  
                 
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
Print

List (head-->last):the data is 33the data is 78the data is 24the data is 22the data is 56List (head-->last):the data is 33the data is 78the data is 24the data is 22find:nullfind:linked_list.SingleLinkedList2$Link@2c6f7ce9delete find:nulldelete find:linked_list.SingleLinkedList2$Link@4b71bbc9List (head-->last):the data is 33the data is 24the data is 22----reverse----List (head-->last):the data is 22the data is 24the data is 33

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.