JAVA数据结构之线性表的链式存储结构——循环链表

循环链表的结构如下图所示:


说明:头结点的next指针指向循环链表中的第一个结点(下标为0),循环链表的最后一个结点的next指针指向第一个结点。下面是具体的实现代码。

1、结点类Node的实现代码如下所示:

public class Node {
	public Object data;//保存当前结点的数据
	public Node next;//指向下一个结点
	
	public Node(Object data){
		this.data = data;
	}
}
2、循环链表类CircleList的实现代码如下所示:

public class CircleList {
	private Node head;
	
	public CircleList(){
		head = new Node(0);
		head.next = null;
	}
	
	//在指定位置插入结点
	public boolean insert(Object data,int pos){
		boolean ret = (head != null) && (data != null) && (pos >= 0);
		
		if(ret){
			Node node = new Node(data);
			
			if(head.next == null){
				//插入的结点前,循环链表中没有结点。
				head.next = node;
				node.next = node;
			}else{
				if(pos >= (Integer)head.data){
					pos = (Integer)head.data;
				}
				
				Node currentNode = head.next;
				
				//若currentNode.next == head.next,就说明currentNode是最后一个结点
				for(int i = 0;(i < pos) && (currentNode.next != head.next);i++){
					currentNode = currentNode.next;
				}
				
				node.next = currentNode.next;
				currentNode.next = node;
				
				//插入位置的下标为0时
				if(pos == 0){
					head.next = node;
				}
			}
			
			head.data = (Integer)head.data + 1;
		}
		
		return ret;
	}
	
	//获取链表中下标为pos的结点
	public Object get(int pos){
		Object ret = null;
		
		if(head != null && pos >= 0 && pos < (Integer)head.data){
			Node node = head;//头结点
			
			//找到要删除的结点
			for(int i = 0;i<=pos;i++){
				node = node.next;
			}
			
			if(node != null){
				ret = node.data;
			}
		}
			
		return ret;
	}
	
	//删除链表中下标为pos的结点
	public Object delete(int pos){
		Object ret = null;
		
		if(head != null && pos >= 0 && pos < (Integer)head.data){
			Node node = head;//头结点
			Node currentNode = null;//要删除的结点
			
			//找到要删除结点的前一个结点
			for(int i = 0;i<pos;i++){
				node = node.next;
			}
			
			currentNode = node.next;
			
			//获取要删除结点的数据
			if(currentNode != null){
				ret = currentNode.data;
			}
			
			//要删除的结点是循环链表中的第一个结点
			if(head.next == currentNode){
				head.next = currentNode.next;
			}
			
			//删除结点
			node.next = currentNode.next;
			
			head.data = (Integer)head.data - 1;
		}
		
		return ret;
	}
	
	//清空链表
	public void clear(){
		if(head != null){
			head.data = 0;
			head.next = null;
		}
	}
	
	//注销链表
	public void destroy(){
		if(head != null){
			head = null;
		}
	}
	
	//获取链表中结点的个数
	public int length(){
		int ret = -1;
		
		if(head != null){
			ret = (Integer)head.data;
		}
		
		return ret;
	}
	
	//打印循环链表中的数据
	public void display(){
		if(head != null){
			Node node = head;
			
			for(int i = 0;i < (Integer)head.data;i++){
				node = node.next;
				System.out.print(node.data+"  ");
			}
		}
	}
}
3、测试类Test的代码如下所示:

public class Test {

	public static void main(String[] args) {
		CircleList list = new CircleList();
		
		list.insert("结点1", 0);
		list.insert("结点2", 1);
		list.insert("结点3", 2);
		list.insert("结点4", 3);
		list.insert("结点5", 4);
		list.insert("结点6", 5);
		
		System.out.println("链表中的元素为:");
		list.display();
		System.out.println("\n链表中结点的个数:"+list.length());
		
		System.out.println("\n获取链表中下标为2的结点:"+list.get(2));
		System.out.println("删除链表中下标为0的结点:"+list.delete(0));
		System.out.println("链表中的元素为:");
		list.display();
		System.out.println("\n链表中结点的个数:"+list.length());
		
		list.clear();
		list.destroy();
	}
}
4、程序运行结果如下所示:

链表中的元素为:
结点1  结点2  结点3  结点4  结点5  结点6  
链表中结点的个数:6

获取链表中下标为2的结点:结点3
删除链表中下标为0的结点:结点1
链表中的元素为:
结点2  结点3  结点4  结点5  结点6  
链表中结点的个数:5

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值