单链表实现

package com.bean;

public class Node<T> {
private T data;
private Node<T> next;
private Node<T> previous;

public Node(){
}

public Node(T data){
this.data = data;
}

public Node(T data, Node<T> next){
this.data = data;
this.next = next;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}

public Node<T> getNext() {
return next;
}

public void setNext(Node<T> next) {
this.next = next;
}

public Node<T> getPrevious() {
return previous;
}

public void setPrevious(Node<T> previous) {
this.previous = previous;
}
}


package com.acm;

import java.util.ArrayList;
import java.util.List;

import com.bean.Node;

public class SingleCycleLinkedList<T> {
private Node<T> next;

private long count = 0;


public SingleCycleLinkedList(){
generate(new Node<T>(), new ArrayList<T>());
}

public SingleCycleLinkedList(Node<T> head, List<T> list){
if(null != list){
generate(head, list);
count = list.size();
}
}


public void print(Node<T> head){
Node<T> current = head;
do{
System.out.println(current.getData());
current = current.getNext();
}while(head != current);
}


public int size(Node<T> head){
Node<T> current = head;
int count = 0;
do{
count++;
current = current.getNext();
}while(head != current);
return count;
}

public void generate(Node<T> head, List<T> list){
Node<T> temp;
Node<T> current = head;

for(T t : list){
temp = new Node<T>(t);
current.setNext(temp);
current = temp;
}
current.setNext(head);
}

public boolean checkOne(Node<T> head){
Node<T> current = head;
int count = 0;
do{
count++;
current = current.getNext();
}while(head != current);
if(count == 1){
return true;
}
return false;
}

public Node<T> getNext() {
return next;
}

public void setNext(Node<T> next) {
this.next = next;
}

public long getCount() {
return count;
}

public void setCount(long count) {
this.count = count;
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值