Java implementation of "algorithm data structure Java implementation" single-link list

Source: Internet
Author: User

1. Background

Single-linked list is the most basic data structure, carefully watched for a long time finally figured out, the difference is not each part, each chain is a node object. Requires two parameter positioning: One is index, which indicates the orientation of the object. The other is the object of node.



2. Code
Node class
public class Node {protected node next; protected int data, public Node (int data) {this.data=data;} public void Display ( {     System.out.print (data+ "");}   }

ArrayList class
public class myArrayList {public node first;//define head node private int pos=0;//node location public myarraylist () {//      This.first=null;        }//Insert a header node public void Addfirstnode (int data) {Node node=new node (data);        Node.next=first;      First=node;        }//Delete head node public node Deletefirstnode () {node tempnode=first;        First=tempnode.next;      return tempnode;             }//Insert node at any location insert public void add after index (int index, int data) {node node = new node (data);             Node current = first;              Node previous = first;                 while (pos! = index) {previous = current; Current = current.                  Next             pos++; } node.             Next = current; Previous.              Next = node;        pos = 0;             }//delete nodes at any location public node Deletebypos (int index) {node current = first;        Node previous = first;      while (pos! = index) {pos++;                 previous = current; Current = current.             Next             } if (current = = first) {first = first. Next;                 } else {pos = 0; Previous. Next = current.             Next        } return current;             } public void Displayallnodes () {Node current = first;              while (current = null) {Current.display ();              System.out.println (); Current = current.            Next }                 }        }


The main function of the implementation:
public class Main {public   static void Main (String args[]) {   myarraylist ls=new myarraylist ();        Ls.addfirstnode (a);      Ls.addfirstnode (+);   Ls.add (1, 144);   Ls.add (2, +);   Ls.deletebypos (1);  Ls.displayallnodes ();      }}



Implementation results:

16

44

15



Reference: http://blog.csdn.net/tayanxunhua/article/details/11100097

/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/


Java implementation of "algorithm data structure Java implementation" single-link list

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.