当前位置: 首页 > wzjs >正文

衡阳网站定制网站站点

衡阳网站定制,网站站点,深圳外包公司排名,网站jsp充值和体现系统怎么做前言 大家好,我是Maybe。最近在学习数据结构中的链表,自己手动实现了一个LinkedList。我想与大家分享一下。 思维导图 代码部分 package Constant;public class constant {public static final String INDEX_IS_WRONG"输入的下标不合法"; }p…

前言

大家好,我是Maybe。最近在学习数据结构中的链表,自己手动实现了一个LinkedList。我想与大家分享一下。

思维导图

代码部分

package Constant;public class constant {public static final String INDEX_IS_WRONG="输入的下标不合法";
}
package utils;public class IndexException extends RuntimeException{public IndexException() {super();}public IndexException(String message) {super(message);}
}
public interface IList {// 头插法public void addFirst(int data);// 尾插法public void addLast(int data);// 任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data);// 查找是否包含关键字key是否在单链表当中public boolean contains(int key);// 删除第一次出现关键字为key的节点public void remove(int key);// 删除所有值为key的节点public void removeAllKey(int key);// 得到链表的长度public int size();public void display();public void clear();
}
import Constant.constant;
import utils.IndexException;public class LinkedList implements IList {//1.定义一个内部类static class ListNode{public int val;//类型写成了String,应该是ListNode的public ListNode prev;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode head;//这个就要给个尾了public ListNode last;@Overridepublic void addFirst(int data) {if(head==null){ListNode node=new ListNode(data);head=last=node;}else{ListNode node=new ListNode(data);node.next=head;head.prev=node;head=node;}}@Overridepublic void addLast(int data) {if(head==null){ListNode node=new ListNode(data);//写成了node=last=null了head=last=node;}else{ListNode node=new ListNode(data);last.next=node;node.prev=last;//这里要注意last=last.next;}}@Override//在LinkedList中找到对应的cur,然后再cur之前插入public void addIndex(int index, int data) {int len=size();if(index<0||index>len){String msg= constant.INDEX_IS_WRONG+index;throw new IndexException(msg);}if(index==0){addFirst(data);}else if(index==len){addLast(data);}else{ListNode node=new ListNode(data);ListNode cur=findIndex(index);node.next=cur;cur.prev.next=node;node.prev=cur.prev;cur.prev=node;}}//在LinkedList中找到对应的curprivate ListNode findIndex(int index){if(head==null){return null;}else{ListNode cur=head;while(index!=0){cur=cur.next;index--;}return cur;}}@Overridepublic boolean contains(int key) {if(head==null){return false;}else{ListNode cur=head;while(cur!=null){if(cur.val==key){return true;}else{cur=cur.next;}}return false;}}@Overridepublic void remove(int key) {//1.先判断链表是否为空if(head==null){return;}else{ListNode cur=head;while(cur!=null){if(cur.val==key){//1.考虑头删的情况if(cur==head){head=head.next;//考虑如果链表中只有一个节点的情况if(head!=null){head.prev=null;}}else{cur.prev.next=cur.next;//尾巴节点和中间节点共用if(cur.next==null){//尾节点last=last.prev;}else{//中间节点cur.next.prev=cur.prev;}}return;}cur=cur.next;}}}@Overridepublic void removeAllKey(int key) {if(head==null){return;}else{ListNode cur=head;while(cur!=null){if(cur.val==key){if(cur==head){head=head.next;//只有一个节点的情况要考虑if(head!=null){head.prev=null;}}else{cur.prev.next=cur.next;if(cur.next==null){last=last.next;}else{cur.next.prev=cur.prev;}}}cur=cur.next;}}}@Overridepublic int size() {if(head==null){return 0;}else{ListNode cur=head;int count=0;while(cur!=null){cur=cur.next;count++;}return count;}}@Overridepublic void display() {if(head==null){return;}else{ListNode cur=head;while(cur!=null){System.out.print(cur.val+" ");cur=cur.next;}System.out.println();}}@Overridepublic void clear() {if(head==null){return;}else{ListNode cur=head;while(cur!=null){ListNode curN=cur.next;cur.prev=null;cur.next=null;cur=curN;}head=last=null;//最后要把head和last置为null}}
}
import utils.IndexException;public class Test {public static void main(String[] args) {LinkedList linkedList=new LinkedList();linkedList.addLast(1);linkedList.addLast(1);linkedList.addLast(1);linkedList.addLast(2);linkedList.display();
//        linkedList.addFirst(1);
//        linkedList.addFirst(2);
//        linkedList.addFirst(3);
//        linkedList.addFirst(4);
//        linkedList.display();
//        int ret=linkedList.size();
//        System.out.println(ret);
//        try{
//            linkedList.addIndex(2,100);
//
//        }catch (IndexException e){
//            e.printStackTrace();
//        }
//        linkedList.display();
//        linkedList.remove(1);
//        linkedList.display();
//        linkedList.removeAllKey(1);linkedList.clear();linkedList.display();}
}

结语 

本次分享到此结束啦。希望可以帮到有需要的人!

 

 

 

 

 

 


文章转载自:

http://7L6nFSbi.bpwdc.cn
http://BPinf93K.bpwdc.cn
http://azXMYCmM.bpwdc.cn
http://YHR32Qi8.bpwdc.cn
http://tsK4Jpa8.bpwdc.cn
http://ZmJybYJJ.bpwdc.cn
http://cHdViwbt.bpwdc.cn
http://pXjbsxzU.bpwdc.cn
http://OI8cNusm.bpwdc.cn
http://BS7ADpEo.bpwdc.cn
http://sQSLJ4Y5.bpwdc.cn
http://Mfsd0k6i.bpwdc.cn
http://dOa2Un9G.bpwdc.cn
http://6zVm8dCH.bpwdc.cn
http://cwyaRUmi.bpwdc.cn
http://TkIIz1fA.bpwdc.cn
http://Dznm8jTm.bpwdc.cn
http://k0fJL0ok.bpwdc.cn
http://PAwaMgSu.bpwdc.cn
http://aFApoMYN.bpwdc.cn
http://uwH3abPD.bpwdc.cn
http://rOipAWqC.bpwdc.cn
http://prT7ANi9.bpwdc.cn
http://LLdHvLMX.bpwdc.cn
http://W6ANS3Id.bpwdc.cn
http://Rp8AoRKd.bpwdc.cn
http://FrhXsDRS.bpwdc.cn
http://XZjhH1vE.bpwdc.cn
http://wF8hPVJN.bpwdc.cn
http://xhigHPmN.bpwdc.cn
http://www.dtcms.com/wzjs/746185.html

相关文章:

  • 国际设计师网站有哪些1小时前俄乌战况消息
  • 怎么看到网站开发时间网站建设成本
  • 网站建设续费是那些驻马店网站建设熊掌号
  • 八角网站建设旅游网站设计说明
  • 成都cms建站52麻将官方网站做代理
  • 哪个网站做免费小程序商城网站建设需要多少钱
  • 团建网站建设wordpress阿里云建站
  • 自己做的网站能上传吗国内营销策划公司排名
  • 设计素材网站酷p搜狗识图
  • 做盗版小说网站赚钱嘛云南建设厅网站首页
  • 长春网站制作企业做网站的必要
  • 怎样做分销网站百度联盟添加网站
  • 网站的推广一般有什么方式便宜购 网站建设
  • 云南省城市建设培训中心网站上海公司注册地址可以是住宅吗
  • 北京规划网站网站建设招聘需求
  • 百度推广代理怎么加盟电子商务沙盘seo关键词
  • 照明公司网站制作公司网络组建方案设计
  • 做网站什么系统简单网站建设案例收费吗
  • 马鞍山做网站的公司78做网站的公司怎么转型
  • 网站制作对公司的作用优秀seo外包平台
  • html手机网站条形码生成器在线制作图片
  • 读经典做临床报名网站东莞seo排名优化公司
  • 网站调研怎样做网站建设怎样接业务
  • 没有外贸网站 如果做外贸宁波最好的推广平台
  • 未央免费做网站找广网
  • ps 做ui比较好的网站江苏seo技术教程
  • 网站左侧图片悬浮代码环球广贸WordPress
  • 网站做单链 好不好营销型企业网站建设
  • 购买了个网站源码 怎么建立个人网站开发制作教程
  • 网页设计与制作做网站wordpress4.5.1