Java Data Structure-linear table merging and java data structure merging
Question: we know that the data elements in the linear table LA and LB are arranged in a descending order by value. Now we need to combine la and lb into a new linear table LC, the data elements in LC are still arranged in a descending order by values. For example, Set
LA = (3, 5, 8, 11)
LB = (, 15, 20)
Then
LC = (, 20)
For linear tables, see the previous chapter. Here we use the sequential storage structure of linear tables.
Code:
Package com. yds. list; public class JavaMain {private static void MergeList (SequenceList <Integer> listA, SequenceList <Integer> listB, SequenceList <Integer> listC) {// data elements in the known linear table La and Lb are arranged in non-decreasing order by value // The new linear table Lc is obtained by combining La AND Lb, the data elements of Lc are also arranged in a non-descending order of int La_length = listA. length (); int Lb_length = listB. length (); int I = 0, j = 0; while (I <La_length & j <Lb_length) {if (listA. get (I) <listB. get (j) {listC. add (listA. get (I); I ++;} else {listC. add (listB. get (j); j ++ ;}} while (I <La_length) {listC. add (listA. get (I); I ++;} while (j <Lb_length) {listC. add (listB. get (j); j ++ ;}} public static void main (String [] args) {// TODO Auto-generated method stub SequenceList <Integer> listA = new SequenceList <Integer> (); SequenceList <Integer> listB = new SequenceList <Integer> (); sequenceList <Integer> listC = new SequenceList <Integer> (); int [] la = {3, 5, 8, 11}; int [] lb = {2, 6, 8 }; for (int I = 0; I <la. length; I ++) {listA. add (la [I]) ;}for (int I = 0; I <lb. length; I ++) {listB. add (lb [I]);} MergeList (listA, listB, listC); for (int k = 0; k <listC. length (); k ++) {System. out. println (listC. get (k ));}}}
Result: