JAVA Implementation of heterogeneous linked lists
The node types of the so-called heterogeneous linked list, that is, the linked list, can be different. I implemented four companies, one of which is defined as basecompany, and the other three inherit this base class. In addition, a list class is created to indicate a node class, which contains a head header node. The ID is 0. In this way, the four classes can be combined into a linked list in any order, and insertion, deletion, modification, and other methods can be implemented.
Company categories include:
Name: Company Name
Boss: company boss
Next: Target Company
ID: company ID
The output is as follows:
*****************************
The contents of creating a linked list are as follows:
*****************************
Company No. 0 name: null boss: NULL
Company Name: Innovation Works BOSS: Kai-fu Lee
Company Name: Tecent BOSS: Pony
3 Company Name: Baidu BOSS: Robbin
4 Company Name: Google BOSS: Larry Page
*****************************
Delete the following nodes:
*****************************
Deleted Node 1
Node 4 is deleted.
*****************************
Modify company 2's content
*****************************
Updated company name and boss
*****************************
The remaining nodes are as follows:
*****************************
Company No. 0 name: null boss: NULL
2 Company Name: Apple boss: jobs
3 Company Name: Baidu BOSS: Robbin
The specific implementation is as follows, and the program has detailed annotations.
Package com. zjw;
// Company base class
Class basecompany {
String name; // company name
String boss; // company boss
Int ID; // company ID
Basecompany next; // The object pointing to the next node
// Constructor
Basecompany (string name, string boss, int ID ){
This. Name = Name;
This. Boss = boss;
This. ID = ID;
}
// Method of pointing to the successor Node
Void next (basecompany next ){
This. Next = next;
}
// Display your own information
Void print (){
System. Out. println ("no." + ID + "company name:" + name + "BOSS:" + boss );
}
}
Package com. zjw;
// Subsidiary Class 1
Class companyone extends basecompany {
Companyone (string name, string boss, int ID ){
Super (name, boss, ID );
}
}
Package com. zjw;
// Subsidiary type 2
Class companytwo extends basecompany {
Companytwo (string name, string boss, int ID ){
Super (name, boss, ID );
}
}
Package com. zjw;
// Subsidiary category 3
Class companythree extends basecompany {
Companythree (string name, string boss, int ID ){
Super (name, boss, ID );
}
}
Package com. zjw;
// Linked list
Class list {
Basecompany head = new basecompany ("null", "null", 0); // header Node
Basecompany p1 = head; // node used for Traversal
Basecompany P2 = head; // node used for Traversal
Int size = 0; // the actual size of the linked list
// Node Insertion Method
Void insert (basecompany ){
P1.next (basecompany );
P1 = p1.next;
Size ++;
}
// Node modification method
Void Update (basecompany, string name, string boss ){
Basecompany. Name = Name;
Basecompany. Boss = boss;
System. Out. println ("updated" + basecompany. ID + "company name and boss ");
}
// Delete a node
Void Delete (basecompany ){
For (INT I = 0; I <size; I ++ ){
If (p2.next = basecompany ){
P2.next = basecompany. Next;
Basecompany. Next = NULL;
P2 = head;
Size --;
System. Out. println ("deleted" + basecompany. ID + "No. node ");
Break;
}
P2 = p2.next;
}
}
}
Package com. zjw;
// Test class
Public class test {
Public static void main (string [] ARGs ){
// Generate four company objects and the head node of the survival linked list
Basecompany one = new basecompany ("Innovation Works", "Kai-fu Lee", 1 );
Companyone two = new companyone ("Tecent", "Pony", 2 );
Companytwo three = new companytwo ("Baidu", "Robbin", 3 );
Companythree four = new companythree ("google", "Larry Page", 4 );
List list = new list ();
// Any four objects are made up of a linked list in order
List. insert (one );
List. insert (two );
List. insert (three );
List. insert (four );
// Print the content of the linked list cyclically
System. Out. println ("*****************************");
System. Out. println ("the contents of creating a linked list are as follows ");
System. Out. println ("*****************************");
Basecompany pp = List. head;
For (INT I = 0; I <= List. size; I ++ ){
Pp. Print ();
Pp = pp. Next;
}
// Delete any node
System. Out. println ("*****************************");
System. Out. println ("Delete the nodes below ");
System. Out. println ("*****************************");
List. Delete (one );
// List. Delete (two );
// List. Delete (three );
List. Delete (four );
// Modify company 2's content
System. Out. println ("*****************************");
System. Out. println ("Modify company 2 content information ");
System. Out. println ("*****************************");
List. Update (two, "apple", "Jobs ");
// Print the content of the remaining linked list cyclically
System. Out. println ("*****************************");
System. Out. println ("the remaining nodes are as follows ");
System. Out. println ("*****************************");
Basecompany PP1 = List. head;
For (INT I = 0; I <= List. size; I ++ ){
Pp1.print ();
PP1 = pp1.next;
}
}
}