顺序表

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实现的更多相关文章

  1. 数据结构(Java描述)之线性表

    基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...

  2. Java Se :线性表

    Java的集合框架分为两个系列,Collection和Map系列.在大学期间,学习数据结构时,好像学习了线性表.非线性表.树,哎,都给忘了.其实,在Collection系列内部又可以分为线性表.集合两 ...

  3. Java数据结构之线性表(2)

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  4. Java数据结构之线性表

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  5. 线性表 及Java实现 顺序表、链表、栈、队列

    数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...

  6. JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)

    Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...

  7. Java线性表的排序

    Java线性表的排序 ——@梁WP 前言:刚才在弄JDBC的时候,忽然觉得order-by用太多了没新鲜感,我的第六感告诉我java有对线性表排序的封装,然后在eclipse里随便按了一下“.” ,哈 ...

  8. 线性表的顺序存储结构——java

    线性表的顺序存储结构:是指用一组地址连续的存储单元一次存放线性表的元素.为了使用顺序结构实现线性表,程序通常会采用数组来保存线性中的元素,是一种随机存储的数据结构,适合随机访问.java中ArrayL ...

  9. 线性表的Java实现

    一.概念 对于常用的数据结构,可分为线性结构和非线性结构,线性结构主要是线性表,非线性结构主要是数和图.当n>0时,表可表示为:(a0,a1,a2,a3,…an) 1. 线性表的特征: 1.存在 ...

随机推荐

  1. PHP中=>是什么意思

    一般用在php数组键名与元素的连接符如:$arr = array('a'=>'123','b'=>'456'); foreach($arr as $key=>$val){//$key ...

  2. Idea中如何将web项目打包成war包并放到tomcat中启动

    第一步:在idea中选中Artifacts.右侧勾选Build on make生成war包,如下图 第二步:将target文件夹里面的war包拷贝到tomcat文件下的webapp目录下 第三步:修改 ...

  3. 访问前台页面${pageContext.request.contextPath}/el表达式失效问题解决

    访问前台页面${pageContext.request.contextPath}/el表达式失效问题解决 2017年05月09日 10:54:18 AinUser 阅读数:922 标签: el表达式4 ...

  4. 链家web前端面试

    共有三轮面试,每个面试官的第一个问题都是:介绍一个你觉着比较出彩的项目 第一轮面试: 因为公司项目没什么亮点,很传统的pc端,美女面试官就说让讲一下我用react的私人项目; 问了很多都是关于reac ...

  5. 自定义消息的操作方法ON_MESSAGE(..)

  6. Update Node.js Package.json

    Update the latest package while using node.js, follow the command as following. npm i -g npm-check-u ...

  7. (求凹包) Bicycle Race (CF 659D) 简单题

    http://codeforces.com/contest/659/problem/D     Maria participates in a bicycle race. The speedway t ...

  8. restfull和传统http的区别

    摘自 https://bbs.csdn.net/topics/390716580 restfull是一种风格,不是规范,也不是所谓的封装,他只是将http协议用的更彻底了,我们在普通的开发中,虽然说是 ...

  9. Django URLs error: view must be a callable or a list/tuple in the case of include()

    Django 1.10 no longer allows you to specify views as a string (e.g. 'myapp.views.home') in your URL ...

  10. poj2186tarjan算法缩点求出度

    poj2186tarjan算法缩点求出度 自己打一遍第一题,入门啦,入门啦 题目还算简单,多头牛,给你仰慕关系(可传递),问你最后有没有牛被所有的牛仰慕 根据关系可以建图,利用tarjan算法缩点处理 ...