Java implementation data Structure-linear table-sequential table, implement INSERT, find, delete, merge function

Source: Internet
Author: User

Package order table; Import Java.util.arraylist;import Java.util.scanner;public class OrderList {/** * @param args * @author Liu Yanbing * @2015-1-31 21:00 *//* * (The following so-called "position" is not an array subscript notation starting from 0, but a representation starting from 1.) * (for example, the data on position 2 in 12,13,14,15,16 data is 13) * * Using Java to implement data structure-linear table-sequential table * INSERT, find, delete, merge functions * insert: Enter position, return data value at location * Find: Enter the data value to find, return the data value in the Order table position * Delete: Enter the location to delete, return the deleted order table results * Merge : Enter two sets of data and combine the two sets of arrays in ascending results to output. * If there are duplicate values in two sets of data, the result is unique in the combined sequential table, guaranteed no duplicate value */public static void Main (string[] args) {//TODO auto-generated method Stubsystem.ou T.PRINTLN ("Input integer number as sequential table, enter-1 to end the input process:"); Scanner sc=new Scanner (system.in); int m=sc.nextint (); Arraylist<integer>list=new arraylist<integer> (); while (M!=-1) {list.add (M); M=sc.nextint ();} int a[]=new int[list.size ()];for (int i=0;i<list.size (); i++) {a[i]=list.get (i);} System.out.println ("******************** Please select an option to determine the action to be performed: *******************************************"); System.out.println ("******************** option--1: Insert data ******************************************* at the specified location"); System.out.println ("***********Option--2: Find the first occurrence of the data based on the data ****************************** "); System.out.println ("******************** option--3: Delete data from this location ******************************" according to the input location); System.out.println ("******************** option--4: Enter two sets of arrays and combine the two sets of arrays in ascending order *********************"); int n=sc.nextint () ; orderlist order=new orderlist (); switch (n) {case 1:{system.out.println ("Enter position to insert:"); int insertp=sc.nextint (); System.out.println ("Enter the value to insert in this position:"); int insertn=sc.nextint (); order. Listinsert (list, INSERTP, INSERTN); Case 2:{SYSTEM.OUT.PRINTLN ("Please enter data to find"); int searchp=sc.nextint (); System.out.println ("Where this data first appears is:"); SYSTEM.OUT.PRINTLN (order. Listseach (A,SEARCHP) + "number position"); Case 3:{SYSTEM.OUT.PRINTLN ("Please enter a location to delete:"); Scanner delete=new Scanner (system.in); int deletep=delete.nextint (); order. Listdelete (A, deletep); Case 4:{SYSTEM.OUT.PRINTLN ("Please enter the first array, press 1 to end the input"); Scanner sc1=new Scanner (system.in); int num1=sc1.nextint (); Arraylist<integer>list1=new arraylist<integer> (); while (Num1!=-1) {list1.add (NUM1); NUM1=SC1.nextInt ();} int la[]=new int[list1.size ()];for (int i=0;i<list1.size (); i++) {la[i]=list1.get (i);} System.out.println ("Please enter a second array, press 1 to end the input"); Scanner sc2=new Scanner (system.in); int num2=sc2.nextint (); Arraylist<integer>list2=new arraylist<integer> (); while (Num2!=-1) {list2.add (num2); Num2=sc2.nextint () ;} int lb[]=new int[list2.size ()];for (int i=0;i<list2.size (); i++) {lb[i]=list2.get (i);} Order. Listmerge (LA, LB); break;} DEFAULT:{SYSTEM.OUT.PRINTLN ("Wrong input!"); System.out.println (); System.out.print ("Please re"); Order.main (null); break;}}} /* * Insert method * Insertpoint: Insert position * Insertvalue: Insert value * ArrayList list: Pass input data Link list * int []a: Get list Linked list value and save in a array as result output */Publi c void Listinsert (ArrayList list,int insertpoint,int insertvalue) {int j;if (insertpoint<1| | Insertpoint>list.size ()) {System.out.println ("wrong insertion position!");} List.add (insertpoint-1,insertvalue); Object []a=new object[list.size ()];for (int i=0;i<list.size (); i++) {a[i]= List.get (i);} System.out.println ("Insert results are as follows:");//results eliminate the last value, the symbol for (int i=0;i<a.length;i++) {if (i==a.length) System.out.print (A[i]); ElseSystem.out.print (a[i]+ ",");}} /* * int []a: Pass the well-constructed array in the main () method * Searchpoint: The position of the numeric value to find */public int Listseach (int []a,int searchpoint) {int i=0; (I<=a.length&&a[i]!=searchpoint) {i++;} if (i<=a.length) return i+1;//returns the position, non-array subscript else return 0; }/* * int []a: Pass the well-constructed array in the main () method * Deletepoint: The location to be deleted */public void listdelete (int []a,int deletepoint) {int J; if (DE letepoint<1| | Deletepoint>a.length) {System.out.println ("Delete location error!")}//Move the data value after Deletepoint position forward for (j=deletepoint;j<a.length  ; j + +) {a[j-1]=a[j];} SYSTEM.OUT.PRINTLN ("The result of deleting data at the specified location is as follows:");//results eliminate the last value of the symbol//here specific not output array last value for (int i=0;i<a.length-1;i++) {if (i== a.length-2) System.out.print (A[i]); ElseSystem.out.print (a[i]+ ",");} }/* * int[] a,int []b: Two arrays for new input */public void listmerge (int []la,int []lb] {arraylist<integer> listc=new Arrayli St<integer> (); int i,j,t;  i=0;j=0; Simple sorting of array A for (i=0;i<la.length;i++) {for (j=0;j<la.length-i-1;j++) {if (la[j]>la[j+1]) {t=la[j]; la[j]=la[j+1]; la[j+1]=t;}}} Simple sorting of array b for (i=0;i<lb.length;i++) {for (j=0;j<lb.length-i-1;j++) {if (la[j]>lb[j+1]) {t=lb[j]; lb[j]=lb[ J+1]; lb[j+1]=t;  }}} i=0;j=0; while (i<la.length&&j<lb.length) {if (La[i]<lb[j])//a,b Array, first enter a smaller number listc.add (la[i++]); else{if (la[i ]==lb[j]) {//a,b array with duplicate values, enter a value for that position in the a array to ensure that the merged array has no duplicate values Listc.add (la[i++]); j++;//skips the value of that position in the B array} else Listc.add (lb[j++]) ; }}//Two while loop, ensure that the remaining values after the B array comparison are stored in the merged array while (I<la.length) {Listc.add (la[i++]), and} while (J<lb.length) { Listc.add (lb[j++]); } System.out.println (); SYSTEM.OUT.PRINTLN ("The result of a two-array merge is:"); int []lc=new int [Listc.size ()]; for (int k=0;k<listc.size (); k++) {lc[k]=listc.get (k);} for (int k=0;k<lc.length;k++) {if (k==lc.length-1) System.out.println (Lc[k]); ElseSystem.out.print (lc[k]+ ",");}} }

  

Java implementation data Structure-linear table-sequential table, implement INSERT, find, delete, merge function

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.