数据结构之循环链表
来源:互联网 发布:cydia数据库损坏 编辑:程序博客网 时间:2024/05/22 01:56
package com.dby.link;/** * Created by suzunshou on 2016/4/11. * 链表的元素不能随机访问 * 元素前面和后面不会出现多个元素相连的情况 * 元素相互依赖,串联而成 * 链表只有一个表头 * 循环链表首尾相连,尾结点指向首结点 */public class LinkedList { //结点类 private class Node { //数据 private Object data; //下一个结点的地址 private Node next = null; public Node(Object data) { this.data = data; } } private Node first = null; private Node last = null; private int size; //循环链表,头尾相连,插入结点到某个位置 public void insert(Object data, int index) { //新结点 Node newNode = new Node(data); //空链表 if (first == null) { first = newNode; first.next = first; return; } //只有一个结点 if (index - 1 == 0) { newNode.next = first.next; first.next = newNode; return; } //普通情况 Node curr = first.next; int count = 0; //遍历直到起始位置 while (curr != first && count < index - 2) { curr = curr.next; count++; } //插入结点,把前一个结点和后一个结点断开,两端接上新结点 if (count == index - 2) { newNode.next = curr.next; curr.next = newNode; } if (newNode == first.next) { first = newNode; } size++; } //输出约瑟夫环被删除的元素,报数m public void output_josephus(int m) { //首结点 Node curr = first; //遍历返回到起始点 while (curr.next != curr) { //找到m索引处的结点 for (int i = 1; i < m; i++) { curr = curr.next; } //找到被删除的结点 Node del = curr.next; //输出结点值 System.out.println(del.data); //断开被删除结点,连接被删结点的下一个结点 curr.next = curr.next.next; //删除结点 del = null; } //输出并删除最后一个结点 System.out.println(curr.data); curr = null; } public static void main(String[] args) { LinkedList ll = new LinkedList(); for (int i = 1; i < 8; i++) { ll.insert2(i, i); } ll.output_josephus(2); }}
0 0
- 数据结构之循环链表
- 数据结构之循环链表
- 数据结构之循环链表
- 数据结构之循环链表
- 数据结构之循环链表
- 数据结构之循环链表
- 数据结构之循环链表
- 数据结构学习之循环链表结构
- 数据结构之单向循环链表
- 数据结构之循环链表05
- 数据结构之双向循环链表06
- 数据结构之单向循环链表
- 数据结构 循环链表之约瑟夫问题
- 数据结构之双向循环链表
- 数据结构之双向循环链表
- 数据结构链表之循环双链表
- 数据结构基础 之 循环链表
- 算法与数据结构之循环链表
- 数据持久化基础知识笔记
- HDU-1088-Write a simple HTML Browser
- 将CART算法用于回归
- Spring xml配置文件头解析
- [转]ServletConfig与ServletContext对象详解
- 数据结构之循环链表
- Objective-C 字符串NSString
- 独木舟上的旅行
- "围观"设计模式(1)--单一职责原则(SRP,Single Responsibility Principle)
- 第六周 项目2-带武器的游戏角色
- jQuery操作DOM元素
- PSobj [3] 脱墨喷溅和炫光特效
- poj3468线段树区间更新
- 【导航条滚动透明】一个分类搞定