Learning data structure with Java--single linked list

Source: Internet
Author: User
Tags count insert
Data | structure *


* Created on 2004-9-10


*


* Statement of node type in single linked list.


*/


package Org.arliang;


/**


* @author Li Liang


*


* nodes in a single linked list.


*/


public class Node


{


private int data; Storing Data


private node link; The next contact for the link.


public static void Main (String[]args)


{


}


/**


* @return Returns the data.


*/


public int getData ()


{


return data;


}


/**


* @param data


* The data to set.


*/


public void SetData (int data)


{


this.data = data;


}


/**


* @return Returns the link.


*/


Public node GetLink ()


{


return link;


}


/**


* @param link


* the link to set.


*/


public void Setlink (node link)


{


this.link = link;


}


/**


* @param linklist


of head node in
* Linked list

* @param K


* Position in the list


* @return The node found, if not found, then add null


*/


Public node Findnode (node linklist, int k)


{


int i = 1;


Node A;


//initialization, make a point to the first element, I is a counter.


a = Linklist.getlink ();


while (a!= null && i < k)


{


a = A.getlink ();


}


//gradually remove a number.


return A;


}


/**


* @param the head node
of the linklist linked list

* @param k Insert position, will insert
before this position

* @param elem the node to be inserted


* @return Success, successful return of 0


*/


public int Insertnode (node linklist, int k, node Elem)


{


Node A, b;


if (k = = 1)


{


Elem.setlink (linklist);


}


Else


{


a = Findnode (linklist, k-1);


if (a!= null)


{


B = A.getlink ();


A.setlink (Elem);


Elem.setlink (b);


}


Else


return-1;


}


return 0;


}


/**


* @param the head node
of the linklist linked list

* @param k position


* @return Success, successful return of 0


*/


public int Deletenode (node linklist, int k)


{


Node A, b;


if (k = = 1)


{


linklist.setlink (NULL);


}


Else


{


a = Findnode (linklist, k);


if (a!= null)


{


B = A.getlink ();


A.setlink (B.getlink ());


return-1;


}


}


return 0;


}


}


--------------------------------------------------------------------------------------





/*


* Founder of the People's Administrator


* Creation Date 2004-9-10


*


*/


package Org.arliang;


/**


* @author Administrator


*


* elected the leader. N players in a circle, starting from the first person in order to count 1,2,3.3 who reported out of the circle, and finally left in the circle of the people of the leader.


* Node number is starting from 0 instead of starting from 1.


*/


public class Selecthead


{





/**


* Create a linked list. Creates a list of a given number of lists and connects them to the beginning and end of the loop.


*


* @param n


* @return The first node of the list


*/


public node createlist (int n)


{


node Headnode = new node ();


int i = 1;


headnode.setdata (0); Head node


Node A = Headnode;


while (i < n)


{


Node B = new node ();


B.setdata (i);


A.setlink (b);


a = b;


i++; The loop cannot be lost, otherwise it becomes a dead loop


}


A.setlink (Headnode); form the Ring


return headnode;


}


/**


* Output The data in the current list


*


* @param the head node
of the linklist linked list

*/


public void Outputdata (node linklist)


{

After the
//formation of the ring to pay attention to identify the location of the head node, for only one node of the chain it may form a dead loop.


Node A;


a = linklist;


Node B = A.getlink ();


while (B.getdata ()!= a.getdata ())


{


//Because it is the ring, so judged, but the judgment is not strict


System.out.print (B.getdata ());


B = B.getlink (); Get Next node


}


}


/**


* Execute the game


* @param the head node
of the linklist linked list

* @param n The total number of the linked list


*/


public void Play (node linklist, int n)


{


int k;


Node A = linklist;


Node B;


k = n;


while (k > 1)


{


//After a node is down from the current node, the current count should be 2


a = A.getlink (); The node should be numbered 2


B = A.getlink (); The count of this node should be 3, to remove


A.setlink (B.getlink ()); Delete the B node.


a = A.getlink (); Swap the current node for the node after the deletion node.


k--; A node has been deleted


}


System.out.print ("result" + integer.tostring (A.getdata ()));


//Output last still in the loop node


}


/**


* @param args


*/


public static void Main (String[]args)


{


selecthead sh = new Selecthead (); Create Object


Node A = sh.createlist (9); Create a linked list ring


Sh.outputdata (a); View Data


Sh.play (A, 9); Execute the game.


}


}














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.