Sophomore learned the knowledge of data structure, but at that time with the C language, a lot of data structures need to achieve their own, still remember to take the pointer to the two-way linked list and two-tree of the kind of pain, then, because there is no practical application, so always in doubt, learning data structure is for what?
Then the junior study of Java, and Java has written some code, it seems that the data structure in the class is still not used in the actual, then, the original study of the data structure, exactly where?
Later in the Java interview, talk about a lot of interview will ask the difference between ArrayList and LinkedList, and then their own Baidu a bit, suddenly, the original data structure in Java every day in use, but with the others packaged good class.
Well, I can certainly write these data structures, the simple kind ....
--------------------------------------------------------------------------------------------------------------- ------------
①arraylist:
Packagetest;Importjava.util.ArrayList; Public classArraylisttest<e> { //Record Array length Private Static intmax=10; //Save Data Private StaticObject[] Values=NewObject[max]; //Save current Array length Private Static intLen=0; Public voidAdd (E value) {if(len<max) {Values[len]=value; Len++; }Else{Max=max+10; Values=NewObject[max]; } } Public BooleanRemove (E value) {object[] values2=NewObject[len-1]; intBeremovednum=-1; for(inti=0;i<len;i++) { if(values[i]==value| |values[i].equals (value)) {Beremovednum=i; Break; } } if(Beremovednum!=-1&&beremovednum!= (len-1)) { for(inti=0;i<beremovednum;i++) {Values2[i]=Values[i]; } for(inti1=beremovednum;i1<len-1;i1++) {VALUES2[I1]=values[i1+1]; } Len--; Values=Values2; return true; }Else if(beremovednum!=-1&&beremovednum== (len-1)){ for(inti=0;i<beremovednum;i++) {Values2[i]=Values[i]; } Len--; if(len==0) {Values=NewObject[max]; return true; } Values=Values2; return true; }Else{System.out.println ("Without this data, delete failed!" "); return false; }} @Override PublicString toString () {if(len>0) {String arr="["; for(inti=0;i<len-1;i++) {arr=arr+values[i]+ ","; } arr=arr+values[len-1]+ "]"; returnarr; } return"[NULL]"; } //Test Public Static voidMain (string[] args) {arraylisttest<Integer> alt=NewArraylisttest<integer>(); Alt.add (1); SYSTEM.OUT.PRINTLN (ALT); Alt.remove (1); SYSTEM.OUT.PRINTLN (ALT); Alt.add (1); Alt.add (1); Alt.add (1); Alt.add (4); Alt.add (4); Alt.add (5); SYSTEM.OUT.PRINTLN (ALT); Alt.remove (5); SYSTEM.OUT.PRINTLN (ALT); Alt.remove (100); SYSTEM.OUT.PRINTLN (ALT); }}
View Code
②linkedlist:
1 Packagelinkedlisttest;2 3 Public classLinkedlisttest<e> {4 PrivateNode<object> tail=NewNode<object>();5 PrivateNode<object> head=NewNode<object>();6 7 Publiclinkedlisttest () {8 Super();9Head.setdata (NULL);Ten head.setnext (tail); OneTail.setdata (NULL); ATail.setnext (NULL); - } - the @Override - PublicString toString () { -Node<object> node=NewNode<object>(); -Node=Head.getnext (); +String str= "["; - while(Node.getnext (). GetData ()! =NULL) { +Str=str+node.getdata () + ","; ASystem.out.println (Node.getdata () + "-" +Node.getnext ()); atNode=Node.getnext (); - } -Str=str+node.getdata () + "]"; - returnstr; - } - in Public voidAppend (E value) { -Node<object> node=NewNode<object>(); to Tail.setdata (value); + tail.setnext (node); -Tail=node; the } * $ Public Static voidMain (string[] args) {Panax NotoginsengLinkedlisttest<integer> llt=NewLinkedlisttest<integer>(); -Llt.append (1); theLlt.append (2); +Llt.append (3); A System.out.println (LLT); the } +}
linkedlisttest
Java Data structure