不合法:输出The position paraPosition is illegal.返回false
publicbooleaninsert(int paraPosition,int paraValue){Node tempNode = header;Node tempNewNode;for(int i =0; i < paraPosition; i++){if(tempNode.next ==null){System.out.println("The position "+ paraPosition +" is illegal.");returnfalse;}
tempNode = tempNode.next;}// of for i//构造一个新的结点
tempNewNode =newNode(paraValue);//将新构造的结点连接到链表中
tempNewNode.next = tempNode.next;
tempNode.next = tempNewNode;returntrue;}// Of insert
4.删除链表中的某个位置的数
首先判断链表是否为空
再判断待删除的位置是否合法
找到待删除位置的前一个位置
publicbooleandelete(int paraPosition){if(header.next ==null){System.out.println("Cannot delete element from an empty list.");returnfalse;}Node tempNode = header;for(int i =0; i < paraPosition; i++){if(tempNode.next.next ==null){System.out.println("The position "+ paraPosition +" is illegal.");returnfalse;}// of if
tempNode = tempNode.next;}// of for i
tempNode.next = tempNode.next.next;returntrue;}// of delete
5.完整代码实现
packagecom.datastructure;publicclassLinkedList{//内置类classNode{int data;Node next;//构造方法publicNode(int paraValue){
data = paraValue;
next =null;}}// of class Node//头节点Node header;//构造方法,创建一个空的链表publicLinkedList(){
header =newNode(0);//header.next = null; }// Of the first constructorpublicStringtoString(){String resultString ="";if(header.next ==null){return"empty";}// of ifNode tempNode = header.next;while(tempNode.next !=null){
resultString += tempNode.data +", ";
tempNode = tempNode.next;}
resultString += tempNode.data;return resultString;}// of toString//重置publicvoidreset(){
header.next =null;}// of reset/*
* 查找:
* 若找到,则返回数值所在的位置
* 若未找到,则返回-1
*/publicintlocate(int paraValue){int tempPosition =-1;Node tempNode = header.next;int tempCurrentPosition =0;//循环遍历链表while(tempNode !=null){if(tempNode.data == paraValue){
tempPosition = tempCurrentPosition;break;}
tempNode = tempNode.next;
tempCurrentPosition++;}// of whilereturn tempPosition;}// of locate//插入publicbooleaninsert(int paraPosition,int paraValue){Node tempNode = header;Node tempNewNode;for(int i =0; i < paraPosition; i++){if(tempNode.next ==null){System.out.println("The position "+ paraPosition +" is illegal.");returnfalse;}
tempNode = tempNode.next;}// of for i//构造一个新的结点
tempNewNode =newNode(paraValue);//将新构造的结点连接到链表中
tempNewNode.next = tempNode.next;
tempNode.next = tempNewNode;returntrue;}// of insert//删除publicbooleandelete(int paraPosition){if(header.next ==null){System.out.println("Cannot delete element from an empty list.");returnfalse;}Node tempNode = header;for(int i =0; i < paraPosition; i++){if(tempNode.next.next ==null){System.out.println("The position "+ paraPosition +" is illegal.");returnfalse;}// of if
tempNode = tempNode.next;}// of for i
tempNode.next = tempNode.next.next;returntrue;}// of deletepublicstaticvoidmain(String args[]){LinkedList tempFirstList =newLinkedList();System.out.println("Initialized, the list is: "+ tempFirstList.toString());for(int i =0; i <5; i++){
tempFirstList.insert(0, i);}// of for iSystem.out.println("Inserted, the list is: "+ tempFirstList.toString());
tempFirstList.insert(6,9);
tempFirstList.delete(4);
tempFirstList.delete(2);System.out.println("Deleted, the list is: "+ tempFirstList.toString());
tempFirstList.delete(0);System.out.println("Deleted, the list is: "+ tempFirstList.toString());for(int i =0; i <5; i++){
tempFirstList.delete(0);System.out.println("Looped delete, the list is: "+ tempFirstList.toString());}// of for i}// of main}// of class LinkedList
1.定义一个表示链表的类data表示这个链表中存储的数据…next指向下一个链表节点每当new 一个Node 实例,在内存中就会为其分配内存空间,它就会有一个内存地址class Node { int data; Node next; //构造方法 public Node(int paraValue) { data = paraValue; next = null; } }// of class Node2.查找链表中的数据循环遍历链表,比对数据,相同则