线性表链式结构的定义:
结点 = 数据域+指针域:数据域包含数据信息,指针域包含指针。
指针:指示直接后继的存储位置。
单链表:每个结点只含一个指针。
头结点,第一个结点,第二个…………
头结点中的数据域可以不存任何信息,头结点不一定是链表必须要素。若没有头结点,头指针指向第一个结点;若有头结点,头指针指向头结点。
头指针是链表必要元素。
一、增删改查:
//添加演示
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(3, "林冲", "豹子头");
HeroNode heroNode7 = new HeroNode(7, "李逵", "黑旋风");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(heroNode1);
singleLinkedList.add(heroNode7);
singleLinkedList.add(heroNode3);
singleLinkedList.add(heroNode4);
singleLinkedList.showList();
singleLinkedList.udpadate(heroNode4);
singleLinkedList.showList();
System.out.println("********************");
singleLinkedList.delete(1);
singleLinkedList.showList();
}
}
class SingleLinkedList {
//头结点
private HeroNode head = new HeroNode(0, "", "");
//添加节点
public void add(HeroNode heroNode) {
HeroNode temp=head;
boolean flag=false;//添加编号是否存在
while (true){//遍历链表
if (temp.next==null)//链表为空,可以增加结点
break;
if (temp.next.no>heroNode.no)//可以插入节点
break;
if (temp.next.no == heroNode.no){//该节点编号已经存在
flag=true;
break;
}
temp=temp.next;
}
if (flag)
System.out.println(heroNode.toString()+"添加失败,已经存在编号");
else {
heroNode.next = temp.next;
temp.next = heroNode;
}
}
//修改节点信息
public void udpadate(HeroNode newHeroNode){
if (head.next==null){
System.out.println("链表为空");
return;
}
HeroNode temp=head.next;
boolean flag=false;
while (true){
if (temp==null)
break;//没找到
if (temp.no== newHeroNode.no){//找到需要修改的结点
flag=true;
break;
}
temp=temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickname= newHeroNode.nickname;
}
else
System.out.println("没找到编号为"+newHeroNode.no+"的结点");
}
//删除结点
public void delete(int no){
HeroNode temp=head;
boolean flag=false;
while (true){
if (temp.next==null)//没找到
break;
if (temp.next.no==no){//找到待删除结点
flag=true;
break;
}
temp=temp.next;
}
if (flag){
temp.next=temp.next.next;
}
else
System.out.println("删除失败");
}
//显示链表
public void showList(){
HeroNode temp=head;
if (temp.next==null)
System.out.println("链表为空");
while (true){
if (temp.next==null)
break;
else{
temp=temp.next;
System.out.println(temp.toString());
}}}}
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;
public HeroNode(int no,String name,String nickname){
this.name=name;
this.no=no;
this.nickname=nickname;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
二、面试题
1、单链表的有效结点个数(不统计头结点)
public static int getLength(HeroNode head){//注意是静态方法
if (head.next==null)
return 0;
else{
int length=0;
HeroNode temp=head.next;
while (temp!=null){
length++;
temp=temp.next;
}
return length;
}
}
2、反转链表(更改原有链表)
public static void reverseLink(HeroNode head){
if (head.next==null||head.next.next==null){
System.out.println("链表为空");
return;
}
else{
HeroNode reverseHead=new HeroNode(0,"","");
HeroNode temp=head.next;
HeroNode cur=null;
while (temp!=null){
cur=temp;
temp=temp.next;
cur.next=reverseHead.next;
reverseHead.next=cur;
}
head.next=reverseHead.next;
}
}
3、不更改原有链表基础上打印反转链表(利用栈先进后出的特点)
public static void reversePrint(HeroNode head){
if (head.next==null){
return;}
Stack<HeroNode> stack=new Stack();
HeroNode temp=head.next;
while (temp!=null){
stack.push(temp);//入栈
temp=temp.next;
}
while (stack.size()>0)
System.out.println(stack.pop());//出栈
}
4、合并两个链表
public static void combine(SingleLinkedList sl1,SingleLinkedList sl2){
HeroNode temp=sl2.getHead().next;
HeroNode next=null;
HeroNode cur=sl1.getHead();
while (temp!=null){
next=temp.next;
while (cur!=null){
if ((cur.next!=null)&&(cur.next.no>temp.no)){//sl1未到末尾,加入结点
temp.next=cur.next;
cur.next=temp;
break;
}
else if ((cur.next==null)&&cur.no<temp.no)//sl1到了末尾,加入结点
cur.next=temp;
else if (cur.next.no==temp.no){//结点重复
break;}
else
cur=cur.next;
}
temp=next;
}
}