单链表的Java实现

复习数据结构吧:

原理就不说了,某天上午正好有空实现了下,直接上代码。

至于双向链表,单向循环链表,基于这个再改改就好了

public class  SingleLinkedList<T> {


class Node {
T ele = null;
Node next = null;


public Node(T ele) {
this.ele = ele;
}


@Override
public String toString() {
return "Node [ele=" + ele + ", next=" + next + "]";
}
}


private Node head = null;


public SingleLinkedList() {
}


/**
* 判断链表是否为空
* @return
*/
public boolean isEmpty() {
return this.head == null;
}



/**
* 鏈錶長度
* @return
*/
public int size() {
Node cur = this.head;
int count = 0;
while (cur != null) {
count += 1;
cur = cur.next;
}
return count;
}


private Node getLastNode() {
if (isEmpty()) {
return null;
}


// 找出头结点,往后遍历
Node cur = this.head;
while (cur.next != null)
cur = cur.next;
return cur;
}


/**
* 鏈錶最後一個元素
* @return
*/
public T getLast() {
if (isEmpty()) {
return null;
}


// 找出头结点,往后遍历
Node cur = this.head;
while (cur.next != null)
cur = cur.next;
return cur.ele;
}


/**
* 链表尾部添加元素
* @param ele
*/
public void append(T ele) {
Node node = new Node(ele);
// 如果该链表为空,则链表头结点指向该添加的结点
if (isEmpty()) {
this.head = node;
} else {
// 如果不为空,则该链表的最后一个结点指向该添加的结点
Node nodeLast = getLastNode();
nodeLast.next = node;
}
}

//指定位置获取结点
private Node getNode(int pos){
Node cur=this.head;
int point=0;
while(cur.next!=null){
if(point==pos){
return cur;
}
cur=cur.next;
point++;
}
return cur;
}

/**
* 指定位置获取数据
* @param pos
* @return
*/
public T get(int pos){
if(isEmpty()){
return null;
}
Node cur=this.head;
int point=0;
while(cur.next!=null){
if(point==pos){
return cur.ele;
}
cur=cur.next;
point++;
}
return cur.ele;
}

/**
* 删除指定位置的数据
* @param pos
*/
public T remove(int pos){

if(pos<=0){
return popFirst().ele;
}

if(pos>=size()-1){
return popLast().ele;
}

//获取到该位置的结点,前一位置的结点,以及后一位置的结点
Node nodePre=getNode(pos-1);
Node node=getNode(pos);
Node nodeNext=getNode(pos+1);
//改變指向
nodePre.next=nodeNext;
return node.ele;
}

private Node popLast(){
if(isEmpty())
return null;
//只有一个结点,返回该结点
if(size()==1){
Node node0=getNode(0);
this.head=null;
return node0;
}
//至少有兩個結點
Node node=getNode(size()-1);
Node nodePre=getNode(size()-2);
nodePre.next=null;
return node;
}


/**
* 得到首元素,并将该元素从链表中移除
* @return
*/
private Node popFirst(){
if(isEmpty())
return null;
//只有一个结点,返回该结点
if(size()==1){
Node node0=getNode(0);
this.head=null;
return node0;
}
//至少有兩個結點
Node node0=getNode(0);
Node node1=getNode(1);
this.head=node1;;
return node0;
}



@Override
public String toString() {
return "singleLinkedList [head=" + head + ", toString()="
+ super.toString() + "]";
}
}


测试代码:

SingleLinkedList<Integer> list = new SingleLinkedList<Integer>();
System.out.println("isempty:"+ list.isEmpty()+",size:"+list.size());
list.append(0);
list.append(1);
list.append(2);
list.append(3);
System.out.println("isempty:"+ list.isEmpty()+",size:"+list.size());
System.out.println("last:"+list.getLast());

for (int i = 0; i < list.size(); i++) {
System.out.println("遍历数据:"+list.get(i));
}
System.out.println("===============================");
Object removedData2=list.remove(0);
Object removedData=list.remove(3);
System.out.println("removedData2:"+removedData2);
System.out.println("removedData:"+removedData);
for (int i = 0; i < list.size(); i++) {
System.out.println("删除后遍历数据:"+list.get(i));
}


测试结果:

isempty:true,size:0
isempty:false,size:4
last:3
遍历数据:0
遍历数据:1
遍历数据:2
遍历数据:3
===============================
removedData2:0
removedData:3
删除后遍历数据:1
删除后遍历数据:2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值