Java Algorithm (sequence table operation instance)

Source: Internet
Author: User

Java Algorithm (sequence table operation instance)

The source code is as follows:

Package com. Xu. Main;

Import java. util. collections;

Public class p2_1 {

/**
* @ Function: sequence table operation instance
* @ Author:
* @ Date: 2012-10-15
*/
Public static void main (string [] ARGs ){

Int I;
Sltype SL = new sltype (); // defines the sequence table variable
Data pdata; // defines the node to save the referenced variable
String key; // Save the keyword

System. Out. println ("sequence table operation demo! ");

SL. slinit (SL); // initialize the sequence table
System. Out. println ("initialization sequence table completed ");

Wrote input = new partition (system. In );

Do {
System. Out. println ("Enter the added node (learn name age ):");
Data DATA = new data ();
Data. Key = input. Next ();
Data. Name = input. Next ();
Data. Age = input. nextint ();

If (data. Age! = 0 ){
If (SL. sladd (SL, data) = 0) // If node addition fails
{
Break; // exit the endless loop
}
} Else // if the age is 0
{
Break; // exit the endless loop
}

} While (true );
System. Out. println ("the node sequence in the sequence table is :");
SL. slall (SL); // display data of all nodes

System. Out. println ("number of nodes to be retrieved :");
I = input. nextint (); // serial number of the input node
Pdata = SL. slfindbynum (SL, I); // search nodes by serial number
If (pdata! = NULL ){
System. Out. printf ("Node % d is :( % s, % s, % d) \ n", I, pdata. Key, pdata. Name,
Pdata. Age );
}

System. Out. println ("keyword of the node to be searched :");
Key = input. Next (); // enter the keyword
I = SL. slfindbycount (SL, key); // search by keyword
Pdata = SL. slfindbynum (SL, I); // search by serial number, return node reference
If (pdata! = NULL ){
System. Out. printf ("Node % d is :( % s, % s, % d) \ n", I, pdata. Key, pdata. Name,
Pdata. Age );
}
}
}

Class data {
String key; // node keyword
String name;
Int age;
}

Class sltype // defines the structure array of the sequence table
{
Static final int maxlename = 100;
Data [] listdata = new data [maxlen + 1]; // Save the structure array of the sequence table
Int listlen; // number of existing nodes in the sequence table

Void slinit (sltype SL) // initialize the sequence table
{
SL. listlen = 0; // empty table initialized
}

Int sllength (sltype SL ){
Return SL. listlen; // returns the number of elements in the sequence table.
}

Int slinsert (sltype SL, int N, data ){
Int I;
If (SL. listlen> = maxlen) // The number of sequential table nodes exceeds the maximum number.
{
System. Out. println ("the sequence table is full and cannot be inserted to nodes! ");
Return 0;
}
If (n <1 | n> SL. listlen-1) // The inserted node sequence number is incorrect.
{
System. Out. println ("the sequence number of the inserted element is incorrect. The element cannot be inserted! ");
Return 0;
}
For (I = SL. listlen; I> = N; I --) // move the data in the sequence table backward.
{
SL. listdata [I + 1] = SL. listdata [I];
}
SL. listdata [N] = data; // insert a node
SL. listlen ++; // The number of nodes in the ordered table increases by 1.
Return 1; // insert successfully, return 1
}

Int sladd (sltype SL, data) // add elements to the end of the sequence table
{
If (SL. listlen> = maxlen) // the sequence table is full.
{
System. Out. println ("the sequence table is full and cannot be inserted to nodes! ");
Return 0;
}
SL. listdata [++ SL. listlen] = data;
Return 1;
}

Int sldelete (sltype SL, int N) // Delete the data element in the sequence table
{
Int I;
If (n <1 | n> SL. listlen + 1) // the sequence number of the deleted node is incorrect.
{
System. Out. println ("An error occurred while deleting the node number. You cannot delete the element! ");
Return 0;
}
For (I = N; I <SL. listlen; I ++) // move the data in the sequence table forward
{
SL. listdata [I] = SL. listdata [I + 1];
}
SL. listlen --; // The number of ordered elements minus 1
Return 1; // Delete successfully, return 1
}

Data slfindbynum (sltype SL, int N) // return data elements based on the serial number
{
If (n <1 | n> SL. listlen + 1 ){
System. Out. println ("the node number is incorrect. The node cannot be returned! ");
Return NULL;
}
Return SL. listdata [N];

}

Int slfindbycount (sltype SL, string key) // query nodes by keyword
{
Int I;
For (I = 1; I <= SL. listlen; I ++ ){
If (SL. listdata [I]. Key. compareto (key) = 0) // if the required node is found
{
Return I;
}
}
Return 0; // if the entire table is not found after searching, 0 is returned.
}

Int slall (sltype SL) // display all nodes in the sequence table
{
Int I;
For (I = 1; I <= SL. listlen; I ++ ){
System. Out. printf ("(% s, % s, % d) \ n", SL. listdata [I]. Key,
SL. listdata [I]. Name, SL. listdata [I]. Age );
}
Return 0;
}
}

Running result:

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.