Java - single key table

xiaoxiao2021-03-06  55

http://blog.9cbs.net/p96114/archive/2004/09/10/100617.aspx

/ *

* Created on 2004-9-10

*

* Node type declaration in single-strand table.

* /

Package org.arliang;

/ **

* @Author Li Liang

*

* Node in single-strand table.

* /

Public Class Node

{

Private int data; // store data

Private node link; // Link the next contact.

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

* The head knots in the list

* @Param K

* Location in the list

* @return found node, if not found, return NULL

* /

Public Node FindNode (Node Linklist, INT K)

{

INT i = 1;

Node a;

// When initialization, let a point to the first element, i is the counter.

a = linelist.getlink ();

While (a! = null && i

{

a = a.getlink ();

}

// Take a number of gradually.

Return A;

}

/ **

* @Param LinkList Linksheet

* @Param K Inserted, will insert it before this location

* @Param ELEM to insert the node

* @return is successful, successfully returns 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 LinkList Linksheet

* @Param K Location

* @return is successful, successfully returns 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;

}

}

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

/ *

* Created man administrator

* Create a date 2004-9-10

*

* /

Package org.arliang;

/ **

* @Author administrator

*

* Select the leader. N players surrounded by a circle, from the first person to start order 1, 2, 3. Anyone who reported to the circle and finally stayed in the circle as the leader.

* The node number starts from 0 instead of starting with 1.

* /

Public Class SelectHead

{

/ **

* Create a list. Create a given number of linked lists and connect their heads up to the formation ring.

*

* @Param n

* @Return's first patch of this list

* /

Public Node Createlist (int N)

{

Node headnode = new node ();

INT i = 1;

HeadNode.SetData (0); // head node

Node a = headnode;

While (i

{

Node b = new node ();

B.SetData (i);

A.SETLINK (B);

A = B;

i ; // This loop cannot be lost, otherwise become a dead cycle

}

A.SetLink (HeadNode); // forming a ring

Return HeadNode;

}

/ **

* Output data in the current list

*

* @Param LinkList Linksheet

* /

Public void outputdata (Node Linklist), PUBLIC VOID OUTPUTDATA

{

// Pay attention to the position of the identification of the head knot after forming the ring, which may form a dead cycle for only one chain of a node.

Node a;

a = linelist;

Node b = a.getlink ();

While (B.GetData ()! = a.getdata ())

{

// Due to the ring, it is reasonable to judge, but the judgment is not strict

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

B = B.GetLink (); // Get the next node

}

}

/ **

* Execute the game

* @Param LinkList Linksheet

* @Param n Total number of this list

* /

Public void Play (Node Linklist, Int N)

{

INT K;

Node a = linelist;

Node B;

K = N;

While (k> 1)

{

// After a node is taken down from the current node, the current number of reference should be 2

A = a.getLink (); // The number of reports of this node should be 2

B = a.GetLink (); // The number of reports should be 3, to delete

A.SETLINK (B.GetLink ()); // Remove the B node.

a = a.getLink (); // Replace the current node to the node after the node is deleted.

K -; // Delete a node

}

System.out.print ("Result" Integer.toString (A.Getdata ())));

/ / Output Node that is finally in the circle

}

/ **

* @Param Args

* /

Public static void main (string [] args)

{

SelectHead sh = new selecthead (); // Create an object

Node a = sh.createlist (9); // Create a chain loop

Sh.outputData (a); // View Data Sh.Play (a, 9); // Perform the game.

}

}

转载请注明原文地址:https://www.9cbs.com/read-117137.html

New Post(0)