List operation code is small but more error-prone, is more suitable for the interview place.
/** * Source Name: Mylinklist.java * Date: 2014-09-05 * program function: Java list operation * Copyright: [email protected] * A2bgeek */import Java.util.Sta Ck;public class Mylinklist {class Linknode<t> {private T mvalue;private linknode<t> mnext;public LinkNode (T V Alue) {//TODO auto-generated constructor stubmvalue = value;} public void SetValue (T value) {mvalue = value;} Public T GetValue () {return mvalue;} public void Setnext (linknode<t> next) {Mnext = next;} Public linknode<t> GetNext () {return mnext;}} Head nodelinknode<integer> mhead = null;int msize;//addpublic void Add (int value) {linknode<integer> NewNo de = new linknode<integer> (value); Newnode.setnext (null); if (mhead = = null) {mhead = NewNode;} else {Linknode<int eger> tmp = Mhead;while (tmp.getnext () = null) {TMP = Tmp.getnext ();} Tmp.setnext (NewNode);} msize++;} insertpublic void Insert (int index, int value) {linknode<integer> NewNode = new linknode<integer> (value); F (mhead = = null) {Newnode.setnext(null); mhead = newnode;msize++;} else {if (Index > MSize-1) {System.out.println ("Index out of Bound");} else {int j = 0; linknode<integer> tmp = Mhead;while (J < index) {tmp = Tmp.getnext (); j + +;} Newnode.setnext (Tmp.getnext ()); Tmp.setnext (newNode); msize++;}} deletepublic void Deletebyindex (int index) {if (Mhead = = null) {System.out.println ("linklist Empty");} else {if (index > mSize-1) {System.out.println ("Index out of Bound"),} else if (index = = 0) {Mhead = Mhead.getnext ();} else {int j = 0; linknode<integer> pretmp = Mhead;while (J < index-1) {pretmp = Pretmp.getnext (); j + +;} Pretmp.setnext (Pretmp.getnext (). GetNext ());}}} public void Deletebyvalue (int value) {if (Mhead = = null) {System.out.println ("linklist Empty");} else {Linknode<intege R> pretmp = null; linknode<integer> tmp = Mhead;while (tmp! = null) {if (Tmp.getvalue () = value) {pretmp = Tmp;tmp = Tmp.getnext ();} else {break;}} if (TMP = = null) {System.out.println ("not Found");} else if (preTMP = = null) {Mhead = Tmp.getnext ();} else {Pretmp.setnext (Tmp.getnext ());}}} reversepublic void reverse () {linknode<integer> tmp = Mhead; Linknode<integer> pretmp = null; linknode<integer> nexttmp = Null;while (tmp! = null) {nexttmp = Tmp.getnext (); Tmp.setnext (pretmp);p retmp = Tmp;tmp = Nexttmp;} Mhead = pretmp;} print_recursivepublic void Printr (linknode<integer> start) {if (start = = null) {return;} else {System.out.print ( Start.getvalue () + "");p Rintr (Start.getnext ());}} print_norecursivepublic void Printnor (linknode<integer> start) {if (start = = null) {SYSTEM.OUT.PRINTLN (" linklist empty ");} else {linknode<integer> tmp = start;while (tmp! = null) {System.out.print (Tmp.getvalue () + ""); tmp = Tmp.getnext (); }}}//printreverse_recursivepublic void Printrr (linknode<integer> start) {if (start = = null) {return;} else {printr R (Start.getnext ()); System.out.print (Start.getvalue () + "");}} printreverse_norecursivepublic void Printrnor (linknode<integer> start) {if (start = = null) {System.out.println ("linklist Empty");} else {stack<linknode<integer>& Gt stack = new stack<linknode<integer>> (); linknode<integer> tmp = Start;while (tmp! = null) {Stack.push (TMP); tmp = Tmp.getnext ();} while (!stack.isempty ()) {System.out.print (Stack.pop (). GetValue () + "");}} public static void Main (string[] args) {mylinklist mylinklist = new Mylinklist (); Mylinklist.add (1); Mylinklist.add (2); Mylinklist.add (3); Mylinklist.add (4); Mylinklist.add (5); Mylinklist.insert (3, 6); System.out.println ("linklist size is" + mylinklist.msize); Mylinklist.printr (Mylinklist.mhead); System.out.println (); mylinklist.deletebyindex (0); Mylinklist.printnor (Mylinklist.mhead); System.out.println ();//Mylinklist.reverse ();//MYLINKLIST.PRINTRR (Mylinklist.mhead);//System.out.println ();// Mylinklist.printrnor (Mylinklist.mhead);}}
"Data structures and Algorithms" Java linked list operations