Java单链表、双端链表、有序链表实现
单链表:
insertFirst:在表头插入一个新的链接点,时间复杂度为O(1)
deleteFirst:删除表头的链接点,时间复杂度为O(1)
有了这两个方法,就可以用单链表来实现一个栈了,见http://blog.csdn.net/a19881029/article/details/22579759
find:查找包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)
remove:删除包含指定关键字的链接点,由于需要遍历查找,平均需要查找N/2次,即O(N)
- public class LinkedList {
- private class Data{
- private Object obj;
- private Data next = null;
- Data(Object obj){
- this.obj = obj;
- }
- }
- private Data first = null;
- public void insertFirst(Object obj){
- Data data = new Data(obj);
- data.next = first;
- first = data;
- }
- public Object deleteFirst() throws Exception{
- if(first == null)
- throw new Exception("empty!");
- Data temp = first;
- first = first.next;
- return temp.obj;
- }
- public Object find(Object obj) throws Exception{
- if(first == null)
- throw new Exception("LinkedList is empty!");
- Data cur = first;
- while(cur != null){
- if(cur.obj.equals(obj)){
- return cur.obj;
- }
- cur = cur.next;
- }
- return null;
- }
- public void remove(Object obj) throws Exception{
- if(first == null)
- throw new Exception("LinkedList is empty!");
- if(first.obj.equals(obj)){
- first = first.next;
- }else{
- Data pre = first;
- Data cur = first.next;
- while(cur != null){
- if(cur.obj.equals(obj)){
- pre.next = cur.next;
- }
- pre = cur;
- cur = cur.next;
- }
- }
- }
- public boolean isEmpty(){
- return (first == null);
- }
- public void display(){
- if(first == null)
- System.out.println("empty");
- Data cur = first;
- while(cur != null){
- System.out.print(cur.obj.toString() + " -> ");
- cur = cur.next;
- }
- System.out.print("\n");
- }
- public static void main(String[] args) throws Exception {
- LinkedList ll = new LinkedList();
- ll.insertFirst(4);
- ll.insertFirst(3);
- ll.insertFirst(2);
- ll.insertFirst(1);
- ll.display();
- ll.deleteFirst();
- ll.display();
- ll.remove(3);
- ll.display();
- System.out.println(ll.find(1));
- System.out.println(ll.find(4));
- }
- }
- 1 -> 2 -> 3 -> 4 ->
- 2 -> 3 -> 4 ->
- 2 -> 4 ->
- null
- 4
双端链表(不是双向链表):
与单向链表的不同之处在保存有对最后一个链接点的引用(last)
insertFirst:在表头插入一个新的链接点,时间复杂度O(1)
insertLast:在表尾插入一个新的链接点,时间复杂度O(1)
deleteFirst:删除表头的链接点,时间复杂度O(1)
deleteLast::删除表尾的链接点,由于只保存了表尾的链接点,而没有保存表尾的前一个链接点(这里就体现出双向链表的优势了),所以在删除表尾链接点时需要遍历以找到表尾链接点的前一个链接点,需查找N-1次,也就是O(N)
有了这几个方法就可以用双端链表来实现一个队列了,http://blog.csdn.net/a19881029/article/details/22654121
- public class FirstLastList {
- private class Data{
- private Object obj;
- private Data next = null;
- Data(Object obj){
- this.obj = obj;
- }
- }
- private Data first = null;
- private Data last = null;
- public void insertFirst(Object obj){
- Data data = new Data(obj);
- if(first == null)
- last = data;
- data.next = first;
- first = data;
- }
- public void insertLast(Object obj){
- Data data = new Data(obj);
- if(first == null){
- first = data;
- }else{
- last.next = data;
- }
- last = data;
- }
- public Object deleteFirst() throws Exception{
- if(first == null)
- throw new Exception("empty");
- Data temp = first;
- if(first.next == null)
- last = null;
- first = first.next;
- return temp.obj;
- }
- public void deleteLast() throws Exception{
- if(first == null)
- throw new Exception("empty");
- if(first.next == null){
- first = null;
- last = null;
- }else{
- Data temp = first;
- while(temp.next != null){
- if(temp.next == last){
- last = temp;
- last.next = null;
- break;
- }
- temp = temp.next;
- }
- }
- }
- public void display(){
- if(first == null)
- System.out.println("empty");
- Data cur = first;
- while(cur != null){
- System.out.print(cur.obj.toString() + " -> ");
- cur = cur.next;
- }
- System.out.print("\n");
- }
- public static void main(String[] args) throws Exception {
- FirstLastList fll = new FirstLastList();
- fll.insertFirst(2);
- fll.insertFirst(1);
- fll.display();
- fll.insertLast(3);
- fll.display();
- fll.deleteFirst();
- fll.display();
- fll.deleteLast();
- fll.display();
- }
- }
- 1 -> 2 ->
- 1 -> 2 -> 3 ->
- 2 -> 3 ->
- 2 ->
有序链表:链表中的数据按从小到大排列
- public class SortedList {
- private class Data{
- private Object obj;
- private Data next = null;
- Data(Object obj){
- this.obj = obj;
- }
- }
- private Data first = null;
- public void insert(Object obj){
- Data data = new Data(obj);
- Data pre = null;
- Data cur = first;
- while(cur != null && (Integer.valueOf(data.obj.toString())
- .intValue() > Integer.valueOf(cur.obj.toString())
- .intValue())){
- pre = cur;
- cur = cur.next;
- }
- if(pre == null)
- first = data;
- else
- pre.next = data;
- data.next = cur;
- }
- public Object deleteFirst() throws Exception{
- if(first == null)
- throw new Exception("empty!");
- Data temp = first;
- first = first.next;
- return temp.obj;
- }
- public void display(){
- if(first == null)
- System.out.println("empty");
- System.out.print("first -> last : ");
- Data cur = first;
- while(cur != null){
- System.out.print(cur.obj.toString() + " -> ");
- cur = cur.next;
- }
- System.out.print("\n");
- }
- public static void main(String[] args) throws Exception{
- SortedList sl = new SortedList();
- sl.insert(80);
- sl.insert(2);
- sl.insert(100);
- sl.display();
- System.out.println(sl.deleteFirst());
- sl.insert(33);
- sl.display();
- sl.insert(99);
- sl.display();
- }
- }
- first -> last : 2 -> 80 -> 100 ->
- 2
- first -> last : 33 -> 80 -> 100 ->
- first -> last : 33 -> 80 -> 99 -> 100 ->
表的插入和删除平均需要比较N/2次,即O(N),但是获取最小数据项只需O(1),因为其始终处于表头,对频繁操作最小数据项的应用,可以考虑使用有序链表实现,如:优先级队列
和数组相比,链表的优势在于长度不受限制,并且在进行插入和删除操作时,不需要移动数据项,故尽管某些操作的时间复杂度与数组想同,实际效率上还是比数组要高很多
劣势在于随机访问,无法像数组那样直接通过下标找到特定的数据项
Java单链表、双端链表、有序链表实现的更多相关文章
- Java数据结构——用双端链表实现队列
//================================================= // File Name : LinkQueue_demo //---------------- ...
- java数据结构-09双端队列
一.相关概念: (Deque)双端队列能够在队头.队尾进行添加.删除等操作 二.接口设计: 三.代码实现 public class Deque<E> { private List< ...
- C++单链表反转、两有序链表合并仍有序
1 #include<iostream> 2 3 struct Node 4 { 5 int data; 6 Node *next; 7 }; 8 9 typedef struct Nod ...
- java数据结构——单链表、双端链表、双向链表(Linked List)
1.继续学习单链表,终于摆脱数组的魔爪了,单链表分为数据域(前突)和引用域(指针域)(后继),还有一个头结点(就好比一辆火车,我们只关心火车头,不关心其它车厢,只需知晓车头顺藤摸瓜即可),头结点没有前 ...
- JAVA基础——链表结构之双端链表
双端链表:双端链表与传统链表非常相似.只是新增了一个属性-即对最后一个链结点的引用 如上图所示:由于有着对最后一个链结点的直接引用.所以双端链表比传统链表在某些方面要方便.比如在尾部插入一个链结点.双 ...
- Java实现 LeetCode 21 合并两个有序链表
21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1 ...
- c# 有序链表合并 链表反转
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 算法练习之合并两个有序链表, 删除排序数组中的重复项,移除元素,实现strStr(),搜索插入位置,无重复字符的最长子串
最近在学习java,但是对于数据操作那部分还是不熟悉 因此决定找几个简单的算法写,用php和java分别实现 1.合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两 ...
- 代码题(14)— 合并有序链表、数组、合并K个排序链表
1.21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出 ...
- leecode刷题(23)-- 合并两个有序链表
leecode刷题(23)-- 合并两个有序链表 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2-> ...
随机推荐
- 阿里面试回来,想和Java程序员谈一谈(转载)
引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...
- hdu1231 最大连续子序列
#include<stdio.h> #include<string.h> #define maxn 10010 int a[maxn],dp[maxn]; int main() ...
- 【HDU 1009】FatMouse' Trade
题 Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the ware ...
- Oracle查看锁表
查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, a ...
- Oracle自定义函数1
用户定义函数是存储在数据库中的代码块,可以把值返回到调用程序.调用时如同系统函数一样,如max(value)函数,其中,value被称为参数.函数参数有3种类型. IN 参数类型:表示输入给函数的参数 ...
- .net String.Format数字格式化输出
内容转载自:http://www.cnblogs.com/lqb/archive/2008/08/04/1259498.html 前面内容这个做的总结的很全,今后有新增的我继续往后补充.请留意我增加的 ...
- codeforces 359D 二分答案+RMQ
上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...
- Tarjan算法详解理解集合
[功能] Tarjan算法的用途之一是,求一个有向图G=(V,E)里极大强连通分量.强连通分量是指有向图G里顶点间能互相到达的子图.而如果一个强连通分量已经没有被其它强通分量完全包含的话,那么这个强连 ...
- 面试题52:缺少i的乘积数组
vector<int> multiply(const vector<int>& A) { int len = A.size(); vector<); result ...
- ssh中getHibernateTemplate()的方法
spring接着又把业务类中的查询也封装成了find() //用来实现分页 /*HibernateTemplate ht=this.getHibernateTemplate(); DetachedCr ...