Simple generic single-chain table

Source: Internet
Author: User

I saw a single-chain table mentioned in the title of a post and suddenly remembered that I had never written the code for a single-chain table. I figured out the simplest one, generic is used to improve applicability.
Java code
Package com. iteye. bolide74.test;
 
Public class SingleLinkedList {
Public static void main (String [] args ){
List <Integer> list = new List <Integer> ();
List. add (1 );
List. add (2 );
List. add (3 );
List. add (4 );
List. add (5 );
Note <Integer> head = list. head;
While (head. hasNext ()){
System. out. println (head. value );
Head = head. next;
}
}
}
 
Class Note <E> {
Public E value;
Public Note <E> next;
 
Public Note (E val, Note <E> next ){
This. value = val;
This. next = next;
}
 
Public boolean hasNext (){
Return this. next! = Null;
}
}
 
Class List <E> {
Public Note <E> head;
 
Public List (){
This. head = new Note <E> (null, null );
}
 
Public void add (E val ){
This. head = new Note <E> (val, this. head );
}
}

Author: "Eight claw is me"
 

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.