Java实现数据结构链表
定义一个node实体类
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname +"]";
}
}
对链表操作的类
class SingleLinkedList{
private HeroNode head= new HeroNode(0, "", "");
public HeroNode getHead() {
return head;
}
//添加数据,不排序
public void add(HeroNode node) {
HeroNode temp = head;
while(true) {
if (temp.next==null) {
break;
}
temp=temp.next;
}
temp.next=node;
}
//添加数据,从小到大
public void addByOrder(HeroNode newHeroNode) {
HeroNode temp = head;
boolean flag=false;
while(true) {
if (temp.next==null) {
break;
}
else if(newHeroNode.no<temp.next.no){
break;
}
//此处必须为temp.next,不然no检索的和前边两条件不对应,会无限添加数据
else if(temp.next.no==newHeroNode.no) {
flag=true;
break;
}
temp=temp.next;
}
if (flag) {
System.out.println("编号已存在,插入失败");
}else {
newHeroNode.next=temp.next;
temp.next=newHeroNode;
}
}
//更新链表里的数据
public void update(HeroNode updateHeroNode) {
if (head.next==null) {
System.out.println("链表为空,没有数据");
}
HeroNode temp = head.next;
boolean flag=false;
while(true) {
if (temp==null) {
break;
}
else if (temp.no==updateHeroNode.no) {
flag=true;
break;
}
temp=temp.next;
}
if (flag) {
temp.name=updateHeroNode.name;
temp.nickname=updateHeroNode.nickname;
}
else {
System.out.println("要更新的编号未找到");
}
}
//删除链表中的元素
public void del(int no) {
HeroNode temp = head;
boolean flag=false;
while(true){
if (temp==null) {
break;
}
else if (temp.next.no==no) {
flag=true;
break;
}
temp=temp.next;
}
if (flag) {
temp.next=temp.next.next;
}
else {
System.out.println("该编号的元素未找到");
}
}
//显示链表[遍历]
public void list() {
//判断是否为空
if (head.next==null) {
System.out.println("链表为空表,没有数据");
return;
}
//因为头节点,不能动,因此我们需要一个辅助变量来遍历
HeroNode temp = head.next;
while(true) {
//判断是否为最后
if (temp==null) {
break;
}
else {
//输出节点信息
System.out.println(temp);
//temp后移
temp=temp.next;
}
}
}
}
还有一些其他操作
public class SingleLinkedListDemo {
public static void main(String[] args) {
SingleLinkedList singleLinkedList = new SingleLinkedList();
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
HeroNode heroNode5 = new HeroNode(2, "aaa", "分为");
singleLinkedList.addByOrder(heroNode1);
singleLinkedList.addByOrder(heroNode4);
singleLinkedList.addByOrder(heroNode3);
singleLinkedList.addByOrder(heroNode2);
System.out.println("反转前数据~~~~~~~~~~~~~");
singleLinkedList.list();
System.out.println("逆序打印~~~~~~~~~~~~");
reversePrint(singleLinkedList.getHead());
reversetList(singleLinkedList.getHead());
System.out.println("反转后数据~~~~~~~~~~~");
singleLinkedList.list();
//逆序打印,不改变链表结构
//可以利用栈这个数据结构,将各个节点压入到栈中,然后利用栈的先进后出的特点,就实现了逆序打印的效果
public static void reversePrint(HeroNode head) {
if (head.next==null) {
System.out.println("栈为空~~~~~~");
return;
}
//创建一个栈结构
Stack<HeroNode> stack= new Stack<HeroNode>();
HeroNode cur=head.next;
while(cur!=null) {
stack.add(cur);
cur=cur.next;
}
while(stack.size()>0) {
System.out.println(stack.pop());
}
}
//将单链表反转
public static void reversetList(HeroNode head) {
if (head.next==null||head.next.next==null) {
return;
}
HeroNode head1=new HeroNode(0, "", "");
//定义一个辅助的指针(变量),帮助我们遍历原来的链表
HeroNode cur=head.next;
// 指向当前节点[cur]的下一个节点
HeroNode next;
//遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表reverseHead 的最前端
while(cur!=null) {
next=cur.next;//先暂时保存当前节点的下一个节点,因为后面需要使用
cur.next=head1.next;//将cur的下一个节点指向新的链表的最前端
head1.next=cur;//将cur 连接到新的链表上
cur=next;//让cur后移
}
//将head.next 指向 reverseHead.next , 实现单链表的反转
head.next=head1.next;
return;
}
//查找单链表中的倒数第k个结点
//思路
//1. 编写一个方法,接收head节点,同时接收一个index
//2. index 表示是倒数第index个节点
//3. 先把链表从头到尾遍历,得到链表的总的长度 getLength
//4. 得到size 后,我们从链表的第一个开始遍历 (size-index)个,就可以得到
//5. 如果找到了,则返回该节点,否则返回nulll
public static HeroNode findLastIndexNode(int index,HeroNode head) {
if (head.next==null) {
return null;
}
else if (index<=0||index>count(head)) {
System.out.println("该序列对应的数据未找到");
return null;
}
HeroNode temp=head.next;
for(int i=0;i<count(head)-index;i++) {
temp=temp.next;
}
return temp;
}
//判断链表中有效节点的个数
public static int count(HeroNode head) {
if (head.next==null) {
return 0;
}
int count=0;
HeroNode temp=head.next;
while(temp!=null) {
count++;
temp=temp.next;
}
return count;
}
}