这个专题将作为数据结构的学习笔记,记录自己的学习过程,多的话也不讲了,也希望能对自学的小伙伴有一定的帮助!
我用了两种方法来合并:第一种就是正常的思路,第二种是递归的思路
static class SingleList{
private person head = new person(0,"");
//没有顺序直接添加
public void addPerson1(person newPerson){
person temp =head;
while (true){
if(temp.getNext()==null){
break;
}
temp=temp.getNext();
}
temp.setNext(newPerson);
}
//根据年龄排序
public void addPerson2(person newPerson){
person temp=head;
boolean isFlag = false;
while(true){
if(temp.getNext()==null){
isFlag=true;
break;
}
if(temp.getNext().getAge()>newPerson.getAge()){
isFlag=true;
break;
}else if(temp.getNext().getAge()==newPerson.getAge()){
break;
}
temp=temp.getNext();
}
if(isFlag){
newPerson.setNext(temp.getNext());
temp.setNext(newPerson);
return;
}else{
System.out.println("已存在该排名");
return;
}
}
//输出遍历
public void list(){
if(head.getNext()==null){
System.out.println("链表无数据");
return;
}
person temp=head.getNext();
while (temp.getNext()!=null){
System.out.println(temp.toString());
temp=temp.getNext();
}
System.out.println(temp.toString());
return;
}
//删除
public void del(int age){
person temp =head;
boolean isFlag =false;
while (true){
if(temp.getNext()==null){
break;
}
if(temp.getNext().getAge()==age){
isFlag=true;
break;
}
}
if(isFlag=true){
System.out.println("成功删除!"+temp.getNext().toString());
temp.setNext(temp.getNext().getNext());
}else{
System.out.println("不存在该数据");
}
}
//改
public void change(int age ,String name){
person temp=head;
boolean isFlag =false;
while (true){
if(temp.getNext()==null){
break;
}
if(temp.getNext().getAge()==age){
isFlag=true;
break;
}
}
if(isFlag){
temp.getNext().setName(name);
System.out.println("修改成功");
}else {
System.out.println("修改失败,不存在该数据");
}
}
//查找
public person find(int age){
person temp=head;
boolean isFlag =false;
while (true){
if(temp.getNext()==null){
break;
}
if(temp.getNext().getAge()==age){
isFlag=true;
break;
}
temp=temp.getNext();
}
if(isFlag){
System.out.println(temp.getNext());
return temp.getNext();
}
System.out.println("不存在该数据");
return null;
}
//正常思路
public void combine(SingleList list){
person temp=list.head;
person temp2=null;
int num=0;
while (true){
if(temp.getNext()==null){
break;
}
temp=temp.getNext();
temp2=temp;
temp=temp.getNext();
list.del(temp2.getAge());
this.addPerson2(temp2);
}
this.addPerson2(temp);
}
//递归思路
public person combine2(person p1,person p2){
if(p1==null){
return p2;
}
if(p2==null){
return p1;
}
if(p1.getAge()<p2.getAge()){
p1.setNext(combine2(p1.getNext(),p2));
return p1;
}
if (p1.getAge()>=p2.getAge()){
p2.setNext(combine2(p1,p2.getNext()));
return p2;
}
return null;
}
}
//栈用到的person类
public class person {
private int age ;
private String name;
private person next;
public person getNext() {
return next;
}
public void setNext(person next) {
this.next = next;
}
public person(int age, String name) {
this.age = age;
this.name = name;
}
public person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
ps:这个栈是我自己写的,模拟的一个栈,并没有直接用java提供的栈