链表
package com.java.kexin;
public class LinkListTest {
public static void main(String[] args) {
SingleLinkList singleLinkList = new SingleLinkList();
singleLinkList.addOrder(new HeroNode(5,"松江","及时雨"));
singleLinkList.addOrder(new HeroNode(2,"无用","智多星"));
singleLinkList.addOrder(new HeroNode(3,"林冲","豹子头"));
singleLinkList.addOrder(new HeroNode(4,"武松","打虎"));
singleLinkList.addOrder(new HeroNode(1,"鲁智深","花和尚"));
//singleLinkList.list();
// HeroNode heroNode = new HeroNode(2, "有用", "智障");
// singleLinkList.update(heroNode);
// singleLinkList.delete(4);
// singleLinkList.list();
// int count = singleLinkList.getCount();
// System.out.println(count);
// 2
// HeroNode r = singleLinkList.getK(2);
// System.out.println(r);
singleLinkList.list();
}
}
class SingleLinkList{
private HeroNode head = new HeroNode(0,"","");
public HeroNode getK(int k){
int count = 0;
HeroNode temp = head;
if(head.next == null){
count = 0;
return null;
}
while (true){
if(temp.next == null){
break;
}
count++;
temp = temp.next;
}
if( k > count){
return null;
}
int index = count - k +1;
int start = 0;
temp = head;
while (true){
if(temp.next == null){
break;
}
if(start == index){
break;
}
start++;
temp = temp.next;
}
return temp;
}
public int getCount(){
HeroNode temp = head;
if(head.next == null){
return 0;
}
int count = 0;
while (true){
if(temp.next == null){
break;
}
count++;
temp = temp.next;
}
return count;
}
public void add(HeroNode heroNode){
HeroNode temp = head;
while (true){
if(temp.next == null){
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
public void delete(int no){
if(head.next == null){
System.out.println("链表为空");
}
HeroNode temp = head;
boolean flag = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.getNo() == no){
flag =true;
break;
}
temp = temp.next;
}
if(flag){
temp.next = temp.next.next;
}else {
System.out.println("没找到");
}
}
public void update(HeroNode heroNode){
if(head.next == null){
System.out.println("链表为空");
return;
}
HeroNode temp = head;
boolean flag = false;
while (true){
if(temp.next == null){
break;
}
if(temp.getNo() == heroNode.getNo()){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
temp.setName(heroNode.getName());
temp.setNickName(heroNode.getNickName());
}else {
System.out.println("没找到");
}
}
public void addOrder(HeroNode heroNode){
HeroNode temp = head;
boolean flag = false;
while (true){
if(temp.next == null){
break;
}
if(temp.next.getNo() > heroNode.getNo()){
break;
}
if(temp.next.getNo() == heroNode.getNo()){
flag = true;
break;
}
temp = temp.next;
}
if(flag){
System.out.println("重复了");
return;
}else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
public void list(){
HeroNode temp = head.next;
if(temp == null){
System.out.println("链表为空");
return;
}
while (true){
if(temp == null){
break;
}
System.out.println(temp);
temp = temp.next;
}
}
}
class HeroNode{
private int no;
private String name;
private String nickName;
public HeroNode next;
public HeroNode(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
双向链表
package com.java.kexin; public class DoubleLinkedListDemo { public static void main(String[] args) { DoubleLinkedList doubleLinkedList = new DoubleLinkedList(); doubleLinkedList.addOrder(new HeroNode2(5,"松江","及时雨")); doubleLinkedList.addOrder(new HeroNode2(2,"无用","智多星")); doubleLinkedList.addOrder(new HeroNode2(3,"林冲","豹子头")); doubleLinkedList.addOrder(new HeroNode2(4,"武松","打虎")); doubleLinkedList.addOrder(new HeroNode2(1,"鲁智深","花和尚")); //doubleLinkedList.delete(3); //doubleLinkedList.update(new HeroNode2(3,"冲哥","狗头")); doubleLinkedList.addOrder(new HeroNode2(12,"李逵","小旋风")); doubleLinkedList.list(); } } class DoubleLinkedList{ private HeroNode2 head = new HeroNode2(0,"",""); public void delete(int no){ if(head.next == null){ System.out.println("链表为空"); } HeroNode2 temp = head; boolean flag = false; while (true){ if(temp.next == null){ break; } if(temp.getNo() == no){ flag =true; break; } temp = temp.next; } // 1 2 3 if(flag){ temp.pre.next = temp.next; if(temp.next != null) { temp.next.pre = temp.pre; } }else { System.out.println("没找到"); } } public void update(HeroNode2 heroNode){ if(head.next == null){ System.out.println("链表为空"); return; } HeroNode2 temp = head; boolean flag = false; while (true){ if(temp.next == null){ break; } if(temp.getNo() == heroNode.getNo()){ flag = true; break; } temp = temp.next; } if(flag){ temp.setName(heroNode.getName()); temp.setNickName(heroNode.getNickName()); }else { System.out.println("没找到"); } } public void list(){ HeroNode2 temp = head.next; if(temp == null){ System.out.println("链表为空"); return; } while (true){ if(temp == null){ break; } System.out.println(temp); temp = temp.next; } } public void addOrder(HeroNode2 heroNode){ HeroNode2 temp = head; boolean flag = false; while (true){ if(temp.next == null){ break; } // if(temp.next.getNo() > heroNode.getNo()){ // break; // } // if(temp.next.getNo() == heroNode.getNo()){ // flag = true; // break; // } temp = temp.next; } temp.next = heroNode; heroNode.pre = temp; } } class HeroNode2{ private int no; private String name; private String nickName; public HeroNode2 next; public HeroNode2 pre; public HeroNode2(int no, String name, String nickName) { this.no = no; this.name = name; this.nickName = nickName; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } @Override public String toString() { return "HeroNode{" + "no=" + no + ", name='" + name + '\'' + ", nickName='" + nickName + '\'' + '}'; } }
浙公网安备 33010602011771号