线性表java实现
顺序表
public class SequenceList {
/*
* content,节点内容
* location,节点在表中的位置(序号)
* */
private String content;
private int location;
SequenceList(String content, int location){
this.content = content;
this.location = location;
}
SequenceList(){
this.content = "-1";
this.location = -1;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getLocation() {
return location;
}
public void setLocation(int location) {
this.location = location;
}
}
顺序表的各个操作
遍历
/*
* 遍历顺序表
* */
public static void showList(SequenceList[] list){
for(SequenceList one:list){
if(one!=null)
System.out.println("location = "+one.getLocation()+",content = "+one.getContent());
else
break;
}
}
插入
/*
* 插入节点
* */
public static void insertItem(SequenceList[] list, SequenceList item, int currentListLength, int insertLocation){
list[currentListLength] = new SequenceList();
if(currentListLength<insertLocation){
System.out.println("插入位置不在线性表内");
}else if(currentListLength==insertLocation+1){
System.out.println("线性表已满");
}else {
for(int i = currentListLength;i>insertLocation;i--){
list[i] = list[i-1];
list[i].setLocation(i);//重新设置节点位置
}
list[insertLocation] = item;
currentListLength++;
} }
获得特定位置的节点
/*
* 获得特定位置的节点
* */
public static SequenceList getSpecificItem(SequenceList[] list, int location){
for(SequenceList one:list){
if(one.getLocation()==location){
return one;
}
}
return null;
}
获得某个节点的位置
/*
* 获得某个节点的位置
* */
public static int getItemLocation(SequenceList[] list, String content){
for(SequenceList one:list){
if(one.getContent().equals(content)){
return one.getLocation();
}
}
return -1;
}
删除某个位置的节点
/*
* 删除节点
* */
public static void deleteItem(SequenceList[] list, int location){
for(int i = location;;i++){
if(list[i+1]==null){
list[i] = null;
break;
}
list[i] = list[i+1];
list[i].setLocation(i);//重新设置节点位置
}
}
测试
public static void main(String[] args) {
SequenceList[] lists = new SequenceList[20];
for(int i = 0;i < 6;i++){
lists[i] = new SequenceList();
lists[i].setContent(String.valueOf(i));
lists[i].setLocation(i);
}
insertItem(lists,new SequenceList("a",5),6,5);
showList(lists);
deleteItem(lists,5);
showList(lists);
System.out.println("5号位的内容是"+getSpecificItem(lists,5).getContent());
System.out.println("内容为2的位置是"+getItemLocation(lists,"2"));
}
结果
location = 0,content = 0
location = 1,content = 1
location = 2,content = 2
location = 3,content = 3
location = 4,content = 4
location = 5,content = a
location = 6,content = 5
location = 0,content = 0
location = 1,content = 1
location = 2,content = 2
location = 3,content = 3
location = 4,content = 4
location = 5,content = 5
5号位的内容是5
内容为2的位置是2
链表
节点类
public class SingleLinkList {
int data;//序号
SingleLinkList next;
SingleLinkList(){}
SingleLinkList(int data){
this.data = data;
}
}
插入节点
/*
* 插入节点到相应的位置
* */
public static void insertNode(SingleLinkList list,SingleLinkList item,int location){
SingleLinkList tempList = list;
while(tempList!=null){
if(tempList.data==location){
item.next = tempList.next;
item.data = tempList.data+1;
tempList.next = item;
break;
}else {
tempList = tempList.next;
}
}
item = item.next;
while (item!=null){
//重新编号排序
item.data++;
item = item.next;
}
}
删除节点
/*
* 删除相应节点
* */
public static void deleteNode(SingleLinkList list,int data){
SingleLinkList tempList = list;
while (tempList!=null){
//System.out.println(tempList.data);
if(tempList.data == data-1){
if(tempList.next.next!=null){
tempList.next = tempList.next.next;
tempList = tempList.next;
break;
}
else
tempList.next = null;
break;
}
tempList = tempList.next;
}
while (tempList!=null){
//重新编号排序
tempList.data--;
tempList = tempList.next;
}
}
获得特定位置的节点
/*
*获得特定位置的节点
* */
public static SingleLinkList getSpecificNode(SingleLinkList list,int location){
while (list!=null){
if(list.data==location){
return list;
}else {
list = list.next;
}
}
return null;
}
遍历
/*
* 遍历单链表
* */
public static void showList(SingleLinkList list){
while (list!=null){
System.out.println(list.data);
list = list.next;
}
}
反转单链表
/*
* 单链表反转
* */
public static SingleLinkList reserveList(SingleLinkList list){
SingleLinkList prevNode = null;//前一个节点
SingleLinkList currentNode = list;//当前节点
SingleLinkList reserveHead = null;//反转后的头节点
while (currentNode!=null){
SingleLinkList nextNode = currentNode.next;
if(nextNode==null){
reserveHead = currentNode;
}
currentNode.next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
return reserveHead;
}
测试类
public static void main(String[] args) {
SingleLinkList head = new SingleLinkList(-1);
SingleLinkList list = head;
for (int i = 0;i < 5;i++){
list.next = new SingleLinkList(i);
list = list.next;
}
showList(head.next);
System.out.println("得到二号位置的节点");
System.out.println(getSpecificNode(head.next,2).data);
insertNode(head.next,new SingleLinkList(2),2);
System.out.println("在二号位置插入节点后");
showList(head.next);
deleteNode(head.next,2);
System.out.println("删除二号位置的节点");
showList(head.next);
System.out.println("反转链表后");
showList(reserveList(head.next));
}
结果

循环链表
循环链表是指收尾相接的链表
链表类
public class LoopList {
int data;
LoopList next;
public LoopList(int data) {
this.data = data;
}
public LoopList(){}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public LoopList getNext() {
return next;
}
public void setNext(LoopList next) {
this.next = next;
}
}
创建循环链表
/*
* 传入各结点的数据,返回头结点
* */
public static LoopList createLoopList(int[] data){
LoopList head = new LoopList(0);
LoopList temp = head;
for(int i=0;i<data.length;i++){
LoopList node = new LoopList(data[i]);
temp.setNext(node);
temp = temp.getNext();
}
temp.setNext(head.getNext());
return head.getNext();
}
其余操作都跟单链表类似,不详细写了
双向链表
双向链表类
import java.util.DuplicateFormatFlagsException;
public class DoubleLinkList {
int data;
DoubleLinkList prev;
DoubleLinkList next;
public DoubleLinkList(){}
public DoubleLinkList(int data){
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public DoubleLinkList getPrev() {
return prev;
}
public void setPrev(DoubleLinkList prev) {
this.prev = prev;
}
public DoubleLinkList getNext() {
return next;
}
public void setNext(DoubleLinkList next) {
this.next = next;
}
}
创建双向链表
/*
* 创建双向链表
* */
public static DoubleLinkList createList(int[] data)
{
DoubleLinkList head = new DoubleLinkList(-1);
DoubleLinkList alter = head;
for(int i = 0;i < data.length;i++){
DoubleLinkList node = new DoubleLinkList(data[i]);
alter.setNext(node);
node.setPrev(alter);
alter = alter.getNext();
}
return head.getNext();
}
向特定位置插入元素
/*
* 插入元素
* */
public static boolean insert(DoubleLinkList head,int location,DoubleLinkList node){
for(int i = 0;i < location-1;i++){
head = head.getNext();
}
DoubleLinkList locationNode = head.getNext();
head.setNext(node);
node.setPrev(head);
node.setNext(locationNode);
locationNode.setPrev(node);
return true;
}
删除特定位置的元素
/*
* 删除元素
* */
public static boolean delete(DoubleLinkList head,int location){
for(int i = 0;i < location-1;i++){
head = head.getNext();
}
DoubleLinkList locationNode = head.getNext().getNext();
head.setNext(locationNode);
locationNode.setPrev(head);
return true;
}
好了 线性表这一章节的基本实现完了,下一篇轮到栈和队列了
线性表java实现的更多相关文章
- 数据结构(Java描述)之线性表
基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...
- Java Se :线性表
Java的集合框架分为两个系列,Collection和Map系列.在大学期间,学习数据结构时,好像学习了线性表.非线性表.树,哎,都给忘了.其实,在Collection系列内部又可以分为线性表.集合两 ...
- Java数据结构之线性表(2)
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- Java数据结构之线性表
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- 线性表 及Java实现 顺序表、链表、栈、队列
数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...
- Java线性表的排序
Java线性表的排序 ——@梁WP 前言:刚才在弄JDBC的时候,忽然觉得order-by用太多了没新鲜感,我的第六感告诉我java有对线性表排序的封装,然后在eclipse里随便按了一下“.” ,哈 ...
- 线性表的顺序存储结构——java
线性表的顺序存储结构:是指用一组地址连续的存储单元一次存放线性表的元素.为了使用顺序结构实现线性表,程序通常会采用数组来保存线性中的元素,是一种随机存储的数据结构,适合随机访问.java中ArrayL ...
- 线性表的Java实现
一.概念 对于常用的数据结构,可分为线性结构和非线性结构,线性结构主要是线性表,非线性结构主要是数和图.当n>0时,表可表示为:(a0,a1,a2,a3,…an) 1. 线性表的特征: 1.存在 ...
随机推荐
- Apache和nginx 域名配置
apache配置 一.hosts配置: 1.用编辑器打开hosts文件,位置:C:\Windows\System32\drivers\etc目录下 2.在hosts文件里添加自己的域名配置,配置规则如 ...
- 微信小程序设置全局字体
微信小程序设置全局css,需要在app.wxss文件中设置page的样式 page { font-family:"PingFangSC-Thin"; font-size:32rpx ...
- matlab 向量场线积分
syms t x y z F x=cos(t); y=sin(t); z=*sin(t)^-; F=[x^*y , (/)*x^,x*y ] ; %场函数 V=[diff(x,t),diff(y,t) ...
- boost-使用property_tree来解析xml、json
property_tree是一个保存了多个属性值的树形数据结构,可以用来解析xml.json.ini.info文件.要使用property_tree和xml解析组件的话需要包含"boost/ ...
- MFC连接MySQL数据库方法
MFC连接数据库的三种方法: 1.ODBC(开放式数据互联) 2.OLEDB与ADO 3.用mysql为C语言提供的接口
- VMware14 安装CentOS7及其配置;CentOS7配置网桥,做远程连接;
1.VMware14安装 进入百度链接,按照图形安装就好了.https://jingyan.baidu.com/article/9f7e7ec09da5906f281554d6.html ...
- IntelliJ IDEA使用心得之Maven项目篇
今天和大家分享下,在IDEA中打开Maven项目的方法. 对于新版的IDEA可以直接打开Maven项目,但是对于旧版的IDEA需要使用Maven命令生成项目的IDEA配置文件. 在项目的根目录(即po ...
- UVa 1610 Party Games(思维)
题意: 给出一系列字符串,构造出一个最短字符串(可以不在集合中)大于等于其中的一半,小于另一半. 析:首先找出中间的两个字符串,然后暴力找出最短的字符串,满足题意. 代码如下: #include &l ...
- php7注意事项
1. 不要使用 mysql_ 函数 这一天终于来了,从此你不仅仅“不应该”使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mysqli_函数,或者更灵活的 ...
- 学以致用三----centos7.2基本环境补充
补充: 在上一篇里,加时间戳 echo ‘export HISTTIMEFORMAT ="%F %T `whoami`" ’ >> /etc/profile sourc ...