链表的相关操作
转载请注明来处:http://www.cnblogs.com/pony1223/
1.约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。
package cn.mdj.link;
import java.util.Scanner;
public class yueSheHuanL {
private static class Node{
private int no;
private Node next;
public Node(int no){
this.no = no;
}
}
public static void main(String[] args) {
// 创建jose 环
Scanner scanner = new Scanner(System.in);
System.out.println("请输入总人数:");
int total = scanner.nextInt();
System.out.println("报数的大小:");
int num = scanner.nextInt();
Node header = new Node(1);
Node pointer = header;
for(int i=2;i<=total;i++){
pointer.next = new Node(i);
pointer = pointer.next;
}
//最后的pointer,要指向头构成环
pointer.next = header;
//jose环构成,开始执行删除元素
System.out.println("报书开始人:");
int k = scanner.nextInt();
System.out.println("以下是出列的顺序:");
while(pointer != pointer.next){
for(int i=k;i<num;i++){//开始循环数数
pointer = pointer.next;
}
System.out.println("出列:"+pointer.next.no);
pointer.next = pointer.next.next;
}
System.out.println("出列:"+pointer.next.no);
}
}
2.单向链表的CRUD等操作:
package cn.mdj.link;
class Link{
private class Node{
private Object data;
private Node next;
public Node(Object data){
this.data = data;
}
public void addNode(Node node){
if(this.next == null){
this.next = node;
}else{
this.next.addNode(node);
}
}
public boolean containsNode(Object data){
if(this.data.equals(data)){
return true;
}else{
if(this.next != null){
return this.next.containsNode(data);
}
return false; }
}
public void removeNode(Node pre,Object data){
if(this.data.equals(data)){
pre.next = this.next;
}else{
if(this.next != null){
this.next.removeNode(this, data);
}
}
}
public void toArrayNode(){
Link.this.resData[Link.this.foot++] = this.data;
if(this.next != null){
this.next.toArrayNode();
}
}
public Object getNode(int index){
if(Link.this.foot == index){
return this.data;
}else{
Link.this.foot++;
return this.next.getNode(index);
}
}
}
private Node root;
private int count;
private Object[] resData = null;
private int foot = 0;
public void add(Object data){
Node node = new Node(data);
if(this.root == null){
this.root = node;
}else{
this.root.addNode(node);
}
this.count ++;
}
public boolean contains(Object data){
if(this.root == null || data == null){
return false;
}else{
return this.root.containsNode(data);
}
}
public void remove(Object data){
if(contains(data)){
if(this.root.data.equals(data)){
this.root = this.root.next;
}else{
this.root.next.removeNode(this.root,data);
}
this.count--;
}
}
public int size(){
return this.count;
}
public boolean isEmpty(){
return this.count==0;
}
public Object[] toArray(){
if(this.root == null){
return null;
}else{
this.foot = 0;
resData = new Object[this.count];
this.root.toArrayNode();
return resData;
}
}
public Object get(int index){
if(this.count > index){
this.foot = 0;
return this.root.getNode(index);
}
return null;
}
}
public class crudLink {
public static void main(String[] args) {
Link link = new Link();
link.add("hello");
link.add("world");
link.add("welcome");
Object[] data = link.toArray();
for(int i=0;i<data.length;i++){
System.out.println(data[i]);
}
System.out.println(link.contains("orld"));
System.out.println(link.get(2));
}
}
3.