1.java版单链表,添插查修删
(注:在初始化节点时最好再设一个索引变量,方便修改节点
双向链表比起单向链表可以自我删除,也可以前后查找)
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.File;
import java.io.IOException;
import java.util.*;
public class main2 {
public static void main(String[] args) {
LinkedListNode p1=new LinkedListNode ("小明",18);
LinkedListNode p2=new LinkedListNode ("小红",17);
LinkedListNode p3=new LinkedListNode ("小白",20);
LinkedListNode p4=new LinkedListNode ("小白",21);
LinkedListNode p5=new LinkedListNode ("小开",19);
singLinkedList list=new singLinkedList();
list.add(p1);
list.add(p2);
list.add(p3);
list.lookList();
System.out.println("---------");
list.delet("小明");
list.lookList();
System.out.println("---------");
list.set(p4);
list.lookList();
System.out.println("---------");
list.insert(p5);
list.lookList();
}
//创建链表节点
public static class LinkedListNode{
private String name;
private Integer age;
//最好再加一个索引 private Integer no;修改操作
private LinkedListNode next;
//双向链表则加上 private LinkedListNode pre;
public LinkedListNode(String name, Integer age) {
this.name = name;
this.age = age;
}
//为了每个节点方便显示
@Override
public String toString() {
return name+" "+age+" ";
}
}
//创建单链表
public static class singLinkedList{
private LinkedListNode head=new LinkedListNode("",0);
//添加元素
public void add(LinkedListNode p){
LinkedListNode temp=head;
while(temp.next!=null)
{
temp=temp.next;
}
temp.next=p;
//双向链表则加上 p.pre=tem;
}
//遍历链表,与双向相同
public void lookList(){
LinkedListNode temp=head.next;
if(temp.next==null) return;
while(temp!=null) {
System.out.println(temp.name+" "+temp.age+" ");
temp=temp.next;
}
}
//删除节点
public void delet(String p){
LinkedListNode temp=head;
if(temp.next==null)
{
System.out.println("链表为空");
return ;
}
boolean flag=false;
while(temp.next!=null)
{
if(temp.next.name==p)//找前一个节点
{
flag=true;
break;
}
temp=temp.next;
}
if(flag)
temp.next=temp.next.next;
else System.out.println("没有你要删除的元素");
/*双向链表的操作是
LinkedListNode temp=head;
if(temp.next==null)
{
System.out.println("链表为空");
return ;
}
boolean flag=false;
while(temp!=null)
{
if(temp.name==p)//直接找到要删除的节点,因为双向链表可以自我删除
{
flag=true;
break;
}
temp=temp.next;
}
if(flag){
temp.pre.next=temp.next;
if(temp.next!=null)//不然会出现空指针,null的前面还是null
temp.next.pre=tem.pre;
]
else System.out.println("没有你要删除的元素");
*/
}
//插入
public void insert(LinkedListNode p) {
LinkedListNode temp=head;
if(temp.next==null)
{
System.out.println("链表为空");
return ;
}
boolean flag=false;
while(temp.next!=null){
if(temp.next.age>p.age){
break;
} else if (temp.next.age==p.age) {
flag=true;
break;
}
temp=temp.next;
}
if(flag==false){
p.next= temp.next;
temp.next=p;
//双向链表我认为应该加上:p.pre=tem; tem.pre=p;
}
else System.out.println("插入失败");
}
//修改链表,与双向相同
public void set(LinkedListNode p) {
LinkedListNode temp=head;
if(temp.next==null)
{
System.out.println("链表为空");
return ;
}
boolean flag=false;
while(temp!=null){
if(temp.name==p.name){
flag=true;
break;
}
temp=temp.next;
}
if(flag){
temp.name=p.name;
temp.age=p.age;
}
else System.out.println("修改失败");
}
}
}
//反转链表
public void reverseLinkedList(){
LinkedListNode next,cur=head.next,newhead=new LinkedListNode("",0);
while(cur!=null){
if(head.next==null){
System.out.println("链表为空");
return;
}
next=cur.next;
cur.next=newhead.next;
newhead.next=cur;
cur=next;
}
head.next=newhead.next;
}
2.环形链表及约瑟夫问题
public class circleLinkedList {
public static void main(String[] args) {
cirList boy=new cirList();
boy.add(5);
boy.showBoy();
System.out.println("--------");
boy.josephu(1,2,5);
}
//创建节点
public static class boyNode{
private int no;
private boyNode next;
public boyNode(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public boyNode getNext() {
return next;
}
public void setNext(boyNode next) {
this.next = next;
}
}
//创建环形链表
public static class cirList{
private boyNode first=null;
//添加元素
public void add(int n){
if(n<1){
System.out.println("输入有误");
return;
}
boyNode cur=null;
for (int i = 1; i <=n; i++) {
boyNode boy=new boyNode(i);
if(i==1){
first=boy;
cur=boy;
first.setNext(first);
}
else {
cur.setNext(boy);
boy.setNext(first);
cur=boy;
}
}
}
//遍历show
public void showBoy(){
if(first==null){
System.out.println("没有boy");
return;
}
boyNode cur=first;
while(true){
System.out.println(cur.getNo());
if(cur.getNext()==first){
break;
}
else{
cur=cur.getNext();
}
}
}
//josephu
/**
*
* @param starNo 从第几个boy开始
* @param breakNo 间隔数
* @param count 多少个小孩
*/
public void josephu(int starNo,int breakNo,int count){
if(starNo<1||starNo>count||first==null){
System.out.println("输入有误");
return;
}
boyNode helper=first;
while(helper.getNext()!=first){
helper=helper.getNext();
}
for (int i = 0; i < starNo-1; i++) {
first=first.getNext();
helper=helper.getNext();
}
while(true) {
if(helper==first){
break;
}
for (int i = 1; i <= breakNo - 1; i++) {
first=first.getNext();
helper=helper.getNext();
}
System.out.println(first.getNo());
first=first.getNext();
helper.setNext(first);
}
}
}
}