java代码圣诞快乐 圣诞节快乐代码书写
java 链表的输出问题
几位的回答都比较清楚了,我想另外说点问题
10多年建站经验, 成都网站建设、网站建设客户的见证与正确选择。创新互联公司提供完善的营销型网页建站明细报价表。后期开发更加便捷高效,我们致力于追求更美、更快、更规范。
你本就不应该加入‘表尾’这个属性,在数据结构中链表的特点就是能用一个地址带一个长串数据链的,不用这个属性的话思路会更加清晰。我也用java模拟过一些基本数据结构:
public class MyNodeT {
public T value;
public MyNodeT next;
public MyNode() {
}
public MyNode(T value) {
this.value = value;
}
public MyNode(MyNodeT t) {
this.value = t.value;
this.next = t.next;
}
public void connect(MyNodeT t){
this.next = t;
}
@Override
public String toString() {
return null==value?"":value+"-";
}
}
在这个节点定义的基础上的链表定义:
public class MyLinkListT{
public MyNodeT next;
public MyLinkList() {
this.next = new MyNodeT();
}
public MyLinkList(T[] tList) {
if(tList.length==0)return;
next = new MyNodeT(tList[0]);
MyNodeT temp = next;
for (int i = 1; i tList.length; i++) {
temp.connect(new MyNodeT(tList[i]));
temp = temp.next;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
MyNodeT t = next;
while (null != t) {
sb.append(t);
t = t.next;
}
return sb.toString();
}
}
然后是相关的操作类:
public class LinkListAction {
MyLinkListComparable list;
public LinkListAction(MyLinkListComparable list) {
this.list = list;
}
/**
* 头插法建立单链表(数组)
* */
public void createFromHead(Comparable...objects){
MyNodeComparable start;
for (int i = 0; i objects.length; i++) {
start = new MyNodeComparable(objects[i]);
start.next = list.next;
list.next = start;
}
}
/**
* 尾插法建立单链表(数组)
* */
public void createFromTail(Comparable...objects){
MyNodeComparable start;
MyNodeComparable end = list.next;
for (int i = 0; i objects.length; i++) {
start = new MyNodeComparable(objects[i]);
end.next = start;
end = start;
}
end.next = null;
}
/**
* 在单链表中查找第i个结点
* */
public MyNodeComparable get(int i){
if(i 0)return null;
MyNodeComparable node = list.next;
int index = 0;
while (node != null index i) {
node = node.next;
index++;
}
return node;
}
/**
* 在单链表中的按值查找
* */
public MyNodeComparable locate(Comparable obj){
if(null == obj)return new MyNodeComparable();
MyNodeComparable node = list.next;
while (node != null !obj.equals(node.value)) {
node = node.next;
}
return node;
}
/**
* 求单链表的长度
* */
public int getLength(){
int length = 0;
MyNodeComparable node = list.next;
while(null != (node = node.next)){
length++;
}
return length;
}
/**
* 单链表的插入操作(按位置)
* */
public void insert(Comparable obj,int location){
int length = 0;
MyNodeComparable node = list.next;
while(node!=null location != length++){node = node.next;}
if(null == node)throw new RuntimeException("插入位置有误!");
MyNodeComparable inserter = new MyNodeComparable(obj);
inserter.next = node.next;
node.next = inserter;
}
/**
* 删除数据
* */
public Comparable delete(int i){
int length = 0;
MyNodeComparable node = list.next;
while(node!=null i != length++){node = node.next;}
if(null == node)throw new RuntimeException("删除位置有误!");
Comparable o = node.next.value;
node.next = node.next.next;
return o;
}
/**
* 合并两个有序的单链表
* */
public static MyLinkListComparable mergeLinkList(MyLinkListComparable la,MyLinkListComparable lb){
MyLinkListComparable lc = new MyLinkListComparable();
MyNodeComparable pc = lc.next;
MyNodeComparable pa = la.next.next;
MyNodeComparable pb = lb.next.next;
while(null != pa || null != pb){
if(null == pa){
pc.next = pb;
break;
}
if(null == pb){
pc.next = pa;
break;
}
if(pa.value.compareTo(pb.value) = 0){
pc.next = pa;
pa = pa.next;
}
else {
pc.next = pb;
pb = pb.next;
}
pc = pc.next;
}
return lc;
}
@Override
public String toString() {
return list.toString();
}
public static void main(String[] args) {
MyLinkListComparable list1 = new MyLinkListComparable();
MyLinkListComparable list2 = new MyLinkListComparable();
LinkListAction lla = new LinkListAction(list1);
// lla.createFromHead(1,3,4,6,8,10);
lla.createFromTail(1,3,4,6,8,10);
LinkListAction llb = new LinkListAction(list2);
llb.createFromTail(2,5,7,9,11);
System.out.println(lla);
System.out.println(llb);
// System.out.println(lla.locate(7));
// System.out.println(lla.getLength());
//
// lla.insert(20, 6);
// System.out.println(lla);
// System.out.println(lla.delete(4));
System.out.println(LinkListAction.mergeLinkList(lla.list, llb.list));
System.out.println(lla);
System.out.println(llb);
}
}
我还写了一些其他的简单数据结构,感兴趣的话,你可以Hi我一下,呵呵。
圣诞快乐!
用java写一个新春贺卡,可转exe的
JAVA 做多媒体,不是强项
转 .exe 可以使用 exe4j
~
求助用JAVA打印一个圣诞树
朋友 你说的最好一句话我没明白?
你能具体说下吗?最好弄个图片。
我跟你说一下思路:就是用for循环就可以.
---给你答案 供你参考,仓促写的:
public class ChristmasTree {
int x = 2;
int i = 0;
public void tree() {
for(i=0; i3; i++) {
for(int j=x; j=0; j--) {
System.out.print(" ");
}
x--;
for(int k=0; k=i; k++) {
System.out.print("*"+" ");
}
System.out.println();
}
}
public static void main(String args[]) {
new ChristmasTree().tree();
}
}
文章标题:java代码圣诞快乐 圣诞节快乐代码书写
文章起源:http://myzitong.com/article/dddgiic.html