链表的定义
链表是一种物理存储单元上非连续、非顺序的存储结构,其物理结构不能只管的表示数据元素的逻辑顺序,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列的结点(链表中的每一个元素称为结点)组成,结点可以在运行时动态生成。
单向链表
定义
单向链表是链表的一种,它由多个结点组成,每个结点都由一个数据域和一个指针域组成,数据域用来存储数据,指针域用来指向其后继结点。链表的头结点的数据域不存储数据,指针域指向第一个真正存储数据的结点。
单向链表API设计
单向链表的代码实现
public class LinkedList<T> implements Iterable<T>{
//记录首结点
private Node head;
private int N; //链表的长度
public LinkedList(){
//初始化头结点
head=new Node(null,null);
N=0;
}
//结点类
private class Node{
T item;
Node next;
public Node(T item,Node next){
this.item=item;
this.next=next;
}
}
//空置线性表
public void clear(){
head.next=null;
head.item=null;
N=0;
}
//判断线性表是否为空,是返回true,否返回false
public boolean isEmpty(){
return N==0;
}
//获取线性表中元素的个数
public int length(){
return N;
}
//获取并返回线性表中第i个元素的值
public T get(int i){
Node n=head;
for(int index=0;index<=i;index++){
n=n.next;
}
return n.item;
}
//往线性表中添加一个元素
public void insert(T t){
Node n=head;
while (n.next !=null){
n=n.next;
}
Node newNode=new Node(t,null);
n.next=newNode;
N++;
}
//在线性表的第i个元素之前插入一个值为t的数据元素
public void insert(int i,T t){
Node pre=head;
//找到第i-1的结点
for(int index=0;index<=i-1;index++){
pre=pre.next;
}
//位置i的结点
Node curr=pre.next;
//构建新的结点,让新结点指向位置i的结点
Node newNode=new Node(t,curr);
//让之前的结点指向新的结点
pre.next=newNode;
//长度+1
N++;
}
//删除并返回线性表中第i个数据元素
public T remove(int i){
Node pre=head;
//找到第i-1的结点
for(int index=0;index<=i-1;index++){
pre=pre.next;
}
//位置i的结点
Node curr=pre.next;
//前一结点指向下一个结点,删除当前结点
pre.next=curr.next;
//长度-1
N--;
return curr.item;
}
//返回线性表中首次出现的指定的数据元素的位序号,若不存在,则返回-1
public int indexOf(T t){
Node n=head;
for(int index=0;n.next!=null;index++){
n=n.next;
if(n.item==t){
return index;
}
}
return -1;
}
@Override
public Iterator<T> iterator() {
return new LIterator();
}
private class LIterator implements Iterator<T>{
private Node n;
public LIterator(){
this.n=head;
}
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public T next() {
n=n.next;
return n.item;
}
}
}
//测试类
public class LinkedListTest {
public static void main(String[] args){
LinkedList<String> list=new LinkedList<>();
list.insert("one");
list.insert("three");
list.insert(0,"four");
list.insert("five");
list.insert(3,"two");
System.out.println("length="+list.length());
for(String s:list){
System.out.println(s);
}
System.out.println("**************");
System.out.println(list.get(1));
System.out.println("***************");
String remove = list.remove(1);
for(String s:list){
System.out.println(s);
}
}
}
双向链表
定义
双向链表也叫双向表,是链表的一种,它由多个结点组成,每个结点都由一个数据域和两个指针域组成,数据域用来存储数据,其中一个指针域用来指向其后继结点,另一个指针域用来指向前驱结点。链表的头结点的数据域不存储数据,指向前驱结点的指针域值为null,指向后继结点的指针域指向第一个真正存储数据的结点。
双向链表API设计
双向链表的代码实现
public class TowWayLinkList<T> implements Iterable<T>{
private Node head; //记录首结点
private Node last; //记录尾结点
private int N; //记录链表的长度
public TowWayLinkList(){
head=new Node(null,null,null);
last=null;
N=0;
}
//结点类
private class Node{
public Node pre;
public Node next;
public T item;
public Node(T item,Node pre,Node next){
this.item=item;
this.pre=pre;
this.next=next;
}
}
//空置线性表
public void clear(){
head.item=null;
head.next=null;
head.pre=null;
last=null;
N=0;
}
//判断线性表是否为空,是返回true,否返回false
public boolean isEmpty(){
return N==0;
}
//获取线性表中元素的个数
public int length(){
return N;
}
//获取并返回线性表中第i个元素的值
public T get(int i){
Node n=head;
for(int index=0;index<=i;index++){
n=n.next;
}
return n.item;
}
//往线性表中添加一个元素
public void insert(T t){
//判断链表是否为空
if(last==null){
last=new Node(t,head,null);
head.next=last;
}else{
Node oldLast=last;
Node newNode=new Node(t,oldLast,null);
oldLast.next=newNode;
last=newNode;
}
N++;
}
//在线性表的第i个元素之前插入一个值为t的数据元素
public void insert(int i,T t){
//找到第i-1的结点
Node pre=head;
for(int index=0;index<=i-1;index++){
pre=pre.next;
}
//位置i的结点
Node curr=pre.next;
//构建新的结点
Node newNode=new Node(t,pre,curr);
curr.pre=newNode;
pre.next=newNode;
//元素个数+1
N++;
}
//删除并返回线性表中第i个数据元素
public T remove(int i){
//找到第i-1的结点
Node pr=head;
for(int index=0;index<=i-1;index++){
pr=pr.next;
}
//位置i的结点
Node curr=pr.next;
//位置i+1的结点
Node curr_next=curr.next;
pr.next=curr_next;
curr_next.pre=pr;
//元素个数-1
N--;
return curr.item;
}
//返回线性表中首次出现的指定的数据元素的位序号,若不存在,则返回-1
public int indexOf(T t){
Node n=head;
for(int i=0;n.next!=null;i++){
n=n.next;
if(n.item==t){
return i;
}
}
return -1;
}
//获取第一个元素
public T getFirst(){
if(isEmpty()){
return null;
}
return head.next.item;
}
//获取最后一个元素
public T getLast(){
if(isEmpty()){
return null;
}
return last.item;
}
@Override
public Iterator<T> iterator() {
return new TIterator();
}
private class TIterator implements Iterator{
private Node n=head;
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public Object next() {
n=n.next;
return n.item;
}
}
}
//测试类
public class TwoWayLinkListTerst {
public static void main(String[] args){
TowWayLinkList<String> list=new TowWayLinkList<>();
list.insert("one1");
list.insert("two2");
list.insert("three3");
list.insert(1,"four4");
list.insert(3,"five5");
for(String s:list){
System.out.println(s);
}
//length
System.out.println(list.length());
String remove = list.remove(1);
System.out.println(remove);
System.out.println(list.getFirst());
System.out.println(list.getLast());
System.out.println("**********");
list.clear();
for(String s:list){
System.out.println(s);
}
System.out.println(list.length());
}
}
链表的时间复杂度分析
1.get(int i):每一次查询,都需要从链表的头部开始,依次向后查找,随着数据元素N的增多,比较的元素越多,时间复杂度为O(n)
2.insert(int i,T t):每一次插入,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n);
3.remove(int i):每一次移除,需要先找到i位置的前一个元素,然后完成插入操作,随着数据元素N的增多,查找的元素越多,时间复杂度为O(n)
4.相比较顺序表,链表插入和删除的时间复杂度虽然一样,但仍然有很大的优势,因为链表的物理地址是不连续的,它不需要预先指定存储空间大小,或者在存储过程中涉及到扩容等操作,同时它并没有涉及的元素的交换。
5.相比较顺序表,链表的查询操作性能会比较低。因此,如果我们的程序中查询操作比较多,建议使用顺序表,增删操作比较多,建议使用链表。
链表的应用
链表反转
需求:原链表中数据为:1->2->3->4
反转后的链表:4->3->2->1
反转原理
反转API设计
public void reverse():对整个链表反转
public Node reverse(Node curr):反转链表中的某个结点curr,并把反转后的curr结点返回
代码实现
//对整个链表反转
public void reverse(){
//当前是空链表,不需要反转
if(N==0){
return;
}
reverse(head.next);
}
//反转链表中的某个结点curr,并把反转后的curr节点返回
public Node reverse(Node curr){
//递归的出口,已经到了最后一个元素
if(curr.next==null){
//反转后,头结点应该指向原链表最后一个元素
head.next=curr;
return curr;
}
Node pre=reverse(curr.next);
pre.next=curr;
curr.next=null;
return curr;
}
快慢指针
快慢指针指的是定义两个指针,这两个指针的移动速度一块一慢,以此来制造出自己想要的差值,这个差值可以然我们找到链表上相应的结点。一般情况下,快指针的移动步长为慢指针的两倍
中间值问题
请完善测试类Test中的getMid方法,可以找出链表的中间元素值并返回。
利用快慢指针,我们把一个链表看成一个跑道,假设a的速度是b的两倍,那么当a跑完全程后,b刚好跑一半,以此来达到找到中间节点的目的。
代码实现
public class MIdTest {
public static void main(String[] args){
Node<String> first=new Node<>("aa",null);
Node<String> second=new Node<>("bb",null);
Node<String> third=new Node<>("cc",null);
Node<String> fourth=new Node<>("dd",null);
Node<String> fifth=new Node<>("ee",null);
Node<String> six=new Node<>("ff",null);
Node<String> seven=new Node<>("gg",null);
first.next=second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
fifth.next=six;
six.next=seven;
String mid=getMid(first);
System.out.println("中间值="+mid);
}
public static String getMid(Node<String> first){
Node<String> fast=first;
Node<String> slow=first;
while(fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
return slow.item;
}
//结点类
private static class Node<T>{
private Node next;
private T item;
public Node(T item,Node next){
this.item=item;
this.next=next;
}
}
}
单向链表是否有环问题
使用快慢指针的思想,还是把链表比作一条跑道,链表中有环,那么这条跑道就是一条圆环跑道,在一条圆环跑道中,两个人有速度差,那么迟早两个人会相遇,只要相遇那么就说明有环。
代码实现
public class CircleTest {
public static void main(String[] args){
Node<String> first=new Node<>("aa",null);
Node<String> second=new Node<>("bb",null);
Node<String> third=new Node<>("cc",null);
Node<String> fourth=new Node<>("dd",null);
Node<String> fifth=new Node<>("ee",null);
Node<String> six=new Node<>("ff",null);
Node<String> seven=new Node<>("gg",null);
first.next=second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
fifth.next=six;
six.next=seven;
seven.next=third;
boolean circle = isCircle(first);
System.out.println("链表中是否有环:"+circle);
}
public static boolean isCircle(Node<String> first){
Node<String> fast=first;
Node<String> slow=first;
while(fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast.equals(slow)){
return true;
}
}
return false;
}
//结点类
private static class Node<T>{
private Node next;
private T item;
public Node(T item,Node next){
this.item=item;
this.next=next;
}
}
}
有环链表入口问题
当快慢指针相遇时,我们可以判断到链表中有环,这时重新设定一个新指针指向链表的起点,且步长与慢指针一样为1,则慢指针与“新”指针相遇的地方就是环的入口。
代码实现
public class EntranceTest {
public static void main(String[] args){
Node<String> first=new Node<>("aa",null);
Node<String> second=new Node<>("bb",null);
Node<String> third=new Node<>("cc",null);
Node<String> fourth=new Node<>("dd",null);
Node<String> fifth=new Node<>("ee",null);
Node<String> six=new Node<>("ff",null);
Node<String> seven=new Node<>("gg",null);
first.next=second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
fifth.next=six;
six.next=seven;
seven.next=second;
Node entrance = getEntrance(first);
System.out.println("链表中环的入口为:"+entrance.item);
}
public static Node getEntrance(Node<String> first){
Node<String> fast=first;
Node<String> slow=first;
Node<String> tmp=null;
while(fast!=null && fast.next!=null){
fast=fast.next.next;
slow=slow.next;
if(fast.equals(slow)){
tmp=first;
continue;
}
if(tmp!=null){
tmp=tmp.next;
if(tmp.equals(slow)){
return tmp;
}
}
}
return null;
}
//结点类
private static class Node<T>{
private Node next;
private T item;
public Node(T item,Node next){
this.item=item;
this.next=next;
}
}
}
约瑟夫问题
问题描述
传说有这样一个故事,在罗马人占领乔塔帕特后,39 个犹太人与约瑟夫及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,第一个人从1开始报数,依次往后,如果有人报数到3,那么这个人就必须自杀,然后再由他的下一个人重新从1开始报数,直到所有人都自杀身亡为止。然而约瑟夫和他的朋友并不想遵从。于是,约瑟夫要他的朋友先假装遵从,他将朋友与自己安排在第16个与第31个位置,从而逃过了这场死亡游戏。
问题转换
41个人坐一圈,第一个人编号为1,第二个人编号为2,第n个人编号为n。
1.编号为1的人开始从1报数,依次向后,报数为3的那个人退出圈;
2.自退出那个人开始的下一个人再次从1开始报数,以此类推;
3.求出最后退出的那个人的编号。
解题思路:
1.构建含有41个结点的单向循环链表,分别存储1~41的值,分别代表这41个人;
2.使用计数器count,记录当前报数的值;
3.遍历链表,每循环一次,count++;
4.判断count的值,如果是3,则从链表中删除这个结点并打印结点的值,把count重置为0;
代码实现
public class YuesefuTest {
public static void main(String[] args){
//1.构建循环链表
Node<Integer> first=null;
//记录前一个结点
Node<Integer> pre=null;
for(int i=1;i<=41;i++){
//第一个元素
if(i==1){
first=new Node<>(i,null);
pre=first;
continue;
}
Node newNode=new Node(i,null);
pre.next=newNode;
pre=newNode;
//最后一个元素
if(i==41){
pre.next=first;
}
}
//2.使用count,记录当前的报数值
int count=0;
//3.遍历链表,每循环一次,count++
Node<Integer> node=first;
Node<Integer> before=null;
while(node!=node.next){
//4.判断count的值,如果是3,则从链表中删除这个结点,并打印结点的值,把count置0
count++;
if(count==3){
//删除当前结点
before.next=node.next;
System.out.print(node.item+",");
count=0;
node=node.next;
}else{
before=node;
node=node.next;
}
}
//打印最后的那个人的信息
System.out.println(node.item);
}
private static class Node<T>{
private T item;
private Node next;
public Node(T item,Node next){
this.item=item;
this.next=next;
}
}
}