本文表述了线性表及其基本操作的代码【Java实现】

参考书籍 :《数据结构 ——Java语言描述》/刘小晶 ,杜选主编

线性表需要的基本功能有:动态地增长或收缩;对线性表的任何数据元素进行访问和查找;在线性表中的任何位置进行数据元素的插入和删除操作;求线性表中指定数据元素的前驱和后继等等。

首先描述线性表的抽象类型,我们使用Java接口interface:

Ilist.java:

package liner_list;

public interface IList
{
public void clear();
public boolean isEmpty();
public int length();
public Object get(int i) throws Exception;
public void insertAt(int i,Object x) throws Exception;
public void remove(int i) throws Exception;
public int indexOf(Object x);
public void display();
}

其次描述顺序表,其特点有:在线性表中的逻辑上相邻的数据元素,在物理存储位置上也是相邻的;存储密度高,但需要预先分配”足够应用“的存储空间,这可能将会造成存储空间的浪费;便于随机存储;不便于插入和删除,因为在顺序表中进行插入和删除操作会引起大量数据元素的移位。我们用SqList类描述顺序表:

SqList.java:

package liner_list;

// 规定方法中的参数i都为顺序表元素的索引(下标)
public class SqList implements IList
{
public Object[] listItem; // 顺序表存储空间
public int curLen; // 线性表的当前长度 public SqList(int maxSize)
{
listItem = new Object[maxSize]; // 为顺序表分配maxSize个存储单元
curLen = 0; // 置当前长度为0
} public void clear()
{
curLen = 0; // 置当前长度为0,即规定为清空顺序表,但是内存中还有数据存在
} public boolean isEmpty()
{
return curLen == 0;
} public int length()
{
return curLen; // 返回当前长度
} public Object get(int i) throws Exception // 得到下标为i的元素,同时判断异常
{
if (i >= curLen || i < 0) // 索引越界,0<=index<=curLen
{
throw new Exception("Argument 'i' is out of range!");
}
return listItem[i];
} public void insertAt(int i, Object x) throws Exception // 在下表为i的位置插入元素x,同时判断异常
{
if (curLen == listItem.length) // 判断表满
{
throw new Exception("SqList is full!");
}
if (i > curLen || i < 0) // 索引越界,可以在curLen的位置进行插入
{
throw new Exception("Argument 'i' is out of range!");
}
for (int j = curLen; j > i; j--) // j从curLen的位置开始,即当前表最后一个元素的后一个位置,从而使得i位置及以后位置上的元素向后移一位
{
listItem[j] = listItem[j - 1];
}
listItem[i] = x; // 将x元素插入i位置
curLen++; // 插入后表长加一
} public void remove(int i) throws Exception
{
if (i >= curLen || i < 0) // i小于0或者大于等于表长时抛出异常
{
throw new Exception("Argument 'i' is out of range!");
}
for (int j = i; j < curLen - 1; j++) // 从i位置开始向后,不能从最后开始,否则最后一个元素将覆盖所有元素,若想从后向前,必须将被覆盖的元素保留给下一个元素
{
listItem[j] = listItem[j + 1];
}
curLen--; // 删除完后curLen减一
} public int indexOf(Object x) // 规定返回-1表示未找到元素x
{
for (int i = 0; i < curLen; i++)
{
if (listItem[i].equals(x))
{
return i;
}
}
return -1;
// 书本代码,效果相同
// int j = 0;
// while (j < curLen && !listItem[j].equals(x))
// {
// j++;
// }
// if (j < curLen)
// {
// return j;
// } else
// {
// return -1;
// }
} public void display() // 输出顺序表中全部元素
{
System.out.println("****** SqList ******");
for (int i = 0; i < curLen; i++)
{
System.out.print(listItem[i] + " ");
}
System.out.println();
System.out.println("********************");
}
}

接着测试我们的顺序表,使用SqListTest类来做测试:

SqListTest.java:

package liner_list;

import java.util.Scanner;

public class SqListTest
{
public static void main(String[] args) throws Exception
{
SqList sq1 = new SqList(10);
sq1.insertAt(0, "a0");
sq1.insertAt(1, "a1");
sq1.insertAt(2, "a2");
sq1.insertAt(3, "a3");
sq1.insertAt(4, "a4");
sq1.insertAt(5, "a5");
int index = sq1.indexOf("a2");
if (index != -1)
{
System.out.println("a2's index is " + index + "!");
} else
{
System.out.println("a5 is not in this SqList!");
}
sq1.display();
sq1.remove(2);
System.out.println("After remove:");
sq1.display();
SqList sq2 = new SqList(10);
Scanner sc = new Scanner(System.in);
System.out.println("Please input element:");
for (int i = 0; i < 8; i++)
{
sq2.insertAt(i, sc.next());
}
sc.close();
sq2.display();
}
}

运行我们的测试类,得到以下测试结果:

然后描述单链表,注意:我们推荐使用带头结点的单链表。这里总结以下关于头指针和头结点的问题:首先要清楚,head就是头指针,毋庸置疑;如果有头结点的话,head也头结点,这里头指针就是头结点,一般说成头指针指向头结点,而head.next是下标为0的元素,规定 head是下标为-1的元素;如果没有头结点的话,head本是就是下标为0的元素,这里没有头结点,但是head还是头指针。下面我们来描述结点类,它由两部分组成,data数据域和next指针域:

Node.java:

package liner_list;

public class Node
{
public Object data; // 数据域
public Node next; // 指针域 public Node() // 无参构造方法
{
this(null, null);
} public Node(Object data) // 带一个参数的构造方法
{
this(data, null);
} public Node(Object data, Node next) // 带两个参数的构造方法
{
this.data = data;
this.next = next;
}
}

我们用LinkList类来描述带头结点的单链表:

LinkList.java:

package liner_list;

import java.util.Scanner;

//关于头结点与头指针的问题
//首先要清楚,head就是头指针,毋庸置疑
//如果有头结点的话,head也头结点,这里头指针就是头结点,一般说成头指针指向头结点,而head.next是下标为0的元素,规定 head是下标为-1的元素
//如果没有头结点的话,head本是就是下标为0的元素,这里没有头结点,但是head还是头指针
//建议写带头结点的单链表,此类就是一个典例
public class LinkList implements IList
{
public Node head; public LinkList() // 无参构造方法,只构建头指针
{
head = new Node();
} public LinkList(int len, boolean Order) throws Exception // 带有两个参数的构造方法,分别为表长和插入的方式,规定true表示尾插法,flase表示头插法
{
this();
if (Order)
{
createAtEnd(len);
} else
{
createAtHead(len);
}
} public void createAtHead(int n) throws Exception // 头插法
{
Scanner sc = new Scanner(System.in);
System.out.println("Please input element:");
for (int i = 0; i < n; i++)
{
insertAt(0, sc.next());
}
// sc.close(); // 不要关闭输入流
// display();
} public void createAtEnd(int n) throws Exception // 尾插法
{
Scanner sc = new Scanner(System.in);
System.out.println("Please input element:");
for (int i = 0; i < n; i++)
{
insertAt(length(), sc.next());
}
// sc.close(); // 不要关闭输入流
// display();
} @Override
public void clear() // 置空链表
{
head.data = null;
head.next = null;
} @Override
public boolean isEmpty() // 链表判空
{
return head.next == null;
} @Override
public int length() // 返回链表长度
{
Node p = head.next; // p指向首结点
int length = 0;
while (p != null)
{
p = p.next;
length++;
}
return length;
} @Override
public Object get(int i) throws Exception
{
Node p = head.next;
int j = 0;
while (p != null && j < i) // 从首结点开始向后查找,直到p指向第i个结点或者p为空
{
p = p.next; // 指针后移
j++; // 计数加1
}
if (i < 0 || p == null) // i小于0或者大于表长减1时抛出异常
{
throw new Exception("Argument 'i' is out of range!");
}
return p.data;
} @Override
public int indexOf(Object x) // 规定-1表示不在LinkList当中
{
Node p = head.next;
int index = 0;
while (p != null && !p.data.equals(x))
{
p = p.next;
index++;
}
if (p != null)
{
return index;
} else
{
return -1;
}
} @Override
public void insertAt(int i, Object x) throws Exception
{
Node p = head; // 插入时从头结点开始,因为可以插入在下标0的位置,也可以插入在下标为表长位置
int j = -1;
while (p != null && j < i - 1) // 找出下标为i-1的结点,即i结点的前驱
{
p = p.next;
j++;
}
if (i < 0 || p == null) // i小于0或者i大于表长时抛出异常
{
throw new Exception("Argument 'i' is out of range!");
}
Node s = new Node(x);
s.next = p.next;
p.next = s;
} @Override
public void remove(int i) throws Exception
{
Node p = head;
int j = -1;
while (p != null && j < i - 1) // 找到下标为i-1的结点,即i结点的前驱
{
p = p.next;
j++;
}
if (i < 0 || p.next == null) // 抛出条件为i小于0或者i大于表长-1,所以此处为p.next==null
{
throw new Exception("Argument 'i' is out of range!");
}
p.next = p.next.next;
} @Override
public void display()
{
Node p = head.next;
System.out.println("****** LinkList ******");
while (p != null)
{
System.out.print(p.data.toString() + " ");
p = p.next;
}
System.out.println();
System.out.println("*********************");
} }

最后测试我们的单链表,使用LinkListTest类来做测试:

LinkListTest.java

package liner_list;

public class LinkListTest
{
public static void main(String[] args) throws Exception
{
LinkList linkList = new LinkList(10, true);
linkList.remove(0);
linkList.remove(1);
linkList.remove(linkList.length() - 1);
System.out.println("After remove:");
linkList.display();
linkList.insertAt(linkList.length(), "a9");
System.out.println("After insert:");
linkList.display();
int index = linkList.indexOf("a2");
if (index != -1)
{
System.out.println("a2's index is " + index + "!");
} else
{
System.out.println("a2 is not in this LinkList!");
}
}
}

运行我们的测试类,得到以下结果:

以上便是线性表中顺序表和单链表最基础的代码描述,算法思想本文中没有写到,大家可以去看看前面说的那本参考书籍。此系列后面会陆续介绍更多有关数据结构的内容,也会更新一些关于数据结构的算法题目例子,谢谢大家支持!

【线性表基础】顺序表和单链表的插入、删除等基本操作【Java版】的更多相关文章

  1. 单链表的插入删除操作(c++实现)

    下列代码实现的是单链表的按序插入.链表元素的删除.链表的输出 // mylink.h 代码 #ifndef MYLINK_H #define MYLINK_H #include<iostream ...

  2. [C++]数据结构:线性表之顺序表

    1 顺序表 ADT + Status InitList(SeqList &L) 初始化顺序表 + void printList(SeqList L) 遍历顺序表 + int ListLengt ...

  3. C#线性表之顺序表

    线性表是最简单.最基本.最常用的数据结构.线性表是线性结构的抽象(Abstract), 线性结构的特点是结构中的数据元素之间存在一对一的线性关系. 这种一对一的关系指的是数据元素之间的位置关系,即: ...

  4. c/c++ 线性表之顺序表

    线性表之顺序表 存储在连续的内存空间,和数组一样. 下面的代码,最开始定义了一个能存8个元素的顺序表,当超过8个元素的时候,会再追加开辟空间(函数:reInit). 实现了以下功能: 函数 功能描述 ...

  5. 线性表之顺序表C++实现

    线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...

  6. [数据结构 - 第3章] 线性表之顺序表(C++实现)

    一.类定义 顺序表类的定义如下: #ifndef SEQLIST_H #define SEQLIST_H typedef int ElemType; /* "ElemType类型根据实际情况 ...

  7. 数据结构Java实现02----线性表与顺序表

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  8. 数据结构Java实现01----线性表与顺序表

    一.线性结构: 如果一个数据元素序列满足: (1)除第一个和最后一个数据元素外,每个数据元素只有一个前驱数据元素和一个后继数据元素: (2)第一个数据元素没有前驱数据元素: (3)最后一个数据元素没有 ...

  9. 面试之路(10)-BAT面试之java实现单链表的插入和删除

    链表的结构: 链表在空间是不连续的,包括: 数据域(用于存储数据) 指针域(用于存储下一个node的指针) 单项链表的代码实现: 节点类 构造函数 数据域的get,set方法 指针域的get,set方 ...

随机推荐

  1. 基于GitLab+Jenkins的DevOps赋能实践

    随着微服务.中台架构的兴起,DevOps也变得非常关键,毕竟是一些基础设施层面的建设,如果搞好了对后面的研发工作会有很大的效率提升.关于DevOps本身的概念,网上已经非常多了,在园子里随便搜索一些都 ...

  2. zabbix -- 学习之一

    网上说这东西是运维必须学会的东西,于是乎捣鼓的第一步就开始了. 首先,在度娘上搜索了一下,找到了官网,按照官网的说法没操作成功.后来照这博主的帖子(https://www.cnblogs.com/xi ...

  3. VUE中CSS样式穿透

    VUE中CSS样式穿透 1. 问题由来 在做两款H5的APP项目,前期采用微信官方推荐的weui组件库.后来因呈现的效果不理想,组件不丰富,最终项目完成后全部升级采用了有赞开发的vant组件库.同时将 ...

  4. Java日志框架SLF4J和log4j以及logback的联系和区别

    1.SLF4J(Simple logging Facade for Java) 意思为简单日志门面,它是把不同的日志系统的实现进行了具体的抽象化,只提供了统一的日志使用接口,使用时只需要按照其提供的接 ...

  5. text2pcap: 将hex转储文本转换为Wireshark可打开的pcap文件

    简介 Text2pcap是一个读取ASCII hex转储的程序,它将描述的数据写入pcap或pcapng文件.text2pcap可以读取包含多个数据包的hexdumps,并构建多个数据包的捕获文件.t ...

  6. PHP 异或 算法

    /** * PHP字符串“异或”算法 * param array key * @param Request $request * @return mixed|string|void */ public ...

  7. Google 官方 侧滑 drawerlayout

    一.概述 目前侧滑框架已经很多了,但是我常用的也就那么2个 ,slidingmenu 和sidemenu-android, 但是项目要求使用官方的,所以就看了一下drawerlayout 二.代码 官 ...

  8. .Net基础篇_学习笔记_第五天_流程控制while循环002

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. PHP 错误:Warning: Cannot modify header information - headers already sent by ...

    PHP初学者容易遇到的错误:Warning: Cannot modify header information - headers already sent by ...: 通常是由不正确使用 hea ...

  10. [VB.NET Tips]再谈字符串连接之内置池

    CLR自动维护一个称为"内置池"(暂存池)(intern pool)的表,在编译时此表包含程序中声明的每个唯一的字符串常量的单个实例,以及以编程方式创建的String类的任何唯一实 ...