java 记事本 链表_任意参数的单链表实现 (Java8)

package DataStructure;

/**

* @author: Inki

* @email: inki.yinji@qq.com

* @create: 2020 1024

* @last_modify: 2020 1025

*/

public class MySingleLinkedList {

/**

* Only used to store the head node.

*/

private SingleNode head = new SingleNode(new Object());

/**

* The single linked list current size.

*/

private int size = 0;

/**

* Add element to the end of the list.

* @param:

* paraVal: The given value.

*/

public void add(AnyType paraVal) {

insert(size, paraVal);

}//Of add

/**

* Pop the last element.

* @return:

* The popped value.

*/

public AnyType pop(){

return delete(size - 1);

}//Of pop

/**

* Insert element at specified index.

* @param:

* paraIdx: The given index.

* paraVal: The given value.

*/

public void insert(int paraIdx, AnyType paraVal) {

if (paraIdx > size) {

throw new IndexOutOfBoundsException("The index error.");

}//Of if

SingleNode tempNode = head;

int i = 0;

while (i++ < paraIdx) {

tempNode = tempNode.next;

}//Of while

SingleNode paraNode = new SingleNode <>(paraVal);

paraNode.next = tempNode.next;

tempNode.next = paraNode;

size++;

}//of add

/**

* Delete the element at specified index.

* @param:

* paraIdx: The given index of element to delete.

* @return:

* The deleted value.

*/

public AnyType delete(int paraIdx) {

if (size == 0) {

throw new RuntimeException("The single linked list is empty.");

}//Of if

if (size <= paraIdx) {

throw new IndexOutOfBoundsException("The index error.");

}//Of if

SingleNode retNode = head;

int i = 0;

while (i++ < paraIdx) {

retNode = retNode.next;

}//Of while

retNode.next = retNode.next.next;

size--;

return retNode.val;

}//Of delete

/**

* Get the current size of the single linked list.

* @return:

* The current size of the single linked list.

*/

public int getSize() {

return size;

}//Of getSize

/**

* Display the single linked list.

*/

public void display() {

if (size == 0) {

throw new RuntimeException("The single linked list is empty.");

}//Of if

System.out.print("The single linked list is:\n[");

SingleNode tempNode = head;

int i = 0;

while (i++ < size - 1) {

tempNode = tempNode.next;

System.out.printf("%s, ", tempNode.val);

}//Of while

System.out.printf("%s]\n", tempNode.next.val);

}//Of display

/**

* The main function.

*/

public static void main(String[] args) {

MySingleLinkedList test = new MySingleLinkedList<>();

test.add('a');

test.add('b');

test.insert(0, 'c');

test.add('d');

test.insert(0, '5');

test.delete(4);

test.pop();

test.add('+');

test.display();

System.out.println(test.getSize());

}//Of main

}//Of class MySingleLinkedList

class SingleNode {

/**

* The value.

*/

AnyType val;

/**

* The next node.

*/

SingleNode next;

/**

* The first constructor.

* @param

* paraVal: The given value.

*/

SingleNode (AnyType paraVal) {

val = paraVal;

}//The first constructor

}//Of class SingleNode

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值