Java实现线性表-顺序表示和链式表示
顺序表示和链式表示的比较:
1.读写方式:顺序表可以顺序存取,也可以随机存取;链表只能从表头顺序存取元素;
2.逻辑结构与物理结构:顺序存储时,逻辑上相邻的元素其对应的物理存储位置也相邻;链式存储时,逻辑上相邻的元素,其物理存储位置则不一定相邻;
3.查找、插入和删除操作:
按值查找,当线性表在无序的情况下,两者的时间复杂度均为o(n);而当顺序表有序时,可采用折半查找,此时时间复杂度为o(log n);
按位查找,顺序表支持随机访问,时间复杂度为o(1);而链表的平均时间复杂度为o(n)。
顺序表的插入和删除操作平均需要移动半个表长的元素;链表的插入、删除操作时,只需修改相关节点的指针即可。
4.空间分配:顺序存储一般是基于静态存储分配,一单存储空间装满就不能扩充;链式存储的节点空间只有在需要的时候申请分配,只要内存有足够的空间即可分配。
顺序表示的实现:
public class ArrayList<E> {
final int defaultSize=0;
int maxSize; //线性表的最大长度
int length; //线性表当前长度
Object[] arrayList; //存储线性表的数组
/*
* 构造函数
*/
public ArrayList(int size){
initList(size);
}
//按给定size初始化顺序表
public void initList(int size) {
if(size < 0){
throw new RuntimeException("数组大小错误:" + size);
}else{
this.maxSize= size;
this.length=0;
this.arrayList = new Object[size];
}
}
//表长
public int length() {
return length;
}
//按值查找
public int locateElem(Object e) {
for(int i=0 ;i<length;i++){
if(arrayList[i] == e){
return i;
}
}
return -1;
}
//按位查找
public Object getElem(int i) {
if(i<0 || i>=length ){
throw new RuntimeException("参数出错:"+i);
}
if(length ==0){
throw new RuntimeException("顺序表为空");
}
return arrayList[i];
}
//插入
public void insert(int i, Object e) {
if(i<0 || i>length+1 ){
throw new RuntimeException("新元素插入位置有误:"+i);
}
if(i >= maxSize){
throw new RuntimeException("顺序表已满,不能再插入新的元素");
}
for(int j=length;j<i; j--){
arrayList[j]=arrayList[j-1];
}
arrayList[i]=e;
length++;
}
//删除
public void delete(int i, Object e) {
if(i<0 || i > length-1){
throw new RuntimeException("需删除元素位置有误:"+i);
}
if(length == 0){
throw new RuntimeException("顺序表为空,不能删除元素");
}
for(int j=i;j<length-1;j++){
arrayList[j] = arrayList[j+1];
}
arrayList[length-1]="";
length--;
}
//判空
public boolean isEmpty() {
return length==0 ? true : false;
}
//删除顺序表
public void destroyList() {
this.arrayList=null;
this.length=0;
this.maxSize=0;
}
}
单链表表示的实现:
class Node<E>{
E e; //数据
Node<E> next; //下一个节点
Node(){}
Node(E e){
this.e = e;
this.next = null;
}
}
public class LinkedList<E> {
private Node<E> header = null; //头结点
int size=0; //链表大小
public LinkedList() {
this.header = new Node<E>();
}
//得到链表的长度
public int length() {
return size;
}
//按值查找节点,返回该节点的位置
public int locateElem(E e) {
Node<E> temp = header;
int count = 1;
while(temp.next != null && temp.e != e){
temp = temp.next;
count ++;
}
return count;
}
//找到第index个位置的节点
public Node<E> getNode(int index) {
if(index > size || index < 0){
throw new RuntimeException("索引值有错:" + index);
}
Node<E> temp = new Node<E>();
temp = header;
int count=1;
while(count != index){
temp = temp.next;
count ++;
}
return temp;
}
//尾添加
public boolean addToLast(E e) {
if(size == 0){
header.e = e;
}else{
Node<E> newnode = new Node<E>(e); //根据需要添加的内容封装为节点
Node<E> last = getNode(size); //得到最后一个节点
last.next = newnode;
}
size ++;
return true;
}
//头添加
public boolean addTofirst(E e) {
if(size == 0){
header.e = e;
}else{
Node<E> newnode = new Node<E>(e); //根据需要添加的内容封装为节点
newnode.next = header.next;
header.next = newnode;
}
size ++;
return true;
}
//插入到第index个的位置
public boolean insert(int index, E e) {
Node<E> newnode = new Node<E>(e); //根据需要添加的内容封装为节点
Node<E> cnode = getNode(index-1); //得到第index-1个节点
newnode.next = cnode.next;
cnode.next = newnode;
size++;
return true;
}
//删除第index个节点
public boolean delete(int index) {
Node<E> prinode = getNode(index-1); //得到被删除的节点的前一个节点
Node<E> delnode = prinode.next; //得到被删除的节点
prinode.next = delnode.next;
size --;
return true;
}
//判空
public boolean isEmpty() {
return size==0 ? true : false;
}
public void destroyList() {
header = null;
size = 0;
}
//输出
public String toString(){
StringBuilder s = new StringBuilder("[");
Node<E> temp = header;
for(int i=0; i < size;i++){
s.append(temp.e.toString()+" ");
temp = temp.next;
}
s.append("]");
return s.toString();
}
}
双链表表示的实现
class TNode<E>{
E e;
TNode<E> prior, next;
TNode(){}
TNode(E e){
this.e = e;
prior = null;
next = null;
}
}
public class DoubleLinkedList<E> {
private TNode<E> header = null; //头结点
int size=0; //链表大小
public DoubleLinkedList(){
this.header = new TNode<E>();
}
//尾添加
public boolean addToLast(E e) {
if(size == 0){
header.e = e;
}else{
TNode<E> TNode = new TNode<E>(e); //根据需要添加的内容封装为节点
TNode<E> last = getNode(size); //得到最后一个节点
last.next = TNode;
TNode.prior=last;
}
size ++;
return true;
}
//找到第index个位置的节点
public TNode<E> getNode(int index){
if(index > size || index < 0){
throw new RuntimeException("索引值有错:" + index);
}
TNode<E> temp = new TNode<E>();
temp = header;
int count =1;
while(count != index){
temp = temp.next;
count ++;
}
return temp;
}
//插入到第index个的位置
public boolean insert(int index,E e){
TNode<E> TNode = new TNode<E>(e);
TNode<E> cnode = getNode(index-1); //找到第index-1个位置的节点
TNode.next=cnode.next;
TNode.prior = cnode;
cnode.next.prior = TNode;
cnode.next = TNode;
size++;
return true;
}
//删除第index个节点
public boolean delete(int index){
TNode<E> delnode = getNode(index);
delnode.prior.next=delnode.next;
delnode.next.prior= delnode.prior;
size--;
return true;
}
}
Java实现线性表-顺序表示和链式表示的更多相关文章
- 数据结构Java实现05----栈:顺序栈和链式堆栈
一.堆栈的基本概念: 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...
- 数据结构Java实现03----栈:顺序栈和链式堆栈
一.堆栈的基本概念: 堆栈(也简称作栈)是一种特殊的线性表,堆栈的数据元素以及数据元素间的逻辑关系和线性表完全相同,其差别是线性表允许在任意位置进行插入和删除操作,而堆栈只允许在固定一端进行插入和删除 ...
- 算法与数据结构(一) 线性表的顺序存储与链式存储(Swift版)
温故而知新,在接下来的几篇博客中,将会系统的对数据结构的相关内容进行回顾并总结.数据结构乃编程的基础呢,还是要不时拿出来翻一翻回顾一下.当然数据结构相关博客中我们以Swift语言来实现.因为Swift ...
- 数据结构导论 四 线性表的顺序存储VS链式存储
前几章已经介绍到了顺序存储.链式存储 顺序存储:初始化.插入.删除.定位 链式存储:初始化.插入.删除.定位 顺序存储:初始化 strudt student{ int ID://ID char nam ...
- 线性表的顺序存储和链式存储的实现(C)
//线性表的顺序存储 #include <stdio.h>typedef int DataType;#define MaxSize 15//定义顺序表typedef struct { Da ...
- 线性表的顺序存储和链式存储c语言实现
一.线性表的顺序存储 typedef int ElemType;typedef struct List { ElemType *data;//动态分配 ,需要申请空间 int length; }Lis ...
- c数据结构 -- 线性表之 复杂的链式存储结构
复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...
- 数据结构----线性表顺序和链式结构的使用(c)
PS:在学习数据结构之前,我相信很多博友也都学习过一些语言,比如说java,c语言,c++,web等,我们之前用的一些方法大都是封装好的,就java而言,里面使用了大量的封装好的方法,一些算法也大都写 ...
- 线性表——顺序表的实现与讲解(C++描述)
线性表 引言 新生安排体检,为了 便管理与统一数据,学校特地规定了排队的方式,即按照学号排队,谁在前谁在后,这都是规定好的,所以谁在谁不在,都是非常方便统计的,同学们就像被一条线(学号)联系起来了,这 ...
随机推荐
- P4 Runtime和p4 info
p4runtime P4 Runtime是一套基于Protobuf以及gRPC框架上的协议,通过P4runtime,SDN控制器可以控制能够支援p4的设备. p4runtime当前由p4 API wo ...
- lintcode-248-统计比给定整数小的数的个数
248-统计比给定整数小的数的个数 给定一个整数数组 (下标由 0 到 n-1,其中 n 表示数组的规模,数值范围由 0 到 10000),以及一个 查询列表.对于每一个查询,将会给你一个整数,请你返 ...
- (windows下的)Apache无法启动解决 the requested operation has failed
以下文章是转载别人的,这里只做学习用 ============================================================================== ...
- 【Linux学习笔记】Linux C中内联汇编的语法格式及使用方法(Inline Assembly in Linux C)
http://blog.csdn.net/slvher/article/details/8864996 https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm. ...
- 【转】Oracle 查询库中所有表名、字段名、表名说明、字段名说明
转自 :http://gis-conquer.blog.sohu.com/170243422.html 查询所有表名:select t.table_name from user_tables t; 查 ...
- Linux正确的关机方式
本人还未入门,仅看书所得. Linux不建议的是直接关电源.Linux后台可能有多人在工作,直接关电源可能造成文件的毁坏. 正常关机之前应该干两件事:一.查看一下谁在线:二.通知一下别人啦,通知别人可 ...
- Hadoop1.2.1异常No route to host
Hadoop1.2.1异常Bad connect ack with firstBadLink (No route to host ) 0.说明 Hadoop集群之前运行正常,增加了新节点之后,需要执行 ...
- c++常量详解
概念 常量是存放固定且不可变值的,一旦确定初始值则在程序其它地方不可改变, 所以const对象必须初始化.常量一般使用const关键字来修饰. const 对象可以大致分为三类: 1. const i ...
- 使用Java解析XML文件或XML字符串的例子
转: 使用Java解析XML文件或XML字符串的例子 2017年09月16日 11:36:18 inter_peng 阅读数:4561 标签: JavaXML-Parserdom4j 更多 个人分类: ...
- (转)iOS开发——来改掉那些被禁用的方法吧(持续更新中)
iOS平台在快速的发展,各种接口正在不断的更新.随着iOS9的发布,又有一批老方法不推荐使用了,你若调用这些方法,运行的结果是没有问题的,但是会出现警告“***is deprecated :first ...