Example of Java analog single-link list and double-end linked list data structure _java

Source: Internet
Author: User
Tags extend

Analog single Linked list

Linear table:
A linear table (also a sequential table) is the most basic, simplest and most commonly used data structure.
The relationship between data elements in a linear table is one-to-one, that is, other data elements are end-to-end, except for the first and last data elements.
The logical 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 only one "first element" in the collection;
2. There must be a unique "last element" in the collection;
3. Except the last element, there is a unique successor (the latter);
4. Except for the first element, there is a unique precursor (front).

Linked list: Linked list
A linked list is a discontinuous, non sequential storage structure on a physical storage unit, and the logical order of data elements is achieved through the sequence of pointers in the linked list.
Each data item is included in the link.
A chain node is an object of a class, which can be called link. There are many similar chain nodes in the list, and each link contains a field next to the next linked node.
The list object itself holds a reference to the first link point. (cannot be positioned without a.)
A list cannot be accessed directly to a data item as an array (using the subscript) and needs to be positioned by the relationship between the data, that is, the next link that is referenced by the link point, and then the next until the data is accessed
The time complexity of inserting and deleting a link is O (1), because only the reference point must be changed
To find, delete, and insert after a specified node, these operations require an average of half of the nodes in the list to be searched, and the efficiency is O (N).
Single Linked list:
The "sequence of nodes" indicates that a linear table is called a linear list (single linked list).
is a chain-access data structure that holds data elements from a linear table in a set of storage units with arbitrary addresses. (This set of storage units can be continuous or discontinuous)
The structure of the chain node:

The data field in which the node value is stored; the pointer field (chain field) Next
that holds the reference to the node links the n nodes of the linear table in their logical order through the chain field of each node.
A linked list with only one chain field per node is called a single linked list, One Direction, and only a successor node reference

/** * Single linked list: Head Insert Method LIFO * The left of the linked 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 side of the list as a fixed, linked lists continue to extend to the left.    * The first to get the first point is the end of the node * @author stone/public class Singlelinkedlist<t> {private link<t>; 
  The first node public singlelinkedlist () {} is public Boolean IsEmpty () {return a null; 
    The public void Insertfirst (T data) {//inserted into the chain header link<t> NewLink = new link<t> (data); Newlink.next = A; 
  The next point of the new node points to the previous node first = NewLink; 
    Public link<t> Deletefirst () {//delete chain head link<t> temp = first; i = First.next; 
  Change the first node to return temp for the next node; 
    Public link<t> Find (T-t) {link<t> find = i; 
      While [find!= null] {if (!find.data.equals (t)) {find = Find.next; 
      } else {break; 
  } return find; 
    Public link<t> Delete (T-t) {if (IsEmpty ()) {return null; } else {if (First.data.equals (T)) {link<t> temp = i; i = First.next; 
      Change the first node to return temp for the next node; 
    } link<t> p = i; 
    link<t> q = A; 
      while (!p.data.equals (t)) {if (P.next = = null) {//indicates that return null was not found at the end of the chain; 
        else {q = p; 
      p = p.next; 
    } q.next = P.next; 
  return p; 
    public void Displaylist () {//Traversal System.out.println ("List (first-->last):"); 
    link<t> current = i; 
      while (current!= null) {Current.displaylink (); 
    current = Current.next; 
    The public void Displaylistreverse () {//Reverse sequence traversal link<t> p = A, q = First.next, T; while (q!= null) {//pointer reverse, traversal data order backwards t = Q.next;//NO3 if (p = = first) {//When the head is the original header. Next should be empty P.nex 
      t = null; } Q.next = p;//no3-> no1 pointer p = q; The start is reverse q = t; NO3 start}//In the loop aboveIf, the First.next is empty, and when q is null and does not perform the loop, p is the oldest and one data item, and then the P is assigned to the top = p; 
  Displaylist ();   Class Link<t> {//Chain node T data; data field link<t> next; 
    Subsequent pointers, 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&lt 
    ;integer> (); 
    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 (24)); 
    List.displaylist (); 
 SYSTEM.OUT.PRINTLN ("----reverse----");   List.displaylistreverse (); 
 } 
}

Print

List (first-->last): The data is the "data is" the "data is" the "data is" 
33
   
    list (first-->last): The data is the ' data is ' the ' data is ' the ' data is ' 
find:null 
Find:linked_list. SINGLELINKEDLIST$LINK@4B71BBC9 
Delete find:null 
delete find:linked_list. Singlelinkedlist$link@17dfafd1 
List (first-->last): The data is the "data is" the 
data is 
33< c19/>----Reverse---- 
List (first-->last): The data is the "data is" the data is 
22 


   

Single linked list: tail interpolation, LIFO-if the left side of the list is fixed, the linked list continues to extend to the right, this method of establishing a linked list is called the tail interpolation method.
When the tail-inserting method establishes the linked list, the head pointer is fixed, so a tail pointer must be set up, extending to the right of the linked list.
The first point in the tail-inserting method is the head node.

public class Singlelinkedlist2<t> {private link<t> head; 
  The first node public SingleLinkedList2 () {} public Boolean IsEmpty () {return head = = NULL; 
    The public void Insertlast (T data) {////At the end of the chain inserts link<t> NewLink = new link<t> (data); 
      if (head!= null) {link<t> NEXTP = Head.next; 
      if (NEXTP = = null) {Head.next = NewLink; 
        else {link<t> rear = null; 
          while (NEXTP!= null) {rear = NEXTP; 
        NEXTP = Nextp.next; 
      } rear.next = NewLink; 
    } else {head = NewLink; 
    } public link<t> Deletelast () {//delete chain tail link<t> p = head; 
    link<t> q = head; 
      while (P.next!= null) the next node of {//P is not empty, Q equals the current p (that is, Q is the previous, p is next) the end of the loop, Q equals the penultimate q = P of the chain tail; 
    p = p.next; 
    }//delete q.next = null; 
  return p; Public link<t> find (T t) {Link<t> 
    find = head; 
      While [find!= null] {if (!find.data.equals (t)) {find = Find.next; 
      } else {break; 
  } return find; 
    Public link<t> Delete (T-t) {if (IsEmpty ()) {return null; 
        else {if (head.data.equals (t)) {link<t> temp = head; head = Head.next; 
      Change the first node to return temp for the next node; 
    } link<t> p = head; 
    link<t> q = head; 
      while (!p.data.equals (t)) {if (P.next = = null) {//indicates that return null was not found at the end of the chain; 
        else {q = p; 
      p = p.next; 
    } q.next = P.next; 
  return p; 
    public void Displaylist () {//Traversal System.out.println ("List (head-->last):"); 
    link<t> current = head; 
      while (current!= null) {Current.displaylink (); 
    current = Current.next; } public void Displaylistreverse () {//Reverse sequence traversal link<t> p = head, q = Head.next, t;  while (q!= null) {//pointer reverse, traversing the data sequence backwards t = q.next;//NO3 if (p = = head) {//when for the original header. Next should be empty P.next 
      = NULL; } Q.next = p;//no3-> no1 pointer p = q; The start is reverse q = t;  
    NO3 start}//In the IF in the loop above, the Head.next is empty, and when q is null and does not perform a loop, p is the oldest and one data item, and the P is assigned to head Head = p after the inversion; 
  Displaylist ();   Class Link<t> {//Chain node T data; data field link<t> next; 
    Subsequent pointers, 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& 
    Lt;integer> (); 
    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 the "data is" the "Data is" List (head-->last): The data is the ' data is ' the ' data is ' 
find:null 
fi Nd:linked_list. SINGLELINKEDLIST2$LINK@4B71BBC9 
Delete find:null 
delete find:linked_list. Singlelinkedlist2$link@17dfafd1 
List (head-->last): The data is the "data is" 
22< c19/>----Reverse---- 
List (head-->last): The data is the ' data is ' 
33 

Simulate a two-terminal list to implement stacks and queues in a linked list
double-end linked list:
A two-terminal list is very similar to a traditional one. Just added a property-that is, a reference to the last linked node rear
This can be very easy to insert at the end of the chain, just change the rear next to the new node, without having to iterate over the last one.
So there are Insertfirst, insertlast
When you delete a chain, you just need to change the reference point, and when you delete the end of the chain, you have to empty the next second node.
And none of the references point to it, so you still need to loop to read the operation

/** * Double-end linked list * @author Stone * * public class Twoendpointlist<t> {private link<t> head;   First node private link<t> rear; Tail pointer public twoendpointlist () {} public T Peekhead () {if (head!= null) {return to He 
    Ad.data; 
  return null; 
  public Boolean IsEmpty () {return head = = NULL; 
    The public void Insertfirst (T data) {//inserted into the chain header link<t> NewLink = new link<t> (data); Newlink.next = head; 
  The next point of the new node points to the previous node head = NewLink; 
    The public void Insertlast (T data) {////At the end of the chain inserts link<t> NewLink = new link<t> (data); 
    if (head = = NULL) {rear = null; 
    } if (rear!= null) {rear.next = NewLink; 
      else {head = NewLink; 
    Head.next = rear; } rear = NewLink; 
    The next time you insert, insert the} public T Deletehead () {//delete the chain header if (IsEmpty ()) return null from rear. 
    link<t> temp = head; head = Head.next; Change the first node to the next node if (head = = null) {<span style= "white-space:pre" > </span>rear = Heads; 
  return temp.data; 
    Public t find (t-t) {if (IsEmpty ()) {return null; 
    link<t> find = head; 
      While [find!= null] {if (!find.data.equals (t)) {find = Find.next; 
      } else {break; 
    } if (find = = null) {return null; 
  return find.data; 
    The public t Delete (T-t) {if (IsEmpty ()) {return null; 
        else {if (head.data.equals (t)) {link<t> temp = head; head = Head.next; 
      Change the first node to return temp.data for the next node; 
    } link<t> p = head; 
    link<t> q = head; 
      while (!p.data.equals (t)) {if (P.next = = null) {//indicates that return null was not found at the end of the chain; 
        else {q = p; 
      p = p.next; 
    } q.next = P.next; 
  return p.data; } public void DIsplaylist () {//Traverse System.out.println ("List (head-->last):"); 
    link<t> current = head; 
      while (current!= null) {Current.displaylink (); 
    current = Current.next; 
    The public void Displaylistreverse () {//Reverse sequence traversal if (IsEmpty ()) {return; 
    } link<t> p = head, q = Head.next, T;  while (q!= null) {//pointer reverse, traversing the data sequence backwards t = q.next;//NO3 if (p = = head) {//when for the original header. Next should be empty P.next 
      = NULL; } Q.next = p;//no3-> no1 pointer p = q; The start is reverse q = t;  
    NO3 start}//In the IF in the loop above, the Head.next is empty, and when q is null and does not perform a loop, p is the oldest and one data item, and the P is assigned to head Head = p after the inversion; 
  Displaylist ();   Class Link<t> {//Chain node T data; data field link<t> next; 
    Subsequent pointers, 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) {twoendpointlist<integer> list = new twoendpointlist<integer> (); 
    List.insertlast (1); 
    List.insertfirst (2); 
    List.insertlast (3); 
    List.insertfirst (4); 
    List.insertlast (5); 
     
    List.displaylist (); 
    List.deletehead (); 
     
    List.displaylist (); 
    System.out.println ("Find:" + list.find (6)); 
 
    System.out.println ("Find:" + list.find (3)); 
    System.out.println ("Delete find:" + list.delete (6)); 
    System.out.println ("Delete find:" + list.delete (5)); 
    List.displaylist (); 
    SYSTEM.OUT.PRINTLN ("----reverse----"); 
  List.displaylistreverse (); 
 } 
}

Print

List (Head-->last): The data is 4 the ' data is 2 the ' data is 1 ' is 3 the ' data is 
5 
List (head-->last): The data is 2 the data was 
1 The data is 3 the data is 
5 
find:null 
find:3
   
    delete find:null 
Delete find:5 
List (head-->last): The data is 2 the data was 
1 The data is 
3< c19/>----Reverse---- 
List (head-->last): The data is 3 the data was 
1 The data is 
2 


   

Using the linked list to achieve the stack, with a single linked list can be realized,
This class uses a two-terminal linked list to achieve:

public class Linkstack<t> {private twoendpointlist<t> datas; 
  Public Linkstack () {datas = new twoendpointlist<t> (); 
  }//into stack public void push (T data) {Datas.insertfirst (data); 
  }//out Stack public T pop () {return datas.deletehead (); 
  //view stack Top Public T peek () {return datas.peekhead (); 
  }//Stack is null public boolean IsEmpty () {return datas.isempty (); 
    public static void Main (string[] args) {linkstack<integer> stack = new linkstack<integer> (); 
    for (int i = 0; i < 5; i++) {Stack.push (i); 
      for (int i = 0; i < 5; i++) {Integer peek = Stack.peek (); 
    System.out.println ("Peek:" + peek); 
      for (int i = 0; i < 6; i++) {Integer pop = Stack.pop (); 
    System.out.println ("Pop:" + Pop); 
    } System.out.println ("----"); 
    for (int i = 5; i > 0; i--) {Stack.push (i); for (iNT i = 5; i > 0; 
      i--) {Integer peek = Stack.peek (); 
    System.out.println ("Peek:" + peek); 
      for (int i = 5; i > 0; i--) {Integer pop = Stack.pop (); 
    System.out.println ("Pop:" + Pop); 
 } 
  } 
}

Print

Peek:4 
peek:4 
peek:4 peek:4 peek:4 pop:4 pop:3 pop:2 pop:1 
pop: Null 
---- 
peek:1 
peek:1 
peek:1 
peek:1 
peek:1 
pop:1 pop:2 pop:3 
Pop:4 
Pop:5 

Linked list implementation queue with two-terminal linked list to achieve:

public class Linkqueue<t> {private twoendpointlist<t> list; 
  Public Linkqueue () {list = new twoendpointlist<t> (); 
  ///Insert team tail public void Insert (T data) {list.insertlast (data); 
  //Remove Team Head public T-Remove () {return list.deletehead (); 
  //View Team head public T-Peek () {return list.peekhead (); 
  public Boolean IsEmpty () {return list.isempty (); 
    public static void Main (string[] args) {linkqueue<integer> queue = new linkqueue<integer> (); 
    for (int i = 1; i < 5; i++) {Queue.insert (i); 
      for (int i = 1; i < 5; i++) {Integer peek = Queue.peek (); 
    System.out.println ("Peek:" + peek); 
      for (int i = 1; i < 5; i++) {Integer remove = Queue.remove (); 
    System.out.println ("Remove:" + Remove); 
     
    } System.out.println ("----"); 
    for (int i = 5; i > 0; i--) {Queue.insert (i); for (int i = 5; i > 0; 
      i--) {Integer peek = Queue.peek (); 
    System.out.println ("PEEK2:" + peek); 
      for (int i = 5; i > 0; i--) {Integer remove = Queue.remove (); 
    System.out.println ("Remove:" + Remove); 
 } 
  } 
}

Print

Peek:1 
peek:1 
peek:1 
peek:1 remove:1 remove:2 remove:3 remove:4 
---- 
PEEK2 : 5 
peek2:5 
peek2:5 
peek2:5 
peek2:5 
remove:5 
remove:4 remove:3 remove:2 
remove:1 

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.