Java实现线性表-顺序表示和链式表示(java),Java高级开发岗必问知识点

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; //数据

Node next; //下一个节点

Node(){}

Node(E e){

this.e = e;

this.next = null;

}

}

public class LinkedList {

private Node header = null; //头结点

int size=0; //链表大小

public LinkedList() {

this.header = new Node();

}

//得到链表的长度

public int length() {

return size;

}

//按值查找节点,返回该节点的位置

public int locateElem(E e) {

Node temp = header;

int count = 1;

while(temp.next != null && temp.e != e){

temp = temp.next;

count ++;

}

return count;

}

//找到第index个位置的节点

public Node getNode(int index) {

if(index > size || index < 0){

throw new RuntimeException(“索引值有错:” + index);

}

Node temp = new Node();

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 newnode = new Node(e); //根据需要添加的内容封装为节点

Node last = getNode(size); //得到最后一个节点

last.next = newnode;

}

size ++;

return true;

}

//头添加

public boolean addTofirst(E e) {

if(size == 0){

header.e = e;

}else{

Node newnode = new Node(e); //根据需要添加的内容封装为节点

newnode.next = header

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

.next;

header.next = newnode;

}

size ++;

return true;

}

//插入到第index个的位置

public boolean insert(int index, E e) {

Node newnode = new Node(e); //根据需要添加的内容封装为节点

Node cnode = getNode(index-1); //得到第index-1个节点

newnode.next = cnode.next;

cnode.next = newnode;

size++;

return true;

}

//删除第index个节点

public boolean delete(int index) {

Node prinode = getNode(index-1); //得到被删除的节点的前一个节点

Node 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 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;

TNode prior, next;

TNode(){}

TNode(E e){

this.e = e;

prior = null;

next = null;

}

}

public class DoubleLinkedList {

private TNode header = null; //头结点

int size=0; //链表大小

public DoubleLinkedList(){

this.header = new TNode();

}

//尾添加

public boolean addToLast(E e) {

if(size == 0){

header.e = e;

}else{

TNode TNode = new TNode(e); //根据需要添加的内容封装为节点

TNode last = getNode(size); //得到最后一个节点

last.next = TNode;

TNode.prior=last;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值