java 板


LINE

就是建立两个linked-list 然後可以让人用键盘输入数字 比如说第一个linked-list输入 1 4 7三个数字 第二个linked-list输入 2 5 6三个数字 接着萤幕上会显示出合并後的数字1 2 4 5 6 7 并且把最大的数值7也显示出来 (底下是我写的code 如果有改进的地方也请大家跟我说 谢谢) =======linkedlist部分======= class Node { int data; //节点资料 Node next; //下一个节点 //建构子 public Node(int data) { this.data = data; this.next = null; } } //串列类别 public class LinkedList { private Node first; private Node last; //检查串列是否是空的 public boolean isEmpty() { return first == null; } //在串列插入节点 public void insert(int data, boolean isFront) { //建立节点 Node newNode = new Node(data); //插入节点在开头 if(this.isEmpty()) { //第一个节点 first = newNode; last = newNode; } else { if(isFront) { newNode.next = first; first = newNode; } else { //新增在节尾 last.next = newNode; last = newNode; } } } //找寻节点资料 public boolean find(int data) { boolean isFound = false; Node current = first; while(current != null) { if(data == current.data) isFound = true; current = current.next; //下一个节点 } return isFound; } //从前面取得节点的资料 public int fetch(boolean isFront) { int data = -1; if(!this.isEmpty()) { if(first == last) { //只有一个节点 data = first.data; first = null; last = null; } else { if(isFront) { //从开头取出 data = first.data; first = first.next; } else { Node current = first; Node temp = last; //找到最後节点的前一个节点 while(current.next != last) current = current.next; last = current; current.next = null; data = temp.data; } } } return data; } //串列走访列印所有节点 public void print() { if(this.isEmpty()) { System.out.println("串列是空的"); return; } Node current = first; while(current != null) { System.out.print("[" + current.data +"]"); current = current.next; //下一个节点 } System.out.println(); } } =========test部分======== import java.io.*; public class test1013 { //主程式 public static void main(String[] args) throws Exception { //建立串列物件 LinkedList list = new LinkedList(); LinkedList list1 = new LinkedList(); //插入节点第一个linkedlist System.out.println("请输入第一个linkedlist的数字三个:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str1 = br.readLine(); String str2 = br.readLine(); String str3 = br.readLine(); int num1 = Integer.parseInt(str1); int num2 = Integer.parseInt(str2); int num3 = Integer.parseInt(str3); list.insert(num1, false); list.insert(num2, false); list.insert(num3, false); System.out.print("第一个linkedlist原始串列:"); list.print(); //插入节点第二个linkedlist System.out.println("请输入第二个linkedlist的数字三个:"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in)); String str4 = br1.readLine(); String str5 = br1.readLine(); String str6 = br1.readLine(); int num4 = Integer.parseInt(str4); int num5 = Integer.parseInt(str5); int num6 = Integer.parseInt(str6); list1.insert(num4, false); list1.insert(num5, false); list1.insert(num6, false); System.out.print("第二个linkedlist原始串列:"); list1.print(); (我想问一下 写到这里我应该怎麽把两个linkedlist合并成一个 由小到大的串列呢?) } } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.139.56.169 ※ 编辑: helenus 来自: 220.139.56.169 (05/07 12:02)







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP