Java Data Structure-linked list (1): Single-Chain tables and related common operations

Source: Internet
Author: User

Java Data Structure-linked list (1): Single-Chain tables and related common operations

Package LinkList; public class Node
 
  
{Public T data; // data domain public Node next; // Node domain // default constructor public Node () {}// constructor with parameters, public Node (T data, Node next) {this. data = data; this. next = next;} // initialize public Node (Node next) {this. next = next;} // display the node value public void display () {System. out. print (data + "");}}
 

**************************************** ***************************** **************************************** ***

Package LinkList; /****************** single-chain table of the leading node and Its Implementation ************* * ********* @ author wl **/public class SinglyLinkList
 
  
{Node
  Head; // head Node int size; // linked table size Node
  Current; // current Node // Initialize an empty linked list public SinglyLinkList () {this. head = new Node
  (Null); this. size = 0; this. current = head;} // determines whether the linked list is empty. public boolean isEmpty () {return size = 0;} // print the public void traverse () {if (isEmpty () {System. out. println ("null");} else {for (Node
   P = head. next; p! = Null; p = p. next) {System. out. print (p. data + ",");} System. out. println () ;}}// Add the public void addFromHead (T value) {Node from the original Node
   
    
Node = new Node
    
     
(Value, null); node. next = head. next; head. next = node; size ++;} // Add the public void addFromTail (T value) {Node from the End node
     
      
Node = new Node
      
        (Value, null); this. current = head. next; if (current = null) {head. next = node; size ++;} else {while (current. next! = Null) {// locate the current node to the End Node current = current. next;} current. next = node; size ++ ;}}// Delete public void removeFromHead () {if (isEmpty () from the original node ()) {// determines whether the linked list is empty. throw new RuntimeException ("the linked list is empty");} current = head. next; // the current node head. next = current. next; current. next = null; size --;} // Delete public void removeFromTail () {if (isEmpty () from the End Node ()) {// determines whether the linked list is empty. throw new RuntimeException ("the linked list is empty");} Node
       Prev = null; // The previous node this. current = head. next; while (current. next! = Null) {// locate the current node to the End Node prev = current; current = current. next;} prev. next = null; size --;} // insert a public void insert (int index, T value) after the index {if (index <0 | index> size) {throw new RuntimeException ("Incorrect index parameter");} Node
       
         Node = new Node
        
          (Value, null); if (index = 0) {node. next = head. next; head. next = node; size ++;} else {int count = 0; // counter Node
         Prev = null; // The previous node current = head. next; while (current! = Null & count! = Index) {// locate the current node to the index node prev = current; current = current. next; count ++;} node. next = current; prev. next = node; size ++ ;}}// delete a public void remove (int index) {if (index <0 | index> size) node at the index position in any location) {throw new RuntimeException ("Incorrect index parameter");} if (index = 0) {current = head. next; head. next = current. next; size --;} else {int count = 0; // counter Node
         Prev = null; // The previous node current = head. next; while (current! = Null & count! = Index) {// locate the current node to the index node prev = current; current = current. next; count ++;} prev. next = current. next; current = null; size -- ;}// delete a node based on the value. When multiple identical values exist, only the first public void removeByValue (T value) is deleted) {if (isEmpty () {throw new RuntimeException ("Empty Linked List");} int count = 0; // counter Node
         Prev = null; // The previous node current = head. next; while (current! = Null & bytes t. data! = Value) {// locate the current node to the first node with the value of value, prev = current; current = current. next; count ++;} if (count> size) {throw new RuntimeException ("A node with a value does not exist");} prev. next = current. next; current = null; size --;}}
         

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.