/**
* 带头结点的单链表
*/
public class LinkListDemo {
//获取节点的个数,不统计头节点
public static int getLength(LinkList linkList){
Node head = linkList.getHead();
int l = 0;
Node c = head.next;
while (c!=null){
l++;
c = c.next;
}
return l;
}
//查找单链表中的倒数第k个结点
public static Node findLastIndexNode(LinkList linkList,int index){
Node head = linkList.getHead();
if (head.next==null){
return null;
}
int length = getLength(linkList);
if (index<=0||index>length){
return null;
}
Node c = head.next;
for (int i = 0; i < length - index; i++) {
c = c.next;
}
return c;
}
//单链表的反转
public static void reverseList(LinkList linkList){
Node head = linkList.getHead();
if (head.next==null||head.next.next==null){
return;
}
//辅助的指针
Node c = head.next;
Node next = null;//临时保存下一个结点
Node reversHead = new Node(0,"");
//新的结点插入头结点的后面
while (c!=null){
next = c.next;//第一次next为第二个结点
c.next = reversHead.next;//第一次,将第一个结点的下一个指向空
reversHead.next = c;//reversHead下一个指向第一个结点
c = next;//第一次c由指向第一个结点改为指向第二个结点
}
head.next = reversHead.next;//reversHead的下一个为空,将使用原来的头结点
}
//合并两个有序的单链表,合并之后的链表依然有序
public static LinkList combineList(LinkList l1,LinkList l2){
Node h1 = l1.getHead();
Node h2 = l2.getHead();
Node temp1 = h1.next;
Node temp2 = h2.next;
LinkList linkList = new LinkList();
Node newHead = new Node(0,"");
linkList.add(newHead);
Node temp3 = newHead;
while (temp1!=null||temp2!=null){
if (temp1==null&&temp2!=null){
temp3.next = temp2;
temp2 = temp2.next;
}else if (temp2==null&&temp1!=null){
temp3.next = temp1;
temp1 = temp1.next;
}else {
if (temp1.no<=temp2.no){
temp3.next = temp1;
temp1 = temp1.next;
}else {
temp3.next = temp2;
temp2 = temp2.next;
}
}
temp3 = temp3.next;
}
return linkList;
}
public static void main(String[] args){
Node n1 = new Node(1,"A");
Node n2 = new Node(2,"B");
Node n3 = new Node(3,"C");
Node n4 = new Node(4,"D");
// LinkList linkList = new LinkList();
// linkList.add(n2);
// linkList.add(n1);
// linkList.add(n4);
// linkList.add(n3);
// linkList.list();
// LinkList linkList2 = new LinkList();
// System.out.println("getLength(linkList2) = " + getLength(linkList2));
// linkList2.addOrder(n2);
// linkList2.addOrder(n1);
// linkList2.addOrder(n4);
// linkList2.addOrder(n3);
// linkList2.addOrder(n3);
//linkList2.list();
// System.out.println("findLastIndexNode(linkList2,2) = " + findLastIndexNode(linkList2, 2));
// Node n44 = new Node(4,"D~");
// linkList2.update(n44);
// linkList2.list();
// System.out.println("getLength(linkList2) = " + getLength(linkList2));
// System.out.println("删除");
// linkList2.del(3);
// linkList2.list();
// System.out.println("getLength(linkList2) = " + getLength(linkList2));
//reverseList(linkList2);
//linkList2.list();
System.out.println("linkList3-------");
Node n11 = new Node(1,"A11");
Node n22 = new Node(2,"B22");
Node n33 = new Node(3,"C33");
LinkList linkList3 = new LinkList();
linkList3.addOrder(n22);
linkList3.addOrder(n11);
linkList3.addOrder(n33);
linkList3.list();
System.out.println("linkList4-------");
Node n333 = new Node(3,"C333");
Node n444 = new Node(4,"D444");
LinkList linkList4 = new LinkList();
linkList4.addOrder(n444);
linkList4.addOrder(n333);
linkList4.list();
System.out.println("combineList------");
combineList(linkList3,linkList4).list();
}
}
class LinkList{
//头结点
private Node head = new Node(0,"");
public Node getHead() {
return head;
}
private boolean addMethod = false;
//顺序插入
public void add(Node node){
this.addMethod = true;
Node temp = head;
//找尾结点
while (true){
if (temp.next==null){
break;
}
temp = temp.next;
}
temp.next = node;
}
//有序插入 根据no
public void addOrder(Node node){
if (addMethod){
System.out.println("有序插入不能与add()调用后使用!");
return;
}
Node temp = head;
boolean flag = false; //重复的no
while (true){
if (temp.next==null){
break;
}else if (temp.next.no == node.no){
flag = true;
System.out.println("存在重复的Node");
break;
}else if (temp.next.no > node.no){
break;
}
temp = temp.next;
}
if (!flag){
node.next = temp.next;
temp.next = node;
}
}
//修改 根据no 修改data
public void update(Node node){
if (head.next==null){
System.out.println("链表为空");
return;
}
Node temp = head.next;
boolean flag = false; //是否存在该节点
while (true){
if (temp == null){
break;
}
if (temp.no == node.no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.data = node.data;
}else {
System.out.println("不存在该结点,不能修改");
}
}
//根据no删除结点
public void del(int no){
Node temp = head;
boolean flag = false; //是否存在该结点
while (true){
if (temp.next == null){
break;
}
if (temp.next.no == no){
flag = true;
break;
}
temp = temp.next;
}
if (flag){
temp.next = temp.next.next;
}else {
System.out.println("不存在该结点,不能删除");
}
}
//显示单链表
public void list(){
if (head.next==null){
System.out.println("链表为空");
return;
}
Node temp = head.next;
while (true){
if (temp!=null){
System.out.println(temp);
temp = temp.next;
}else {
break;
}
}
}
}
class Node{
public int no;
public String data;
public Node next;
public Node(int no, String data) {
this.no = no;
this.data = data;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
", data='" + data + '\'' +
'}';
}
}