链表

结合模型仔细理解!!!!!!!!

单链表

理解头插法和尾插法

头插法:将新节点见到头结点之前,并使新结点成为头结点

尾插法:遍历链表到最后一个结点,再让最后一个节点指向新节点。头结点不能移动

单链表的增删查改实现

package LinkedList;

public class SingleLinkedList2 {

public static void main(String[] args) {
	Node hero1=new Node(1,"宋江","及时雨");
	Node hero2=new Node(2,"陆俊义","玉麒麟");
	Node hero3=new Node(4,"林冲","豹子头");
	Node hero4=new Node(3,"吴用","智多星");
	
	List list=new List();
	list.addSort(hero1);
	list.addSort(hero2);
	list.addSort(hero3);
	list.addSort(hero4);
	list.show();
	
	System.out.println("============");
	Node hero5=new Node(4,"曹冲","称象");
	list.change(hero5);
	list.show();
	/*
	System.out.println("============");
	list.delete(new Node(2,"卢俊义","智多星"));
	list.show();*/
	System.out.println("============");
	int x=list.number();
	System.out.println(x);


}

}
class Node{
int num;
String name;
String otherName;
Node next;
public Node(int x,String name,String otherName) {
this.num=x;
this.name=name;
this.otherName=otherName;

}
public String toString() {
	return num+" "+name+" "+otherName;
}

}
class List{
private Node head=new Node(0,null,null);
//无序添加
public void add(Node hero) {
Node temp=head;//头结点不能移动,用辅助指针帮助完成遍历

	while(true) {
		//temp=temp.next;
		if(temp.next==null)
			break;
		temp=temp.next;
		
	}
	temp.next=hero;
}
//有序添加
public void addSort(Node hero) {
	Node temp=head;
	boolean flag=false;
	while(true) {
		if(temp.next==null) {
			
		break;}
		if(temp.next.num>hero.num) {
			break;
		}
		else if(temp.next.num==hero.num) {
			flag=true;
		    break;
		}
		temp=temp.next;
		
	}
	if(flag) {
		System.out.println("已存在");
		
	}
	else {
		hero.next=temp.next;
		temp.next=hero;
	}
	
}
//改变节点中的数据
public void change(Node hero) {
	Node temp=head.next;
	if(temp==null) {
		System.out.println("链表为空");
		return;
	}
	boolean flag=false;
	while(true) {
		if(temp.num==hero.num) {
			flag=true;
			break;
		}
		temp=temp.next;
	}
	if(flag) {
		temp.name=hero.name;
		temp.otherName=hero.otherName;
	}
	else
		System.out.println("结点找不到");
		
}
//删除结点中的数据,要删除结点的前一个结点
public void delete(Node hero) {
	Node temp=head.next;
	boolean flag=false;
	if(temp==null) {
		return;
	}
	while(true) {
		if(temp==null) {
			break;
		}
		if(temp.next.num==hero.num) {
			flag=true;
		    break;}
	     temp=temp.next;
	}
	if(flag) {
		
		temp.next=temp.next.next;
	}
	else
		System.out.println("未找到");
}
public void show() {
	
	if(head.next==null) {
		System.out.println("链表为空");
	    return;}
	Node temp=head.next;
	while(true) {
		
		if(temp==null)
			break;
		System.out.println(temp);
		temp=temp.next;
	}
}
//获取有效节点个数(带头结点的链表不将头结点计入
public int number() {
	Node temp=head;
	int len=0;
	if(head.next==null)
		return 0;
	while(true) {
		if(temp.next==null) {
			
			break;
		}
		
		temp=temp.next;
		len++;
	}
	return len;
}

}

双向链表

代码实现

package LinkedList;

public class DoubleLinkedList {

public static void main(String[] args) {
	heroNode hero1=new heroNode(1,"宋江","及时雨");
	heroNode hero2=new heroNode(2,"陆俊义","玉麒麟");
	heroNode hero3=new heroNode(4,"林冲","豹子头");
	heroNode hero4=new heroNode(3,"吴用","智多星");
	DoubleLinkedListDemo list=new DoubleLinkedListDemo();
	list.add(hero1);
	
	list.add(hero2);
	
	list.add(hero3);
	
	list.add(hero4);
	list.delete(4);
	
	list.show();
	System.out.println("======");
	heroNode hero5=new heroNode(3,"有用","星");
	list.change(hero5);
	list.show();
}


}

class heroNode{
int num;
String name;
String otherName;
heroNode next;
heroNode pre;

public heroNode(int x,String y,String z) {
	 num=x;
	 name=y;
	 otherName=z;


}
public String toString() {
	return num+" "+name+" "+otherName;
}

}
class DoubleLinkedListDemo{

heroNode head=new heroNode(0,null,null);
//双线链表增
public void add(heroNode hero) {
heroNode temp=head;

	while(true) {
		if(temp.next==null)
			break;
		temp=temp.next;
	}
	temp.next=hero;
	hero.pre=temp;
		
}
//双向链表遍历
public void show() {
	heroNode temp=head.next;
	if(temp==null)
		return;
	while(true) {
		if(temp==null)
			break;
		System.out.println(temp);
		temp=temp.next;
	}
}
//双向链表删
public void delete(int x) {
	heroNode temp=head.next;
	
	if(temp==null) {
		System.out.println("不存在");
		return;
	}
	boolean flag=false;
    while(true) {
    	if(temp.next==null)
    		break;
    	if(temp.num==x) {
    		flag=true;
    	    break;
    	}
    	temp=temp.next;
    }
    if(flag) {
    	temp.pre.next=temp.next;
    	if(temp.next!=null) {
        temp.next.pre=temp.pre;
    	}
    }
    else
    	System.out.println("不存在");
}
//双链表修改
public void change(heroNode hero) {
	heroNode temp=head.next;
	if(temp==null)
		return;
	boolean flag=false;
	while(true) {
		
		if(temp.num==hero.num) {
			flag=true;
			break;
		}
		if(temp.next==null)
			break;
		temp=temp.next;


	}
	if(flag) {
		temp.num=hero.num;
		temp.name=hero.name;
		temp.otherName=hero.otherName;
	}
	else
		System.out.println("不存在");


}

}

环形链表

代码实现(约瑟夫问题)

package LinkedList;

public class JosephfDemo {

public static void main(String[] args) throws Exception {

  circleLinkedList list=new circleLinkedList();

  list.add(5);

  list.show();
  System.out.println("=========");
  list.out(1, 2, 5);
}

}
class Boy{

private int num;
private Boy next;
public Boy(int num) {
this.num=num;
}
public void setNext(Boy next) {
this.next = next;
}
public Boy getNext() {
return next;
}
public void setNum(int x) {
this.num=x;
}
public int getNum() {
return num;
}

}
//构建环形链表
class circleLinkedList{
private Boy first=null;
//Boy curBoy=null;
//环形链表的构建
public void add(int x) throws Exception {
if(x<1) {
System.out.println(“输入不正确”);
return;
}
Boy curBoy=null;

	for(int i=1;i<=x;i++) {
		Boy boy=new Boy(i);
		if(i==1) {
			//自己指向自己
			first=boy;
			first.setNext(first);
			curBoy=first;
			
		}
		else {
			
			curBoy.setNext(boy);
			
			boy.setNext(first);//构成环状
			curBoy=boy;//curBoy后移


}

	}
}
public void show() {
	if(first==null) {
		System.out.println("链表为空");
		return;
	}
	Boy curBoy=first;
	while(true) {
		System.out.println("编号为"+curBoy.getNum());
		if(curBoy.getNext()==first) {
			break;
		}
		curBoy=curBoy.getNext();
	}
}
/**
 * 
 * @param start 表示从第几个数开始数
 * @param count 表示数几下
 * @param nums 表示小孩的总个数
 */
public void out(int start,int count,int nums) {
	if(first==null||start<1||count>nums) {
		System.out.println("输入的数据不合法");
		return;
	}
	//构建辅助指针
	Boy helper=first;
	//让辅助指针指向最后一个小孩
	while(true) {
		if(helper.getNext()==first) {
			break;
		}
		helper=helper.getNext();
	}
	//while结束后,helper已指向最后一个小孩
	//开始报数之前,先将first移动到开始报数的那个小孩上,start-1
	for(int i=0;i<start-1;i++) {
		first=first.getNext();
		helper=helper.getNext();
		
	}
	while(true) {
		//只有一个小孩
		if(helper==first) {
			break;
		}
	//开始报数时,移动count-1到要出圈小孩上
	for(int j=0;j<count-1;j++) {
		first=first.getNext();
		helper=helper.getNext();
		
	}
	//出圈
	System.out.println("出圈小孩编号为"+first.getNum());
	first=first.getNext();
	helper.setNext(first);
}
	System.out.println("留在圈中的编号为"+first.getNum());










}

}

{
first=first.getNext();
helper=helper.getNext();

	}
	while(true) {
		//只有一个小孩
		if(helper==first) {
			break;
		}
	//开始报数时,移动count-1到要出圈小孩上
	for(int j=0;j<count-1;j++) {
		first=first.getNext();
		helper=helper.getNext();
		
	}
	//出圈
	System.out.println("出圈小孩编号为"+first.getNum());
	first=first.getNext();
	helper.setNext(first);
}
	System.out.println("留在圈中的编号为"+first.getNum());










}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值