java向循环链表添加_java数据结构单向循环链表的实现

一、9a4509b04f73186547e37a8af94d45a8.png

由图可以看出,单项循环链表和普通单链表的区别在于尾节点,普通单链表的为节点指向一个空的位置,表示这个节点是链表的族最末端,而循环链表的尾节点则指向该链表的头结点,形成一个环状结构,首尾相连。

二、

1、空表的循环列表是头结点的后继指针指向首结点自身 head.next=head;

2、循环列表中,尾结点后继指针指向了头结点 rear.next=head

3、与单链表的区别还有一点,就是关于循环遍历链表时的判断条件,单链表的循环体的结束条件是p = = null,而单向循环链表的循环判断条件是p.next = = head,如下代码所示:

//单链表

Nodep = this.head

while(p!=null){

}

//单向循环链表

Nodep = this.head

while(p.next!=head){

}

3.1首先每个链表都是有节点组成的,每个节点都包含该节点所存储的数据和该节点后继得地址,所以需要定义一个节点类

/**

* 结点类

* @author Administrator

*/

public class Node{

private E data;//存储数据

private Node next;

public Node(E data){

this.data=data;

}

public Node(E data,Node next){

this.data=data;

this.next=next;

}

}

3.2循环链表的接口与单链表一致,为线性表

/**

* 链表的接口,包含所有

* 链表的基本方法

* @author Administrator

*

*/

public interface List{

//判断链表是否为空

boolean isEmpty();

//链表长度

int length();

//获取元素

E get(int index);

//根据index添加元素data

E set(int index,E data);

//根据index添加元素data

boolean add(E data);

//根据index插入元素

boolean add (int index,E data);

//删除指定位置的元素

E remove(int index);

//删除指定元素

boolean remove(E data);

//根据data移除所有相同元素

E removeAll(E data);

//清空链表

void clear();

//是否包含特定元素

boolean contains(E data);

//获取元素的位置

int indexOf(E data);

//根据data值查询最后一个出现在顺序表的下标

int lastIndexOf(E data);

// 输出格式

String toString();

}

3.3在SingLoop类中实现List接口,声明头结点和尾节点以及其构造方法

private Node head;//头结点

private Node rear;//尾节点

private int size;//链表的长度

public LoopSingle() {

head = null;

rear = null;

size = 0;

}

public LoopSingle(E[] arr) {

this();

for(E e:arr) {

addLast(e);

}

}

判断链表是否为空:由于链表的结构特点,头结点是链表的开始位置,所有判断链表是否为空,只要判断链表的头结点是为空

public boolean isEmpty() {

return size == 0&&head == null&&rear == null;

}

获取元素:由于链表的结构特点,获取链表首先声明变量count(从0开始)来表示结点指向的位置,需要依次按照后继指针循环直到获取结点从而取得结点存储的数据。从程序来看,链表获取元素在最坏情况下需要依次遍历所结点,最好情况下时间复杂度为O(1),最坏情况下时间复杂度O(n)。而特别注意结点的结束判断,避免死循环。

public E get(int index) {

if(index<0||index>=size) {

throw new IllegalArgumentException("查找角标非法");

}

if(index == 0){

return head.data;

}else if(index == size) {

return rear.data;

}else {

Node p = head;

for(int i = 0;i

修改指定位置的元素:在获取元素位置时,与获取元素位置相同,修改元素只是在获取元素后用新的存储数据替换旧的存储数据data。在时间复杂度上,与获取元素相同。

public void set(int index, E e) {

if(index<0||index>=size) {

throw new IllegalArgumentException("修改角标非法");

}

if(index == 0){

head.data = e;

}else if(index == size) {

rear.data = e;

}else {

Node p = head;

for(int i = 0;i

添加元素:单链表添加元素主要分为四种场景,a.空链表插入结点;b.头结点插入元素,新增结点为头结点;c.中间情况插入节点;d.尾结点插入元素,也就是在插入在单链表末尾

public void add(int index, E e) {

if(index<0||index>size) {

throw new IllegalArgumentException("插入角标非法");

}

Node n = new Node(e,null);

if(isEmpty()) {//为空的情况

head = n;

rear = n;

rear.next=head;

}else if(index == 0) {//头插

n.next = head;

head = n;

rear.next = head;

}else if(index == size){//尾插

n.next = head;

rear.next = n;

rear = n;

}else {//一般情况

Node p =head;

for(int i = 0;i

删除指定位置的元素:删除元素与插入结点类型,首先要先找到要删除指定位置的索引位置分为三种情况,删除头结点,删除中间位置结点,删除尾结点

public E remove(int index) {

if(index<0||index>=size) {

throw new IllegalArgumentException("删除角标非法");

}

E res = null;

if(size == 1) {

res = head.data;

head = null;

rear = null;

}else if(index == 0) {

res = head.data;

head = head.next;

rear.next = head;

}else if(index == size-1) {

res = rear.data;

Node p = head;

while(p.next!=rear) {

p=p.next;

}

p.next = rear.next;

rear = p;

}else {

Node p = head;

for(int i = 0;i

查找链表中是否存在某个元素:如果链表为空则返回-1,否则定义一个指针从第一个节点开始判断,每部循环都对index进行+1操作,如果查找到,则返回index,否则返回-1

public int find(E e) {

if(isEmpty()) {

return -1;

}

Node p = head;

int index = 0;

while(p.data!=e) {

p = p.next;

index++;

if(p == head) {

return -1;

}

}

return index;

}

清空链表:可以直接使头结点和尾节点指向空,size=0。

public void clear() {

head = null;

rear = null;

size = 0;

}

最后输出格式:

public String toString() {

StringBuilder sb = new StringBuilder();

sb.append("loopSingle:size = " + getsize() + "\n");

if(isEmpty()) {

sb.append("[]");

}else{

sb.append('[');

Node p = head;

while(true) {

sb.append(p.data);

if(p.next == head) {

sb.append(']');

break;

}else {

sb.append(',');

}

p = p.next;

}

}

return sb.toString();

}

总结:单向循环链表与普通链表的区别主要是在尾结点的判断,查询的时间复杂度都为O(1),插入和删除的时间复杂度为O(n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值