package com.structure.demo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SingleLinkedList singleLinkedList = new SingleLinkedList();
HeroNode n1 = new HeroNode("宋江", "及时雨", 1);
HeroNode n2 = new HeroNode("杨志", "青面兽", 2);
HeroNode n3 = new HeroNode("花荣 ", "小李广", 3);
HeroNode n4 = new HeroNode("李逵", "黑旋风", 4);
//插入节点
singleLinkedList.add(n1);
singleLinkedList.add(n2);
singleLinkedList.add(n3);
singleLinkedList.add(n4);
//单链表按顺序插入节点
/* singleLinkedList.addNodeByOrder(n1);
singleLinkedList.addNodeByOrder(n3);
singleLinkedList.addNodeByOrder(n2);
singleLinkedList.addNodeByOrder(n4);
HeroNode updateNode = new HeroNode("花荣11 ", "小李广11", 5);
singleLinkedList.update(updateNode);
HeroNode delNode = new HeroNode("花荣11 ", "小李广11", 4);
singleLinkedList.deleteNode(delNode);
singleLinkedList.show();
//获取链表的长度
int length = singleLinkedList.getLength();
Log.i("tag", length + "====length=======");
HeroNode lastIndexNode = singleLinkedList.getLastIndexNode(3);
Log.i("tag",lastIndexNode.toString());*/
// singleLinkedList.reversePrint();
//获取链表的倒数第k个节点
singleLinkedList.reverse();
singleLinkedList.show();
}
}
class SingleLinkedList {
HeroNode head = new HeroNode("", "", 0);
/**
* 添加节点到最后
*
* @param heroNode
*/
public void add(HeroNode heroNode) {
if (head == null) {
return;
}
HeroNode temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
temp.next = heroNode;
}
/**
* 展示所有节点
*/
public void show() {
if (head.next == null) {
return;
}
HeroNode temp = head.next;
while (true) {
if (temp == null) {
break;
}
Log.i("tag", temp.toString());
temp = temp.next;
}
}
/**
* 按顺序添加节点
*
* @param heroNode
*/
public void addNodeByOrder(HeroNode heroNode) {
HeroNode temp = head;
//插入的编号是否存在
boolean flag = false;
while (true) {
//已经到链表的最后
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
//找到了插入的位置
break;
} else if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
Log.i("tag", "编号已经存在");
} else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
/**
* 更新某个节点
*
* @param heroNode
*/
public void update(HeroNode heroNode) {
if (head.next == null) {
Log.i("tag", "链表为空");
return;
}
HeroNode temp = head;
//是否找到节点
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next.name = heroNode.name;
temp.next.nickName = heroNode.nickName;
} else {
Log.i("tag", "没有找到节点" + heroNode.no);
}
}
/**
* 删除节点
*
* @param heroNode
*/
public void deleteNode(HeroNode heroNode) {
if (head.next == null) {
Log.i("tag", "链表为空");
return;
}
HeroNode temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
Log.i("tag", "没有找到该节点");
}
}
/**
* 获取链表的节点的长度
*
* @return
*/
public int getLength() {
if (head.next == null) {
return 0;
}
HeroNode temp = head;
int count = 0;
while (true) {
if (temp.next == null) {
break;
}
count++;
temp = temp.next;
}
return count;
}
/**
* 单链表找倒数第k个节点
* @param index
* @return
*/
public HeroNode getLastIndexNode(int index) {
if (head.next == null) {
return null;
}
if (index == 0 || index >= getLength()) {
Log.i("tag", "节点不存在");
return null;
}
HeroNode temp = head.next;
for (int i = 0; i < getLength() - index; i++) {
temp = temp.next;
}
return temp;
}
/**
* 逆序打印链表数据
*/
public void reversePrint(){
if (head.next == null){
Log.i("tag","链表为空");
return;
}
HeroNode temp = head.next;
Stack<HeroNode> heroNodes = new Stack<>();
while (temp!=null){
heroNodes.push(temp);
temp = temp.next;
}
for (int i = 0; i <getLength() ; i++) {
Log.i("tag",heroNodes.pop().toString());
}
}
/**
* 将链表进行反转
*/
public void reverse(){
if (head.next == null){
Log.i("tag","链表为空");
return;
}
HeroNode cur = head.next;
HeroNode next = null;
HeroNode reverseHead = new HeroNode("","",1);
while (cur!=null){
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
}
class HeroNode {
String name;
String nickName;
int no;
HeroNode next;
public HeroNode(String name, String nickName, int no) {
this.name = name;
this.nickName = nickName;
this.no = no;
}
@Override
public String toString() {
return "HeroNode{" +
"name='" + name + '\'' +
", nickName='" + nickName + '\'' +
", no=" + no +
'}';
}
}
数据结构和算法——单链表
最新推荐文章于 2023-05-21 13:26:57 发布