【DataStructure】Linked Data Structures
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的更多相关文章
- 【DataGuard】部署Data Guard相关参数详解 (转载)
		原文地址:[DataGuard]部署Data Guard相关参数详解 作者:secooler 有关物理Data Guard部署参考<[DataGuard]同一台主机实现物理Data Gua ... 
- 【Leetcode】Linked List Cycle II
		Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ... 
- 【机器学习】Iris Data Set(鸢尾花数据集)
		[机器学习]Iris Data Set(鸢尾花数据集) 注:数据是机器学习模型的原材料,当下机器学习的热潮离不开大数据的支撑.在机器学习领域,有大量的公开数据集可以使用,从几百个样本到几十万个样本的数 ... 
- 【DataStructure】Description and usage of queue
		[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ... 
- 【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 ... 
- 【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. ... 
- 【DataStructure】Description and Introduction of Tree
		[Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ... 
- 【转】char data[0]用法总结
		@2019-07-31 struct MyData { int nLen; ]; }; 开始没有理解红色部分的内容,上网搜索下,发现用处很大,记录下来. 在结构中,data是一个数组名:但该数组没有元 ... 
- 【C#】Send data between applications
		This sample shows how to send data between different applications, including object data——transform ... 
随机推荐
- matplot模块中的pylab
			pylab的目的 Pylab combines the functionality of pyplot with the capabilities of NumPy in a single names ... 
- 【C++】int与string互转
			int转string(注:itoa不是标准函数,OJ平台可能不接受) ; ]; string str; sprintf(temp, "%d", n); str = temp; or ... 
- Google官方下拉刷新组件---SwipeRefreshLayout
			今天在Google+上看到了SwipeRefreshLayout这个名词,遂搜索了下,发现竟然是刚刚google更新sdk新增加的一个widget,于是赶紧抢先体验学习下. SwipeRefreshL ... 
- Android学习系列(10)--App列表之拖拽ListView(上)
			研究了很久的拖拽ListView的实现,受益良多,特此与尔共飨. 鉴于这部分内容网上的资料少而简陋,而具体的实现过程或许对大家才有帮助,为了详尽而不失真,我们一步一步分析,分成两篇文章. ... 
- 安装Python 3.6
			原文地址:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143160904 ... 
- oc 代码块的使用
			#import <UIKit/UIKit.h> #import "AppDelegate.h" int (^max)(int,int);//定义代码块,类似c的函数指针 ... 
- 【Life】 Never Too Late, Just Do it Better!
			开这个博客: 一来是认为自己记忆力不好,对所学的东西做个记录: 二来是希望找到很多其它志同道合的人.一起交流进步: 不论什么时候開始努力都不晚! 希望平淡的工作生活不要磨灭我们心中的梦想,与君共勉~ 
- 使用latex撰写博士,硕士学位论文(浙大博士经验分享)
			使用latex撰写博士,硕士学位论文(浙大博士经验分享) 浙大博士: 个人感觉,还是要用latex来写.因为之前发过几篇word排版的中文论文,在参考文献的引用.文字格式调整上,实在是难受.如果坚持 ... 
- JS动态创建Table,Tr,Td并赋值
			JS动态创建Table,Tr,Td并赋值. 成果库修改: 要求主题列表随成果类型改变而改变 网上查询资料后开工,在成果类型下拉框添加change()事件触发Dwr,查询主题集合——动态创建/编辑Tab ... 
- [sh]shell案例
			调用同目录下的ip.txt内容: 路径 [root@lanny ~]# pwd /root txt文件 [root@lanny ~]# cat ip.txt 10.1.1.1 10.1.1.2 10. ... 
