线性表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.存在 ...
随机推荐
- 2019.01.02 洛谷P4512 【模板】多项式除法
传送门 解析 代码: #include<bits/stdc++.h> #define ri register int using namespace std; typedef long l ...
- 2018.12.31 bzoj3992: [SDOI2015]序列统计(生成函数+ntt+快速幂)
传送门 生成函数简单题. 题意:给出一个集合A={a1,a2,...as}A=\{a_1,a_2,...a_s\}A={a1,a2,...as},所有数都在[0,m−1][0,m-1][0,m− ...
- SELECT INTO创建临时表
SELECT INTO创建临时表 SQL Server临时表有两种类型:本地和全局.它们在名称.可见性以及可用性上有区别.本地临时表的名称以单个数字符号 (#) 打头:它们仅对当前的用户连接是可见的: ...
- 解决maltab的中文和英文字体问题,中文乱码
用比较好看的编程字体,偏偏不显示中文,用支持中文的字体,英文不是等宽的,非常难看. 最近在网上找这方面的解决方法,发现解决问题的方法还是有的. 其实这个问题的原因就是系统自带的等宽字体,不支持中文,解 ...
- 三个UID
1.三个UID 这三个UID分别是实际用户ID(real uid).有效用户ID(effective uid).保存的设置用户ID(saved set-user-ID)(SUID) 实际用户ID(RU ...
- mysql学习之路_视图
视图 视图:view是一种有结构的但是没有结构来源的虚拟表,虚拟表的结构来源不是自己定义的而是从对应的基表中产生(来源) 创建视图 基本语法: Create view 视图名字 as select 语 ...
- POJ 2433 Landscaping (贪心)
题意:给定一个序列表示一群山,要你保留最多 K 个山峰,最少要削去多少体积和土.一个山峰是指一段连续的相等的区间,并且左边和右边只能比这个区间低,或者是边界. 析:贪心,每次都寻找体积最小的山峰,然后 ...
- JS中的计时器事件
JS可以实现很多java代码不易完成的功能.这里学习一些js中的计时器事件. JavaScript 一个设定的时间间隔之后来执行代码,称之为计时事件. 主要通过两个方法来实现: 1.setInterv ...
- bzoj5109(图论好题)
我的参考题解:https://www.cnblogs.com/ccz181078/p/7907022.html: 不过我感觉题解的压位有问题,(1<<x)还不炸上天.不过这题数据水,好像怎 ...
- gcc和vs在c的一些区别
1.switch中每个标签后面的命令在gcc中需要{}括起来以指明作用域. 2._itoa是非标准的c和c++扩展函数,在linux下可以使用sprintf(string, "%d &q ...