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. windows平台下压缩tar.gz

    windows平台下很多压缩软件(如360压缩)都支持tar.gz的解压,但不支持压缩 推荐一款支持tar.gz压缩的软件7zip 下面介绍可视化和命令行两种操作方式将文件压缩为tar.gz 可视化操 ...

  2. 2.Java基础:方法定义和调用

    一.方法的定义 1.类的方法代表的是实力的某种行为(或功能) 方法的定义:方法类型 . 方法签名 . 方法体 2.定义类的方法 访问修饰符 类型 方法名(参数列表){ ///方法体 } 3.把方法当作 ...

  3. Python练习笔记——对输入的数字进行加和

    请您输入数字,每个数字采用回车结束,当您输入型号*时,则结束数字输入,输出所有数字的总和 def num_sum(): i = 0 while True: get_num = input(" ...

  4. c编程:求出4&#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

    //求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max, ...

  5. 使用xshell远程登录ubuntu使用vi编辑不能使用删除键方向键

    近期安装了xshell,远程登录上ubuntu后,在插入模式下,按删除键没有任何反应,按方向键分别打印出A.B.C.D,每个字符一行. 这是因为ubuntu初始化安装的是vi的tiny版本,解决办法安 ...

  6. WPF绑定时要绑定属性,不要绑定字段

    如题(就是加get;set;),绑定属性不出东西,不知道为什么...

  7. JAX-RS(REST Web Services)2.0 can not be installed: One or more constraints have not been satisfied

    eclipse出错: JAX-RS(REST Web Services)2.0 can not be installed: One or more constraints have not been ...

  8. Qt解析xml

    发现用 Qt 解析 xml 文件非常方便,下面是一个简单的解析 xml 文件的例子: #include <QtCore/QCoreApplication> #include <QDo ...

  9. activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建

    项目中须要整合activiti-modeler自己定义流程,找了非常多资料后,最终成功的跳转到activiti-modeler流程设计界面.下面是记录: 一.整合基础:eclipse4.4.1.tom ...

  10. asp.net MVC学习的一些总结

    起初认为视图,控制器,模型它们是完全没有耦合的,真正用了一段时间MVC发现错了. 但通过抽象让他们完全没有耦合,也不是不可能. 1.奇怪的连接地址 用MVC之前,一直认为页面必然访问某个文件.用了MV ...