java链表
2016-05-18 16:43
417 查看
节点数据类的存储信息
class Node{ Node next = null; int data; public Node(int data){this.data = data;} }
对链表的具体操作(插入数据、删除数据、排序)
public class MylinkedList{ Node head = null; //向链表中插入数据 public void addNode(int d){ Node newNode = new Node(d); if(head == null){ head = newNode; return; } Node tmp = head; while(tmp.next != null){ tmp = tmp.next; } //add node to end tmp.next = newNode; } //删除第index个节点 public Boolean deleteNode(int index){ if(index<1 || index>length()){ return false; } //删除链表第一个元素 if(index == 1){ head = head.next; return true; } int i = 1; Node preNode = head; Node curNode = preNode.next; while(curNode != null){ if(i == index){ preNode.next = curNode.next; return true; } preNode = curNode; curNode = curNode.next; i++; } return true; } //返回节点长度 public int length(){ int length = 0; Node tmp = head; while (tmp != null) { length++; tmp = tmp.next; } return length; } //对链表进行排序 public Node orderList(){ Node nextNode = null; int temp = 0; Node curNode = head; while(curNode.next != null){ nextNode = curNode.next; while(nextNode != null){ if(curNode.data > nextNode.data){ temp = curNode.data; curNode.data = nextNode.data; nextNode.data = temp; } nextNode = nextNode.next; } curNode = curNode.next; } return head; } }
相关文章推荐
- java如何把String字符串中所有的空格和制表符(\t)替换成单个符号。
- Java注解
- 常用排序算法的java实现
- Java容器类浅析一-----容器类的类型
- Spring <context:annotation-config/> <context:component-scan/>与<mvc:annotation-driven />
- java &与&&的区别
- IDEA与Eclipse默认快捷键对比
- 全排序
- java对象参数测试源码
- JAVA_OPTS参数过小
- MyEclipse安装Freemarker插件
- JAVA—对象流
- java检测对象中是否存在某个属性
- Java语言中IO流的操作规律学习笔记
- Java_动态加载类(英文)
- shiro+springMVC文档
- Struts2中 Result类型配置详解
- eclipse 清除svn 帐号密码
- Spring知识点提炼
- 好文分享_java堆栈的区别