Java数据结构——有序链表
2016-03-30 22:09
597 查看
//================================================= // File Name : SortedList_demo //------------------------------------------------------------------------------ // Author : Common //类名:SortedList //属性: //方法: class SortedList{ private Link_long first; public SortedList(){ //构造函数 first = null; } public void insert(long key){ Link_long newLink = new Link_long(key); Link_long previous = null; //上一次插入的值 Link_long current = first; //每插入一次,current就重新赋为表头的值 while(current != null && key > current.dData){ //没进入这里,pre就是null,也就只进入下面if的上一层 previous = current; current = current.next; //current的位置往后移动 } if(previous == null){ //最开始的情况,给first赋值为newLink,即key first = newLink; }else{ // previous.next = newLink; } newLink.next = current; } public long remove(){ Link_long temp = first; first = first.next; return temp.dData; } public void displayList(){ System.out.println("List (first-->last)"); Link_long current = first; while(current != null){ current.displayLink(); current = current.next; } } } //主类 //Function : SortedList_demo public class SortedList_demo { public static void main(String[] args) { // TODO 自动生成的方法存根 SortedList theSortedList = new SortedList(); theSortedList.insert(10); theSortedList.insert(20); theSortedList.insert(30); theSortedList.displayList(); theSortedList.remove(); theSortedList.remove(); theSortedList.displayList(); } }
相关文章推荐
- 一元多项式求积
- 剑指offer 数据结构与算法 二维数组查找 java 实现
- 数据结构一顺序表
- 数据结构课程上机实验题(二)
- Java数据结构——用双端链表实现队列
- Java数据结构——用链表实现栈
- 数据结构 常用定义
- 数据结构与算法分析
- 数据结构单链表删除相同元素(int)
- 数据结构和算法 – 1.基础排序算法
- 【Redis源码剖析】 - Redis内置数据结构之字典dict
- [数据结构]N-Queens Problem(recursion)
- [数据结构]Stack_Calculator(中序波兰式转后序波兰式)
- [数据结构]双链表模拟网站前进后退
- [数据结构]Polynomial
- [数据结构]Joseph problem(模拟)
- [数据结构]Linked_stack
- [数据结构]Linked_queue
- 《数据结构》 队列(Queue)操作代码集合
- 数据结构 - 简单的单链表结构