Java implementation linked list

Source: Internet
Author: User

About the basic operation of the linked list in the data structure (C language Implementation) where the detailed description, now I will use Java to implement a single-linked list
It's not going to be a detailed explanation.

0. Write a node class to act as a node model.

/*      * Node in the list, data represents the content of the node, next refers to the reference     to the next node */    privateclass  node{         Private int data;         Private Node next;          Public Node (int  data) {                     this. Data= data;        }    }

1. Writing linklist

 Public class LinkedList {    privatestaticint  length;     Private // head node     Public LinkedList () {        length=0;        Head=null;    }

2. Insertion of nodes Here I use the head interpolation method and the tail interpolation method two kinds of operation

Head Insertion method

 /*   Insert node  */ public  Object AddHead ( int   obj)       {Node newhead  =new   Node (obj);  if  (Length==0 =newhead;  else   {newhead.next  =head;       Head  =newhead;           } length  ++;           return   obj; }   

Tail interpolation method

//inserting nodes using the tail interpolation method    Public voidAddNode (intobj) {Node NewNode=NewNode (obj); if(head==NULL) {Head=NewNode; return; } Node Temp=Head;  while(temp.next!=NULL) {Temp=temp.next;//Move back} temp.next=NewNode; Length++; }

3. Deletion of nodes

1 deleting elements in a table header

// /delete element   in table header  Public int Delhead () {      int obj=head.data;       Head=head.next;       Length--;  // length minus one    return obj;      }

2 delete the specified element, delete the first index node

   Public BooleanDelnode (intindex) {     if(index<1| | Index>length) {     return false; }    if(index==1) {Head=Head.next; Length--; return true; }    inti=2; Node previous=Head; Node Current=Previous.next;  while(current!=NULL) {        if(i==index) {Previous.next=Current.next; Length--; return true; } Previous=Current ; Current=Current.next; I++; }     return true;}

4. Link List traversal operation

  Public voidprintlist () {if(length>0) {Node temp=Head; intTempsize=length; if(tempsize==1) {System.out.print ("[" +temp.data+ "]"); return; }           while(tempsize>0) {              if(Temp.equals (head)) {System.out.print ("[" +temp.data+ "]"); }Else if(temp.next==NULL) {System.out.print (Temp.data+"]"); }Else{System.out.print (temp.data+ "-"); } temp=Temp.next; Tempsize--; }      }  }

Test code

 Public classlinkedlisttest{ Public Static voidMain (string[] args) {LinkedList list=NewLinkedList (); List.addhead (5); List.addhead (3); List.addhead (1); List.addnode (2); List.addnode (55); List.addnode (36); System.out.println ("Head.data:" +list.head.data);      List.printlist (); System.out.println ("List length is" +length); List.delnode (4); System.out.println ("After removing node 4");      List.printlist (); System.out.println ("The length of the linked list after deletion is" +length); }}

Output results

Here the first three nodes I used to insert the head interpolation method, the last three nodes using the tail interpolation method to insert
The code is very rough, if there are deficiencies please also indicate that the reprint is prohibited.

Java implementation 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.