Implement a linked list linear table using Java

Source: Internet
Author: User

Public class linklist <t> {

Public class node {
Private t data;
Private node next;
Public node (){

}
Public node (t data, node next ){
This. Data = data;
This. Next = next;
}
}

Private node header;
Private node tail;
Private int size;

Public linklist (){
Header = NULL;
Tail = NULL;
}
Public linklist (t element ){
Header = new node (element, null );
Tail = header;
Size ++;
}
Public int length (){
Return size;
}
Public t get (INT index ){
Return getnodebyindex (INDEX). Data;
}
Private node getnodebyindex (INT index ){
// Todo auto-generated method stub
If (index <0 | index> size-1 ){
Throw new indexoutofboundsexception ("linear table Index out of bounds ");
}
Node current = header;
For (INT I = 0; I <size & current! = NULL; I ++, current = current. Next ){
If (I = index ){
Return Current;
}
}
Return NULL;
}
Public int locate (t element ){
Node current = header;
For (INT I = 0; I <size & current! = NULL; I ++, current = current. Next ){
If (current. Data = element ){
Return I;
}
}
Return-1;
}
Public void insert (t element, int index ){
If (index <0 | index> size ){
Throw new indexoutofboundsexception ("linear table Index out of bounds ");
}
If (header = NULL ){
Add (element );
} Else {
If (Index = 0 ){
Addatheader (element );
} Else {
Node Prev = getnodebyindex (index-1 );
Prev. Next = new node (element, Prev. Next );
Size ++;
}
}
}
// Add a new node to the linked list using the tail plug method
Private void add (t element ){
// Todo auto-generated method stub
If (header = NULL ){
Header = new node (element, null );
Tail = header;
} Else {
Node newnode = new node (element, null );
Tail. Next = newnode;
Tail = newnode;
}
Size ++;
}
// Add a new node to the linked list using the header Insertion Method
Private void addatheader (t element ){
Header = new node (element, header );
If (tail = NULL ){
Tail = header;
}
Size ++;
}
Public t Delete (INT index ){
If (index <0 | index> size-1 ){
Throw new indexoutofboundsexception ("linear table Index out of bounds ");
}
Node del = NULL;
If (Index = 0 ){
Del = header;
Header = header. Next;
} Else {
Node Prev = getnodebyindex (index-1 );
Del = Prev. Next;
Prev. Next = del. Next;
Del. Next = NULL;
}
Size --;
Return del. Data;
}
Public t remove (){
Return Delete (size-1 );
}
Public Boolean empty (){
Return size = 0;
}
Public void clear (){
Header = NULL;
Tail = NULL;
Size = 0;
}
Public String tostring (){
If (empty ()){
Return "[]";
} Else {
Stringbuilder sb = new stringbuilder ("[");
For (node current = header; current! = NULL; current = current. Next ){
SB. append (current. Data. tostring () + ",");
}
Int Len = sb. Length ();
Return sb. Delete (len-2, Len). append ("]"). tostring ();
}
}
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Linklist <string> List = new linklist <string> ();
List. insert ("AAA", 0 );
List. Add ("BBB ");
List. Add ("CCC ");
System. Out. println (list. tostring ());
List. insert ("DDD", 1 );
System. Out. println (list. tostring ());
List. Delete (2 );
System. Out. println (list. tostring ());
System. Out. println ("CCC position in the linked list:" + list. Locate ("CCC "));
System. Out. println ("element at index 2 in the linked list:" + list. Get (2 ));
}

}

Output result

[AAA, BBB, CCC]
[AAA, DDD, BBB, CCC]
[AAA, DDD, CCC]
CCC position in the linked list: 2
The element at index 2 in the linked list: CCC

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.