Java linked list

Source: Internet
Author: User
Import Java. util. struct; Class data2 {string key; // key of the node string name; int age;} class cltype // defines the linked list Structure {data2 nodedata = new data2 (); cltype nextnode; // append node cltype claddend (cltype head, data2 nodedata) {cltype node, htemp; If (node = new cltype () = NULL) {system. out. print ("memory application failed! \ N "); return NULL; // memory allocation Failed} else {node. nodedata = nodedata; // Save the node data. nextnode = NULL; // set the node pointer to null, that is, if (Head = NULL) at the end of the table. // the header pointer {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 ;}// Add the header node cltype claddfirst (cltype head, data2 nodedata) {cltype node; If (node = new cltype ()) = NULL) {system. out. print ("memory application failed! \ N "); return NULL; // memory allocation Failed} else {node. nodedata = nodedata; // Save the node data. nextnode = head; // point to the header pointer pointing to the node head = node; // the header pointer pointing to the new node return head;} // search for the node cltype clfindnode (cltype head, string key) {cltype htemp; htemp = head; // Save the head pointer while (htemp! = NULL) // If the node is valid, search for {If (htemp. nodedata. key. compareto (key) = 0) // If the node keyword is the same as the input keyword {return htemp; // return the node pointer} htemp = htemp. nextnode; // process next node} return NULL; // return NULL pointer} // Insert the cltype clinsertnode (cltype head, string findkey, data2 nodedata) {cltype node, nodetemp; if (node = new cltype () = NULL) // allocate the content of the saving node {system. out. print ("memory application failed! \ N "); return NULL; // memory allocation Failed} node. nodedata = nodedata; // Save the data in the node nodetemp = clfindnode (Head, findkey); If (nodetemp! = NULL) // if you find the node {node to be inserted. nextnode = nodetemp. nextnode; // The New Insertion node points 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. print ("the correct insertion location is not found! \ N "); // free (node); // release memory} return head; // return head pointer} int cldeletenode (cltype head, string key) {cltype node, htemp; // node Save the previous node htemp = head; node = head; while (htemp! = NULL) {If (htemp. nodedata. key. compareto (key) = 0) // locate the keyword and perform the delete operation {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; // not deleted} 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 ++; // accumulate the number of nodes htemp = htemp. nextnode; // process next node} return Len; // number of returned nodes} void clallnode (cltype head) // Referer Link Table {cltype htemp; data2 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 process each node in the linked list {nodedata = htemp. nodedata; // obtain the node data system. out. printf ("Node (% s, % s, % d) \ n", nodedata. key, nodedata. name, nodedata. age); htemp = htemp. nextnode; // process the next node} public class complete list {public static void main (string [] ARGs) {cltype node, head = NULL; cltype Cl = new cltype (); string key, findkey; partition input = new partition (system. in); system. out. print ("linked list test. First, enter the data in the linked list in the format of: keyword name age \ n "); do {data2 nodedata = new data2 (); nodedata. key = input. next (); If (nodedata. key. equals ("0") {break; // If 0 is entered, exit} else {nodedata. name = input. next (); nodedata. age = input. nextint (); Head = Cl. claddend (Head, nodedata); // Add a node to the end of the linked list} while (true); CL. clallnode (head); // display all nodes system. out. printf ("\ n indicates the insertion node. Enter the keyword of the insertion position:"); findkey = input. next (); // enter the insert position keyword system. out. print ("Enter the data of the inserted node (keyword name age): "); Data2 nodedata = new data2 (); nodedata. key = input. next (); nodedata. name = input. next (); nodedata. age = input. nextint (); // enter the inserted node data head = Cl. clinsertnode (Head, findkey, nodedata); // call the insert function Cl. clallnode (head); // display all nodes system. out. print ("\ n shows how to delete a node. Enter the keyword to delete:"); Key = input. next (); // enter the delete node keyword Cl. cldeletenode (Head, key); // call the delete node function Cl. clallnode (head); // display all nodes system. out. printf ("\ n shows how to search in the linked list. Enter the Keyword:"); Key = input. n EXT (); // enter the search keyword node = Cl. clfindnode (Head, key); // call the search function and return the node pointer if (node! = NULL) // If the returned node pointer is valid {nodedata = node. nodedata; // obtain the node data system. out. printf ("Node corresponding to the keyword % s is (% s, % s, % d) \ n", key, nodedata. key, nodedata. name, nodedata. age);} else // If the node pointer is invalid {system. out. printf ("node with the keyword % s not found in the linked list! \ N ", key );}}}

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.