【Java精品源码栏目提醒】:网学会员为广大网友收集整理了,【精品】数据结构源代码 - 其它资料,希望对大家有所帮助!
实验一源代码1、判断数组元素是否已按升序排序/ isSortedArray.java isSortedArray application author version 1.00 2010/3/5 /public class isSortedArray public static boolean isSortedint table iftable.lengthgt0 int i0 for i0ilttable.length-2ampamp tableilttablei1i if itable.length-1 return true else System.out.printlnquotthe table has no numberquot return false /判断整数数组是否已按升序排序/ public static boolean isSortedComparable table if tablenull int i0 for i0ilttable.length-2ampamp tablei.compareTotablei1lt0i if itable.length-1 return true else System.out.printlnquotthe table has no objectquot return false /判断对象数组是否已按升序排序/ public static void mainString args int keywords111213 String keywords2quotabstractquotquotbooleanquotquotbreakquotquotacstractquot ifisSortedkeywords1 System.out.printlnquotksywords1 in accent wayquot else System.out.printlnquotksywords1 not in accent wayquot ifisSortedkeywords2 System.out.printlnquotksywords2 in accent wayquot else System.out.printlnquotksywords2 not in accent wayquot 2、数组逆置/ ReverseArray.java ReverseArray application author version 1.00 2010/3/8 /public class ReverseArray /将一个已知数组中所有元素的次序颠倒为相反次序/ /交换元素的方法/ public static void swapObject table int iint j iftablenull ampamp igt0 ampamp ilttable.length ampamp jgt0ampampjlttable.length ampamp ij Object temptablej tablejtablei tableitemp /主方法/ public static void mainString args Object table522697 System.out.printlnquotbefore Reversequot forint i0 ilttable.lengthi System.out.printtableiquot - quot System.out.printlnquotquot forint i0ilttable.length/2i swaptableitable.length-1-i System.out.printlnquotafter Reversequot forint i0 ilttable.lengthi System.out.printtableiquot - quot // TODO add your application code 实验二源代码import java.io.class Node_type public String name public int score public Node_type nextpublic class SingleList static Node_type ptrheadcurrentprevtemp DataInputStream infile DataOutputStream outfile public static void anykey_f char tChar System.out.printlnquot press any key to continue ...quot try tCharcharSystem.in.read catchIOException e SingleList headnew Node_type head.nextnull public void load_f boolean donefalse System.out.printquotFile loading...quot try infile new DataInputStreamnew FileInputStreamquotsinglelist.datquot catchIOException e System.err.printlnquotfailedquot System.err.printlnquotsinglelist.dat not foundquot return whiledonefalse ptrnew Node_type try ptr.nameinfile.readUTF ptr.scoreinfile.readInt catchIOException eof donetrue ifdonetrue ptrnull System.out.printlnquotOKquot try infile.close catchIOException e public void save_f Node_type node System.out.printquotFile saving...quot try outfile new DataOutputStreamnew FileOutputStreamquotsinglelist.datquot catchIOException e System.out.printlnquotfailedquot return nodehead.next whilenodenull try outfile.writeUTFnode.name outfile.writeIntnode.score catchIOException e nodenode.next System.out.printlnquotOKquot try outfile.close catchIOException e public static void insert_f BufferedReader innew BufferedReadernew InputStreamReaderSystem.in String stquot quot ptrnew Node_type ptr.nextnull System.out.printquotn student name : quot System.out.flush try ptr.namein.readLine catchIOException e System.out.printquotstudent score: quot System.out.flush try stin.readLine catchIOException e try ptr.scoreInteger.valueOfst.intValue catchNumberFormatException e System.out.printlnquot quot prevhead currenthead.next whilecurrent nullampampcurrent.scoregtptr.score prevcurrent currentcurrent.next ptr.nextcurrent prev.nextptrpublic static void delete_f BufferedReader innew BufferedReadernew InputStreamReaderSystem.in String del_namequot quot int count0 ifhead.nextnull System.out.printquot No student recordnquot else System.out.printquotDelete student name: quot System.out.flush try del_namein.readLine catchIOException e prevhead currenthead.next whilecurrent nullampampdel_name.equalscurrent.name prevcurrent currentcurrent.next ifcurrentnull prev.nextcurrent.next currentnull System.out.printquotstudent quot del_namequotrecord deletednquot else System.out.printquotstudent quot del_namequot not foundnquot anykey_fpublic static void modify_f BufferedReader innew BufferedReadernew InputStreamReaderSystem.in int count0 String n_tempquotquotstquotquot ifhead.nextnull System.out.printquotNo student recordnquot else System.out.printquotModify student name : quot System.out.flush try n_tempin.readLine catchIOException e currenthead.next whilecurrentnullampampn_temp.equalscurrent.name prevcurrent currentcurrent.next ifcurrentnull System.out.printquotnquot System.out.printquotstudent name : quotcurrent.namequotnquot System.out.printquotstudent name : quotcurrent.scorequotnquot System.out.printquotnquot System.out.printquotPlease enter new score: quot System.out.flush try stin.readLine catchIOException e try current.scoreInteger.valueOfst.intValue catchNumberFormatException e count System.out.printquotStudent quot n_tempquotrecord modifiednquot else System.out.printquotStudent quot n_tempquot not foundnquot anykey_fpublic static void display_f BufferedReader in new BufferedReadernew InputStreamReaderSystem.in int count0 String opquotquot int option0 ifhead.nextnull System.out.printquotNo student recordnquot else System.out.printquotNAME SCOREnquot System.out.printquot---------------nquot currenthead.next whilecurrentnull System.out.printquot quot System.out.printcurrent.namequot quot System.out.printcurrent.scorequotnquot count currentcurrent.next ifcount200 anykey_f System.out.printquot--------------------------nquot System.out.printquotTotalquotcountquotrecords foundnquot anykey_fpublic static void main String args BufferedReader in new BufferedReadernew InputStreamReaderSystem.in String opquotquot int option0 SingleList objnew SingleList obj.load_f do System.out.printlnquot SingleList Program quot System.out.printlnquot quot System.out.printlnquot lt1gtInsert Node quot System.out.printlnquot lt2gtDelete Node quot System.out.printlnquot lt3gtDisplay Node quot System.out.printlnquot lt4gtModify Node quot System.out.printlnquot lt5gtExit quot System.out.printlnquot quot System.out.printlnquotquot System.out.printlnquotn Choice : quot System.out.flush op quot quot try opin.readLine catchIOException e option0 try optionInteger.valueOfop.intValue catchNumberFormatException e System.out.printlnquot quot switch option case 1: obj.insert_f break case 2: obj.delete_f break case 3: obj.display_f break case 4: obj.modify_f break case 5: obj.save_f System.exit0 whiletrue 实验三源代码1、 栈/1.栈的入栈与出栈的程序实例/ File Name:Stack.Java/import java.io.class Stack static int MAX26 static String item new StringMAX static int top Stack top-1 public static void push_f DataInputStream in new DataInputStreamSystem.in iftopgtMAX-1 System.out.printquotn Stack is full nquot else top System.out.printquotn Please enter item to insert: quot System.out.flush try itemtopin.readLine catchIOException eSystem.out.printlnquot quot public static void pop_f iftoplt0 System.out.printquotn No ite stack is empty nquot else System.out.printquotn Item quot itemtop quotdeleted nquot top-- System.out.printlnquot quot public static void list_f int count0i0 iftoplt0 System.out.printquotn No ite stack is empty nquot else System.out.printquotnn ITEMnquot System.out.printquot--------------nquot foritopigt0i-- System.out.printquot quot System.out.printlnitemi count System.out.printquot--------------nquot System.out.printquot Total item : quot count quot nnquot System.out.printlnquot quot public static void main String args DataInputStream innew DataInputStreamSystem.in String opquot quot int option0 Stack objnew Stack do System.out.printlnquot Stack Programquot System.out.printlnquot quot System.out.printlnquot lt1gt Insert Node quot System.out.printlnquot lt2gt Delete Node quot System.out.printlnquot lt3gt List Node quot System.out.printlnquot lt4gt Exit quot System.out.printlnquotquot System.out.printquotn Choice : quot System.out.flush opquot quot try opin.readLine catchIOException e System.out.printlne option0 try optionInteger.valueOfop.intValue catchNumberFormatException e System.out.printlnquotn Please input 1234....quot System.out.printlnquotnnnquot switchoption case 1: push_f break case 2: pop_f break case 3: list_f break case 4: System.exit0 whiletrue 2、 队列/ 2.队列的入队与出队的程序实例使用循环队列处理数据//File Name:Queue.java/import java.io. class Queuestatic int MAX3 static Stringitemnew StringMAXstatic int frontrearQueue//构造函数 front0 rear-1public static void anykey_f//按任一键即继续执行 Function char tChar System.out.printlnquot Press any key to continue...quot try tCharcharSystem.in.read catchIOException e public static void enqueue_f//入队函数 DataInputStream innew DataInputStreamSystem.in ifreargtMAX-1 //当队列已满,则显示错误 System.out.printlnquotn Queue is fullnquot else rear System.out.printquotn Please enter item to insert:quot System.out.flush try itemrearin.readLine catch IOException e System.out.printlnquotquot public static void dequeue_f //出队函数 iffrontgtrear //当队列中没有数据存在是,则显示错误 System.out.printquotn No item,queue is empty nquot else System.out.printquotn Itemquotitemfrontquotdeleted nquot front System.out.printlnquotquotpublic static void list_f//显示函数DataInputStream innew DataInputStreamSystem.inint count0i0iffrontgtrear System.out.printquotn No item, queue is emptynnquotelseSystem.out.printquotn ITEMnquotSystem.out.printquot------------------nquotforifrontiltreari System.out.printquot quot System.out.printitemi quotnquot count ifcount MAX 0 anykey_f Syste.上一篇:[精品论文]外文翻译 JAVA技术及SSH框架和JSP技术的介绍