Use Java arrays to implement basic and self-scalable linked lists

Source: Internet
Author: User

Package com. Tongji. szx. base;

Import com. Tongji. szx. basedao. listinterface;

Public class extendlist <t> implements listinterface <t> {

Private T [] entry;
Private int length;
Private int capacity;
Private Static final int base_length = 10;

/**
* Default constructor
*/
Public extendlist (){
This (base_length );
}
@ Suppresswarnings ("unchecked ")
/**
* Provides an initial linked list length constructor.
*/
Public extendlist (INT intlength ){
If (intlength> 0 ){
This. Length = intlength;
This. capacity = intlength;
Entry = (T []) New object [intlength];
} Else {
This. Length = base_length;
This. capacity = base_length;
Entry = (T []) New object [base_length];
}
}
@ Override
/**
* Insert an element at the end of the linked list.
*/
Public Boolean add (T anentry ){
// Todo auto-generated method stub
Try {
If (length = capacity ){
Copyentry ();
}
Entry [length] = anentry;
Length ++;
Return true;
} Catch (exception e ){
Return false;
}
}

@ Suppresswarnings ("unchecked ")
Private void copyentry (){
T [] newentry = entry;
This. Capacity * = 2;
Entry = (T []) New object [This. capacity];
For (INT Index = 0; index <newentry. length; ++ index ){
Entry [Index] = newentry [Index];
}
}
@ Override
Public Boolean add (INT index, t anentry ){
// Todo auto-generated method stub
Try {
While (index> = capacity ){
Copyentry ();
}
If (index <length ){
Moveentry (index, anentry );
Length ++;
Return true;
} Else {
Entry [Index] = anentry;
// Length + = (INDEX );
Int sub = index-Length + 1;
Length + = sub;
Return true;
}
} Catch (exception e ){
Return false;
}
// Return false;
}

/**
* Move the element before the insert position is length.
* @ Param Index
* @ Param anentry
*/
Private Boolean moveentry (INT aposition, t anentry ){
If (aposition <length ){
For (INT Index = length-1; index> = aposition; ++ index ){
Entry [index + 1] = entry [Index];
}
Entry [aposition] = anentry;
Return true;
}
Return false;
}

@ Override
Public Boolean contains (T anentry ){
// Todo auto-generated method stub
For (T: entry ){
If (T. Equals (anentry )){
Return true;
}
}
Return false;
}

@ Override
Public t get (INT index ){
// Todo auto-generated method stub
If (index <length ){
Return entry [Index];
}
Return NULL;
}

@ Override
Public Boolean isempty (){
// Todo auto-generated method stub
Return (length = 0 );
}

@ Override
Public Boolean isfull (){
// Todo auto-generated method stub
Return (length = capacity );
// Return false;
}

@ Override
Public t remove (INT index ){
// Todo auto-generated method stub
If (index <length ){
T newentry = entry [Index];
For (INT I = index; I <length; ++ I ){
Entry [I] = entry [I + 1];
}
Length --;
Return newentry;
}
Return NULL;
}

@ Override
Public t Replace (INT index, t anentry ){
// Todo auto-generated method stub
If (index <length ){
T newentry = entry [Index];
Entry [Index] = anentry;
Return newentry;
}
Return NULL;
}
@ Override
Public int getlength (){
Return length;
}
Public int getcapacity (){
Return this. capacity;
}
Public void display (){
For (INT Index = 0; index <length; ++ index ){
If (entry [Index]! = NULL ){
System. Out. println (index + ":" + entry [Index]. tostring ());
} Else {
System. Out. println (index + ":" + "the object is empty! ");
}
}
}

Public static void main (string [] ARGs ){
System. Out. println ("===================== ");
Extendlist <string> Elst = new extendlist <string> (5 );
Elst. Add ("sunzhenxing ");
Elst. Add (10, "sunhailong ");

Elst. Display ();
System. Out. println (Elst. getlength ());
System. Out. println (Elst. getcapacity ());
Elst. Remove (10 );
System. Out. println (Elst. getlength ());
Elst. Display ();
}





}

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.