1.不带头结点的循环单链表**
`
import LinkList.LinkList.Node;
class LinkList { // 定义链表类
class Node { // 定义结点类
public int data;
public Node next;
public Node() {
next = null;
}
public Node(int value) { // 构造函数
this.data = value;
next = null;
}
}
public Node head;
public Node tail;
LinkList() {
head = tail;
}
public boolean isEmpty() { // 判空函数
return (head == null);
}
public int length() {
int count = 0;
Node p = head;
if(head == null){
return 0;
}
do{
count++;
p = p.next;
}while(p!=head);
return count;
}
public Node getINode(int i) {
if (i < 0 || i > length()) {
System.out.println("位置参数不合法!");
return null;
}
Node p = head;
for (int j = 0; j < i - 1; j++) { // j从0开始,循环i-1次
p = p.next;
}
return p;
}
public void displayList() { // 输出函数
System.out.print("循环单链表为:");
Node p = head;
if(head == null){
System.out.println("循环单链表为空!");
return;
}
do{
System.out.print(p.data + "->");
p = p.next;
}while(p!=head);
/* while(p!=head) //当只有一个结点时,输出不了
{
System.out.print(p.data + "->");
p = p.next;
}*/
System.out.println("nul");
}
public void insertHead(int value) {
Node newNode = new Node(value);
if (head == null) { //这里必须判断,若不判断则会空指针异常
head = tail = newNode;
tail.next = head;
return; //直接return,则不用使用else
}
newNode.next = head;
head = newNode;
tail.next = head;
}
public void insertTail(int value) {
Node newNode = new Node(value);
if (head == null) { //这里也必须判断
head = tail = newNode;
tail.next = head;
return;
}
// newNode.next = head;
tail.next = newNode;
tail = newNode;
tail.next = head;
}
public void insertINode(int i, int value) { // 需要length(),getINode(int i);
Node newNode = new Node(value);
if (i == 1) { // 在第一个位置插入节点,相当于头插
insertHead(value);
return;
}
Node p = getINode(i - 1); // 获得第i个位置的前一个结点
if (p != null) {
newNode.next = p.next;
p.next = newNode;
}
}
public void deleteHead() {
if (isEmpty()) {
System.out.println("循环单链表为空!");
return;
}
Node p = head;
if (length() == 1) {
head = tail = null;
System.out.println("循环链表为空!");
return;
}
head = p.next;
}
public void deleteTail() {
if (isEmpty()) {
System.out.println("循环单链表为空!");
return;
}
Node p = getINode(length() - 1);
if (length() == 1) {
head = tail = null;
System.out.println("循环链表为空!");
return;
}
p.next = head; //维护循环结构
tail = p;
}
public void deleteINode(int i) {
if (i == 1) {
deleteHead();
return;
}
Node p = getINode(i - 1);
Node del_node = p.next;
p.next = p.next.next;
}
}
public class CircLinkList {
public static void main(String[] args) {
LinkList l = new LinkList();
// l.insertHead(3);
l.displayList();
// l.deleteHead();
l.deleteTail();
// l.insertTail(4);
// l.insertHead(1);
l.displayList();
l.insertINode(0, 9); // 不能在第0个结点插
// l.insertINode(1, 0); // 在第一个位置插结点,相当于头插
// l.insertINode(3, 2);
l.displayList();
l.deleteHead();
l.deleteTail();
l.deleteINode(1); //删除第一个位置的结点,相当于头删
// l.deleteINode(2);
// l.displayList();
int len = l.length();
System.out.println("共有"+len+"个结点!");
// Node node = l.getINode(2);
// System.out.println(“第i个结点为:” + node.data);
}
}
`
2.带头结点的循环单链表
import LinkList.List.Node;
//带有头结点的循环单链表
class List{
class Node{
int data;
Node next;
public Node(){}
public Node(int value) {
this.data =value;
next = null;
}
}
public Node head;
public Node tail;
public List(){
this.head = new Node(); //注意对head,tail进行初始化,否则默认为null
this.tail = new Node();
head = tail;
tail.next = head; //若不写,当链表中没有结点时,会出现错误
}
public boolean isEmpty() {
return (head == tail); // 当链表为空时,只有一个头结点
}
public int length(){
int count = 0;
Node p = head.next;
while(p!=head){
count++;
p = p.next;
}
return count;
}
public void getHeadNode(){ //获取头结点函数
head.data = length();
System.out.println("head.data:"+head.data);
}
public Node getINode(int i){
if(i < 0 || i > length()){
System.out.println("位置i参数不合法!");
return null;
}
Node p = head.next;
for(int j = 0;j<i-1;j++){
p=p.next;
}
return p;
}
public void displayList() {
Node p = head.next;
System.out.println("共有"+length()+"个结点!");
while (p != head) {
System.out.print(p.data + "->");
p = p.next;
}
System.out.println("nul");
}
public void insertHead(int value){ //头插需要分情况,尾插不需要
Node newNode = new Node(value);
if(head == tail){ //只有头结点时,需要移动tail的位置
head.next = newNode;
tail = newNode;
tail.next = head;
return;
}
newNode.next = head.next;
head.next = newNode;
}
public void insertTail(int value) {
Node newNode = new Node(value);
tail.next = newNode;
tail = newNode;
tail.next = head;
}
public void insetINode(int i,int value){
if(i==1){
insertHead(value);
return;
}
Node p = getINode(i-1);
Node newNode = new Node(value);
if(p!=null){ //当获取i-1个结点为空时,直接return
newNode.next = p.next;
p.next = newNode;
}
}
public void deletetHead(){
if(isEmpty()){
System.out.println("循环单链表为空!");
return;
}
Node p = head.next;
head.next = p.next;
}
public void deleteTail(){
if(isEmpty()){
System.out.println("循环单链表为空!");
return;
}
if (length() == 1) {
tail = head;
tail.next = head; //维护循环结构
System.out.println("删除后循环链表为空!");
return;
}
Node p = getINode(length()-1);
if(p!=null){
p.next = head;
tail = p;
}
}
public void deleteINode(int i){
if(isEmpty()){
System.out.println("循环单链表为空!");
return;
}
if(i==1){ //当i==1,相当于头删
deletetHead();
return;
}
Node p = getINode(i-1);
if(p!=null){
p.next = p.next.next;
// tail.next = head;
}
}
}
public class HeadCircLinkList {
public static void main(String[] args) {
List L = new List();
L.insertHead(2);
L.insertTail(3);
L.insertTail(4);
L.displayList();
// L.insetINode(0, 1);
// L.insetINode(1, 1);
// L.displayList();
// L.insetINode(2, 2);
// L.displayList();
// L.deletetHead();
// L.deleteTail();
// L.deleteINode(0); //位置不合法
// L.deleteINode(1);
// L.deleteINode(2);
// L.displayList();
L.getHeadNode();
Node node = L.getINode(1);
System.out.println("第i个结点是:"+node.data);
}
}
总结:在java中,一般把成员变量定义为private,比较安全,也就是实现数据封装。当定义为private就需要使set,get方法改变属性。