电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Java實現線性表的鏈式存儲

瀏覽:17日期:2022-08-21 17:15:04

本文實例為大家分享了Java實現線性表的鏈式存儲,供大家參考,具體內容如下

鏈表:一種物理存儲結構上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現的。

package algorithm.datastructure.linklist;import java.util.NoSuchElementException;/** 鏈表* 物理存儲上非連續的存儲結構,數據元素的邏輯順序是通過鏈表中的指針鏈接次序實現* * */public class LinkedList { private Node head;//頭節點 private int size;//鏈表長度 static private class Node{ private int data; private Node next; public Node(){ } private Node(int data,Node next){ this.data=data; this.next=next; } } //初始化空鏈表 public LinkedList(){ //head=null; } //添加元素 public Boolean add(int element){ linkLast(element); return true; } //在某個位置之前添加元素 public Boolean add(int index,Integer element){ checkPositionIndex(index); if (index==size){ linkLast(element); } else { linkBefore(element,node(index)); } return true; } //根據下標獲取元素 public int get(int index){ checkElementIndex(index); return node(index).data; } //獲取第一個元素 public Integer getFirst(){ Node f=head; if (f==null){ throw new NoSuchElementException(); } else { return f.data; } } //獲取最后一個元素 public Integer getLast(){ if (size==0){ throw new NoSuchElementException(); } int index=size-1; return node(index).data; } //刪除第一個元素 public Integer removeFirst(){ Node f=head; if (f==null){ throw new NoSuchElementException(); } else { return unlink(head); } } //刪除最后一個元素 public Integer removeLast(){ if (size==0){ throw new NoSuchElementException(); } int index=size-1; return unlink(node(index)); } //根據索引刪除元素 public Integer remove(int index){ checkElementIndex(index); return unlink(node(index)); } //銷毀鏈表 public void destroyList(){ clearList(); } //將鏈表置為空表 public void clearList() { for (Node p=head;p!=null;){ Node next=p.next;//記錄下一個結點 p=null;//刪除當前結點 p=next;//指向下一個結點 } size=0; head=null; } //遍歷鏈表 public void traverseList(){ for (Node p=head;p!=null;){ System.out.println(p.data); p=p.next; } } //返回鏈表元素個數 public int size(){ return size; } //尾部添加結點 private void linkLast(int element){ Node cur =null,p; if (size==0){//沒有結點時 head=new Node(element,null); size++; return; } for (p=head;p!=null;){//有結點時候 cur=p; p=cur.next; } cur.next= new Node(element,null);//尾部添加元素 size++; } //在某結點之前插入結點 private void linkBefore(int element,Node node){ if (node==null){ linkLast(element); } else { Node p=head,q=p.next; if (node.data==p.data){//node為結點時候 head= new Node(element, p);//在頭部插入元素 size++; } else { while (p!=null){ if (q.data==node.data) { p.next= new Node(element,q);//在q之前(p之后)插入一個元素 size++; break; } p=p.next; q=p.next; } } } } //刪除結點 private Integer unlink(Node node){ Integer deleteNodeData=null; Node p=null; deleteNodeData=node.data; if (node.data==head.data){//該節點為頭結點 p=head; head=p.next; p=null; size--; } else { Node q=head; for (p=q.next;p!=null;){//使用兩個指針,p,q if (p.data==node.data){ break; } q=q.next;//p始終為q的next結點 p=q.next; } q.next=p.next; p=null;//刪除p size--; } return deleteNodeData; } //數組下標是否越界 private Boolean isElementIndex(int index){ return index>=0&&index<size; } //插入位置是否越界 public Boolean isPositionIndex(int index){ return index>=0&&index<=size; } //檢驗下標是否越界,拋出異常 private void checkElementIndex(int index){ if(!isElementIndex(index)){ try { throw new IndexOutOfBoundsException('下標越界'); } catch (Exception e) { e.printStackTrace(); } } } //檢驗插入下標是否越界,拋出異常 private void checkPositionIndex(int index){ if(!isPositionIndex(index)){ try { throw new IndexOutOfBoundsException('下標越界'); } catch (Exception e) { e.printStackTrace(); } } } //返回指定位置的元素 private Node node(int index){ int nowIndex = 0; if(size>0){ for (Node p=head;p!=null;){ if (nowIndex==index){ return p; } p=p.next; nowIndex++; } } return null; } public static void main(String[] args) { java.util.LinkedList linkedList0=new java.util.LinkedList(); linkedList0.add(6); linkedList0.remove(0); linkedList0.size(); linkedList0.peek(); //linkedList0.getFirst(); linkedList0.clear(); //測試 LinkedList linkedList=new LinkedList(); linkedList.add(2); linkedList.add(6); linkedList.add(0); linkedList.add(3); linkedList.add(8); linkedList.add(10); System.out.println(linkedList.get(0)); System.out.println(linkedList.getFirst()); System.out.println(linkedList.getLast()); System.out.println(linkedList.get(5)); System.out.println(linkedList.remove(5)); System.out.println(linkedList.remove(4)); linkedList.remove(2); linkedList.remove(0); linkedList.remove(0); linkedList.remove(0); linkedList.add(2); linkedList.add(6); linkedList.add(0); linkedList.add(3); linkedList.add(8); linkedList.add(10); linkedList.removeFirst(); linkedList.removeFirst(); linkedList.removeLast(); System.out.println(linkedList.size()); System.out.println('遍歷鏈表'); linkedList.traverseList(); linkedList.add(0,4); linkedList.add(0,5); linkedList.add(2,5); linkedList.add(4,5); linkedList.add(6,9); linkedList.add(8,11); linkedList.add(9,222); // linkedList.linkBefore(3,new Node(3,null));// linkedList.linkBefore(3,new Node(2,null));// linkedList.linkBefore(3,new Node(2,null));// linkedList.linkBefore(7,new Node(2,null));// linkedList.linkBefore(9,new Node(7,null));// linkedList.linkBefore(9,new Node(8,null)); System.out.println('遍歷鏈表'); linkedList.traverseList(); linkedList.clearList(); }}

以上就是Java簡單實現線性表的鏈式存儲,更多功能可參考Java集合中的LinkedList實現。

標簽: Java
相關文章:
主站蜘蛛池模板: 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 威客电竞(vk·game)·电子竞技赛事官网 | 帽子厂家_帽子工厂_帽子定做_义乌帽厂_帽厂_制帽厂 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 储能预警-储能消防系统-电池舱自动灭火装置-四川千页科技股份有限公司官网 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 在线钠离子分析仪-硅酸根离子浓度测定仪-油液水分测定仪价格-北京时代新维测控设备有限公司 | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 产业规划_产业园区规划-产业投资选址及规划招商托管一体化服务商-中机院产业园区规划网 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司 | 无纺布包装机|径向缠绕包装机|缠绕膜打包机-上海晏陵智能设备有限公司 | 成都中天自动化控制技术有限公司 | COD分析仪|氨氮分析仪|总磷分析仪|总氮分析仪-圣湖Greatlake | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 活性氧化铝|无烟煤滤料|活性氧化铝厂家|锰砂滤料厂家-河南新泰净水材料有限公司 | 注塑_注塑加工_注塑模具_塑胶模具_注塑加工厂家_深圳环科 | 手术室净化厂家_成都实验室装修公司_无尘车间施工单位_洁净室工程建设团队-四川华锐16年行业经验 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 除湿机|工业除湿机|抽湿器|大型地下室车间仓库吊顶防爆除湿机|抽湿烘干房|新风除湿机|调温/降温除湿机|恒温恒湿机|加湿机-杭州川田电器有限公司 | 六自由度平台_六自由度运动平台_三自由度摇摆台—南京全控科技 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 聚合氯化铝-碱式氯化铝-聚合硫酸铁-聚氯化铝铁生产厂家多少钱一吨-聚丙烯酰胺价格_河南浩博净水材料有限公司 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 庭院灯_太阳能景观灯_草坪灯厂家_仿古壁灯-重庆恒投科技 | 电销卡_稳定企业大语音卡-归属地可选-世纪通信 | 首页_中夏易经起名网| 二手色谱仪器,十万分之一分析天平,蒸发光检测器,电位滴定仪-湖北捷岛科学仪器有限公司 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 热熔胶网膜|pes热熔网膜价格|eva热熔胶膜|热熔胶膜|tpu热熔胶膜厂家-苏州惠洋胶粘制品有限公司 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 交联度测试仪-湿漏电流测试仪-双85恒温恒湿试验箱-常州市科迈实验仪器有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | 餐饮加盟网_特色餐饮加盟店_餐饮连锁店加盟| 冷油器,取样冷却器,热力除氧器-连云港振辉机械设备有限公司 | 气力输送设备_料封泵_仓泵_散装机_气化板_压力释放阀-河南锐驰机械设备有限公司 | 屏蔽泵厂家,化工屏蔽泵_维修-淄博泵业 |