package org.structure.linkedList;
/**
* 单链表
* @author cjj_1
* @date 2020-08-03 14:59
*/
public class SingleLinkedListDemo {
public static void main(String[] args) {
LinkedNode node1 = new LinkedNode(1,"鲁智深","鲁达");
LinkedNode node4 = new LinkedNode(4,"王五","55");
LinkedNode node2 = new LinkedNode(2,"张三","ss");
LinkedNode node3 = new LinkedNode(3,"李四","44");
SingleLinkedList list = new SingleLinkedList();
list.orderToAdd(node1);
list.orderToAdd(node3);
list.orderToAdd(node2);
list.orderToAdd(node4);
System.out.println("-----------修改前-----------");
list.showList();
System.out.println("---------修改后----------");
//修改节点
list.updateNode(new LinkedNode(2,"张三三三","ss"));
list.showList();
System.out.println("-----------删除节点---------");
list.deleteNode(node1);
list.showList();
System.out.println("-----------反转---------");
list.reverseLinkedList();
}
}
class SingleLinkedList{
//定义头节点
LinkedNode head = new LinkedNode(0,null,null);
/**
* 链表反转(腾讯面试题)
* @return
*/
public void reverseLinkedList(){
LinkedNode newHead = new LinkedNode(0,null,null);//重新定义一个head指针
if(head.nextNode == null || head.nextNode.nextNode == null){
System.out.println("反转成功!");
newHead.nextNode = head.nextNode;
}
//创建一个头节点
LinkedNode cur = head.nextNode;
LinkedNode next = null;
while(cur!=null){
next = cur.nextNode;
cur.nextNode = newHead.nextNode;
newHead.nextNode = cur;
cur = next;
}
//打印反转代码
while (newHead !=null){
System.out.println(newHead);
newHead = newHead.nextNode;
}
}
/**
* 顺序添加增加节点
* @param node
*/
public void add(LinkedNode node){
LinkedNode temp = head;
//当最后一个节点的指向为null时说明是尾节点(此步骤是为了找尾节点)
while (true){
if(temp.nextNode == null){
break;
}
temp = temp.nextNode;
}
//增加一个节点
temp.nextNode = node;
}
/**
* 按照编号顺序添加节点
* @param node
*/
public void orderToAdd(LinkedNode node){
LinkedNode temp = head;
boolean flag = Boolean.FALSE;
//当最后一个节点的指向为null时说明是尾节点(此步骤是为了找尾节点)
while (true){
//判断编号大小
if(temp.nextNode == null){
break;
}
if(temp.nextNode.no>node.no){
break;
}else if(temp.no == node.no){
System.out.println("英雄编号已经存在!");
flag =true;
break;
}
temp = temp.nextNode;
}
//增加一个节点
if(!flag){
node.nextNode = temp.nextNode;
temp.nextNode = node;
}else {
System.out.println("节点重复!");
}
}
/**
* 按照新节点的内容修改
* @param newNode
*/
public void updateNode(LinkedNode newNode){
//当遍历到的节点的no与新节点的node相等时修改
LinkedNode temp = head;
boolean flag = Boolean.FALSE;
while (true){
if(temp.no == newNode.no){
flag = Boolean.TRUE;
break;
}
if(temp.nextNode ==null){
break;
}
temp = temp.nextNode;
}
//flag为true时才能修改,否则就是没有找到匹配的节点
if(flag){
temp.name=newNode.name;
temp.nickName = newNode.nickName;
}else{
System.out.println("没有匹配到节点!!");
}
}
/**
* 展示列表的所有节点
*/
public void showList(){
LinkedNode temp = head;
while(true){
if(temp.nextNode == null)
break;
System.out.println(temp.toString());
temp = temp.nextNode;
}
System.out.println(temp.toString());
//打印链表的节点
}
/**
* 获取链表的长度
* @return
*/
public int getLength(){
LinkedNode temp = head;
int i = 0;
while(true){
if(temp.nextNode == null)
break;
i++;
temp = temp.nextNode;
}
return i;
}
/**
* 删除一个节点
* @param deleteNode
*/
public void deleteNode(LinkedNode deleteNode){
//先找到编号为deleteNode的编号相等的编号
LinkedNode temp = head;
boolean flag = Boolean.FALSE;
while (true){
if(temp.nextNode ==null)
break;
if (temp.nextNode.no == deleteNode.no){
flag = Boolean.TRUE;
break;
}
temp = temp.nextNode;
}
if(flag){
temp.nextNode = temp.nextNode.nextNode;
}else {
System.out.println("没有找到要删除的节点!");
}
}
}
/**
* 节点类
*/
class LinkedNode{
int no;
String name;
String nickName;
LinkedNode nextNode;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public LinkedNode getNextNode() {
return nextNode;
}
public void setNextNode(LinkedNode nextNode) {
this.nextNode = nextNode;
}
public LinkedNode(int no, String name, String nickName){
this.no=no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "LinkedNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
单链表实现增删改查,反转
最新推荐文章于 2023-08-15 11:11:20 发布