Java Algorithm (Linked List Operation instance)
Code:
Package com. Xu. Main;
Import java. util. collections;
Public class p3_1 {
/**
* @ Function: Linked List Operation instance
* @ Author:
* @ Date: 2012-10-15
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Cltype node, head = NULL;
Cltype Cl = new cltype ();
String key, findkey;
Wrote input = new partition (system. In );
System. Out. println ("linked list test. First, enter the data in the linked list in the format of: keyword name age ");
Do {
Data nodedata = new data ();
Nodedata. Key = input. Next ();
If (nodedata. Key. Equals ("0 "))
{
Break; // exit if the input is 0
}
Else
{
Nodedata. Name = input. Next ();
Nodedata. Age = input. nextint ();
Head = Cl. claddend (Head, nodedata); // Add a node at the end of the linked list
}
} While (true );
Cl. clallnode (head); // display all nodes
System. Out. println ("indicates the insertion node. Enter the keyword of the insertion position :");
Findkey = input. Next ();
System. Out. println ("Enter the data of the inserted node (keyword name age ):");
Data nodedata = new data ();
Nodedata. Key = input. Next ();
Nodedata. Name = input. Next ();
Nodedata. Age = input. nextint ();
Head = Cl. clinsertnode (Head, findkey, nodedata); // insert a node
Cl. clallnode (head); // display
System. Out. println ("demonstrate deleting a node. Enter the keyword to delete :");
Key = input. Next ();
Cl. cldeletenode (Head, key );
Cl. clallnode (head );
System. Out. println ("demonstrate searching in the linked list, and enter the Search Keyword :");
Key = input. Next ();
Node = Cl. clfindnode (Head, key );
If (node! = NULL)
{
Nodedata = node. nodedata;
System. out. printf ("Node corresponding to the keyword % s is (% s, % s, % d)", key, nodedata. key, nodedata. name, nodedata. age );
}
Else
{
System. Out. printf ("the node with the keyword % s is not found in the linked list! ", Key );
}
}
}
Class data {
String key; // keyword
String name;
Int age;
}
Class cltype // define the linked list Structure
{
Data nodedata = new data ();
Cltype nextnode;
@ Suppresswarnings ("UNUSED ")
Cltype claddend (cltype head, data nodedata) // append node
{
Cltype node, htemp;
If (node = new cltype () = NULL ){
System. Out. println ("memory application failed! ");
Return NULL;
} Else {
Node. nodedata = nodedata; // save data
Node. nextnode = NULL; // set the node reference to null, that is, the end of the table.
If (Head = NULL) // header reference
{
Head = node;
Return head;
}
Htemp = head;
While (htemp. nextnode! = NULL) // find the end of the linked list
{
Htemp = htemp. nextnode;
}
Htemp. nextnode = node;
Return head;
}
}
@ Suppresswarnings ("UNUSED ")
Cltype claddfirst (cltype head, data nodedata) // Add a node to the header Node
{
Cltype node;
If (node = new cltype () = NULL)
{
System. Out. println ("memory application failed! ");
Return NULL;
}
Else
{
Node. nodedata = nodedata; // save data
Node. nextnode = head; // point to the node indicated by the header reference
Head = node; // the header reference points to the new node.
Return head;
}
}
Cltype clfindnode (cltype head, string key) // find the node
{
Cltype htemp;
Htemp = head; // Save the reference of the linked list Header
While (htemp! = NULL) // If the node is valid, search
{
If (htemp. nodedata. Key. compareto (key) = 0) // If the node keyword is the same as the input keyword
{
Return htemp; // return this node reference
}
Htemp = htemp. nextnode; // process the next node
}
Return NULL;
}
@ Suppresswarnings ("UNUSED ")
Cltype clinsertnode (cltype head, string findkey, data nodedata) // insert a node
{
Cltype node, nodetemp;
If (node = new cltype () = NULL) // allocate memory node content
{
System. Out. println ("memory application failed! ");
Return NULL;
}
Node. nodedata = nodedata; // Save the data in the node
Nodetemp = clfindnode (Head, findkey );
If (nodetemp! = NULL) // If the node to be inserted is found
{
Node. nextnode = nodetemp. nextnode; // point the newly inserted node to the next node of the key node
Nodetemp. nextnode = node; // set the key node to point to the new inserted node.
}
Else
{
System. Out. println ("the correct insertion location is not found! ");
// Free (node); // releases the memory.
}
Return head;
}
Int cldeletenode (cltype head, string key) // delete a node
{
Cltype node, htemp;
Htemp = head;
Node = head;
While (htemp! = NULL)
{
If (htemp. nodedata. Key. compareto (key) = 0)
{
Node. nextnode = htemp. nextnode; // point the previous node to the next node of the current node.
// Free (htemp); // release the memory
Return 1;
}
Else
{
Node = htemp; // point to the current node
Htemp = htemp. nextnode; // point to the next node
}
}
Return 0;
}
Int cllength (cltype head) // calculate the length of the linked list
{
Cltype htemp;
Int Len = 0;
Htemp = head;
While (htemp! = NULL) // traverse the entire linked list
{
Len ++;
Htemp = htemp. nextnode; // process the next node
}
Return Len;
}
Void clallnode (cltype head) // display the linked list
{
Cltype htemp;
Data nodedata;
Htemp = head;
System. Out. printf ("the current linked list has a total of % d nodes. All data in the linked list is as follows: \ n ", cllength (head ));
While (htemp! = NULL) // cyclically processes each node of the linked list
{
Nodedata = htemp. nodedata;
System. Out. printf ("Node (% s, % s, % d) \ n", nodedata. Key, nodedata. Name, nodedata. Age );
Htemp = htemp. nextnode;
}
}
}
Running result: