Arrayss work well for unordered sequences, and even for ordered squences if they don't change much. But if you want to maintain an ordered list that allows quick insertions and deletions, you should use a linked data structure.
so inserting an element using array may have to move a lot of data. if n = 1000 and x is less than all of those elements, then the method will move all 1000 elements. On average, inserting into a sorted array of n elements
will move n/2 elements. So this is a F(n) operation. Deleting an element is simply the reverse of the insertion process, It too will have to move n/2 elements, on average, so deletion is also a f(n) operation.

The following is a simple example about linked list.

package com.albertshao.ds.array;

public class TestNode {
public static void main(String[] args) {
Node start = new Node(22);
Node p = start;
for (int i = 1; i < 5; i++)
p = p.next = new Node(22 + 11 * i);
for (p = start; p != null; p = p.next)
System.out.println(p.data);
for (p = start; p != null; p = p.next)
System.out.println(p);
}
} class Node {
int data;
Node next; Node(int data) {
this.data = data;
}
}

The  output is as follows:

22
33
44
55
66
com.albertshao.ds.array.Node@c17164
com.albertshao.ds.array.Node@1fb8ee3
com.albertshao.ds.array.Node@61de33
com.albertshao.ds.array.Node@14318bb
com.albertshao.ds.array.Node@ca0b6

Test the insert element into linked list.

//  Data Structures with Java
// by John R. Hubbard and Anita Huray
// Copyright 2004 by Prentice Hall package com.albertshao.ds.array; public class TestInsert {
TestInsert() {
Node start = init();
print(start);
insert(start, 50);
print(start);
} Node init() {
Node start = new Node(22), p = start;
for (int i=1; i<5; i++)
p = p.next = new Node(22+11*i);
return start;
} void print(Node start) {
for (Node p=start; p!=null; p=p.next)
System.out.print(p.data + " ");
System.out.println();
} void insert(Node start, int x) {
// PRECONDITIONS: the list is in ascending order, and x > start.data;
// POSTCONDITIONS: the list is in ascending order, and it contains x;
Node p = start;
while (p.next != null) {
if (p.next.data > x) break;
p = p.next;
}
p.next = new Node(x,p.next);
} public static void main(String[] args) {
new TestInsert();
} class Node {
private int data;
private Node next; public Node(int data) {
this.data = data;
} public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
} /* Output:
22 33 44 55 66
22 33 44 50 55 66
*/

The method of deleting element is as follows:

    Node delete(Node start, int x) {
// precondition: the list is in ascending order;
// postconditions: the list is in ascending order, and if it did
// contains x, then the first occurrence of x has been deleted;
if (start == null || start.data > x)
return start; // x is not in the list
if (start.data==x) return start.next;
for (Node p = start; p.next != null; p = p.next) {
if (p.next.data > x) break; // x is not in the list
if (p.next.data == x) {
p.next = p.next.next; // delete x
break;
}
}
return start;
}

【DataStructure】Linked Data Structures的更多相关文章

  1. 【DataGuard】部署Data Guard相关参数详解 (转载)

    原文地址:[DataGuard]部署Data Guard相关参数详解 作者:secooler    有关物理Data Guard部署参考<[DataGuard]同一台主机实现物理Data Gua ...

  2. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. 【机器学习】Iris Data Set(鸢尾花数据集)

    [机器学习]Iris Data Set(鸢尾花数据集) 注:数据是机器学习模型的原材料,当下机器学习的热潮离不开大数据的支撑.在机器学习领域,有大量的公开数据集可以使用,从几百个样本到几十万个样本的数 ...

  4. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  5. 【DataStructure】One of queue usage: Simulation System

    Statements: This blog was written by me, but most of content  is quoted from book[Data Structure wit ...

  6. 【DataStructure】The description of Java Collections Framework

    The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. ...

  7. 【DataStructure】Description and Introduction of Tree

    [Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...

  8. 【转】char data[0]用法总结

    @2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ...

  9. 【C#】Send data between applications

    This sample shows how to send data between different applications, including object data——transform ...

随机推荐

  1. xp 如何打开(进行)远程桌面连接

    http://apps.hi.baidu.com/share/detail/31102654http://help.360.cn/5030804/40072526.htmlhttp://hi.baid ...

  2. TortoiseSVN 覆盖SVN仓库最新版本提交

    情况背景: ibank.pdm文件最新版本有问题,版本号为5051. 我想在5050的版本上修改后提交,覆盖5051版本的修改,也就是经过我修改后的5050版本,覆盖5051版本的修改,提交成功并成为 ...

  3. Hadoop On Demand

    原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hod.html 简介 文档 简介 Hadoop On Demand(HOD)是一个能在大型物理集群上供应虚拟 ...

  4. hello oc

    printf("Hello C\n"); //OC可以采用C语言的输出方式 printf("The number is %d\n",100);//%d 输出数字 ...

  5. Linux中断 - ARM中断处理过程

    一.前言 本文主要以ARM体系结构下的中断处理为例,讲述整个中断处理过程中的硬件行为和软件动作.具体整个处理过程分成三个步骤来描述: 1.第二章描述了中断处理的准备过程 2.第三章描述了当发生中的时候 ...

  6. Unix环境高级编程(六)进程控制

    本章介绍Unix的进程控制,包括进程创建,执行程序和进程终止,进程的属性,exec函数系列,system函数,进程会计机制. 1.进程标识符 每一个进程都有一个非负整数标识的唯一进程ID.ID为0表示 ...

  7. Linux时间子系统(一) 基本概念

    本文使用Q & A的方式来和大家以前探讨一下时间的基本概念 一.什么是时间? 这个问题实在是太复杂了,我都不知道这是一个物理学.宇宙学.还是热力学异或是哲学问题,我只是想从几个侧面来了解一下时 ...

  8. Codeforces 6D Lizards and Basements 2 dfs+暴力

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  9. 【转】python2与python3的主要区别

    摘自:http://www.cnblogs.com/codingmylife/archive/2010/06/06/1752807.html 1.性能 Py3.0运行 pystone benchmar ...

  10. 怎么在ubuntu中新增一个用户并赋予管理员权限

    用useradd时,并没有创建同名的用户主目录.例子:adduser user1这样他就会自动创建用户主目录,创建用户同名的组.root@ubuntu:~# sudo adduser db[sudo] ...