Flask接口签名sign原理与实例代码浅析
262
2022-11-15
Java 8实现任意参数的单链表
本文实例为大家分享了java 8实现任意参数的单链表,供大家参考,具体内容如下
1、实现功能
1)add():链表末尾添加元素;
2)pop():移除链表尾部元素;
3)insert():指定索引处添加元素;
4)delete():指定索引处删除元素;
5)getSize():获取链表当前长度;
6)display():展示链表当前元素。
2、代码
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
/**
* 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 int i = 0; while (i++ < paraIdx) { tempNode = tempNode.next; }//Of while SingleNode 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 int i = 0; while (i++ < paraIdx) { retNode = retNode.next; }//Of while retNode.next = retNode.next.next; size--; return retNode.val; }//Of delete /** * Get thehttp:// 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 http://linked list is:\n["); SingleNode int i = 0; while (i++ < size - 1)fUcFhiRi { 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.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 /** * The first constructor. * @param * paraVal: The given value. */ SingleNode (AnyType paraVal) { val = paraVal; }//The first constructor }//Of class SingleNode
int i = 0;
while (i++ < paraIdx) {
tempNode = tempNode.next;
}//Of while
SingleNode
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
int i = 0;
while (i++ < paraIdx) {
retNode = retNode.next;
}//Of while
retNode.next = retNode.next.next;
size--;
return retNode.val;
}//Of delete
/**
* Get thehttp:// 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 http://linked list is:\n[");
SingleNode
int i = 0;
while (i++ < size - 1)fUcFhiRi {
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.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
/**
* The first constructor.
* @param
* paraVal: The given value.
*/
SingleNode (AnyType paraVal) {
val = paraVal;
}//The first constructor
}//Of class SingleNode
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~