参考了 网上 部分 代码 单链表
来源:互联网 发布:无锡服务器数据恢复 编辑:程序博客网 时间:2024/04/30 06:00
单链表的基本操作 比较简单 但因为 自己在大二时候数据结构没学好 现在 重新 学一下希望 各位 能够 指正一下
/** * Link.java * Created on2017年4月17日 下午1:31:30 * All rights reserved */package javaPro;/** * @author 12091 * */public class Link {/** * @param args */public static void main(String[] args) {LinklinkList=new Link();linkList.addFirstNode(20);linkList.addFirstNode(19);linkList.addFirstNode(18);linkList.displayAllNodes();linkList.deleteBycontent(20);linkList.deleteByPos(0);linkList.displayAllNodes();System.out.println(linkList.findByIndex(0).data+"这是调用 findByIndex");System.out.println(linkList.findByData(19).data+"这是调用 findByData");linkList.insertByIndex(100, 1);linkList.displayAllNodes();linkList.deletefirst();linkList.displayAllNodes();}private Node first;private int pos;public Link(){this.first=null;}//增加一个first 节点public void addFirstNode(int data){Node node =new Node(data);node.next=first;first=node;}//按照索引删除一个节点public Node deleteByPos(int index){Node previous=first;Node current=first;while(pos!=index){previous=current;current=current.next;pos++;}if(first==current){first=first.next;}else{pos=0;previous.next=current.next;}return current;}//按照 data 删除一个节点public Node deleteBycontent(int data){Node previous=first;Node current=first;while(current.data!=data){if(current.next==null){return null;}previous=current;current=current.next;}if(current==first){first=first.next;}else{previous.next=current.next;}return current;}//打印 全部节点 数据部分(包括俩部分 数据 和指针(引用))public void displayAllNodes(){Node current=first;while(current!=null){System.out.println(current.data);current=current.next;}}//插入 节点public void insertByIndex(int data,int index){Node node=new Node(data);Node previous=first;Node current=first;while(pos!=index){previous=current;current=current.next;pos++;}if(current==first){node.next=first;System.out.println("ssssssss");first=node;}else{previous.next=node;node.next=current;pos=0;}}//删除 头结点并返回public Node deletefirst(){Node current=first;first=current.next;return current;}//根据 数据 查找 节点并返回 节点public Node findByData(int data){Node current=first;while(current.data!=data){if(current.next==null){return null;}current=current.next;}return current;}//根据 index查找 节点 并返回节点public Node findByIndex(int index){Node current=first;while(pos!=index){if(current.next==null){return null;}current=current.next;pos++;}pos=0;return current;}}
代码运行无误 若有疑问的朋友 直接 评论 我会看的
0 0
- 参考了 网上 部分 代码 单链表
- j2me判断机器平台代码---参考了部分网上资料
- j2me判断机器平台代码---参考了部分网上资料
- QQ2007密码盗取程序介绍(参考部分网上代码)
- 上传了部分实验代码供参考
- bootloader----自己写的,部分代码是参考网上的,转入pro模式后问题很多!
- 自己写的目录递归Perl代码。 参考了网上的其他源代码
- 参考了网上代码之后自己编写整数和真分数的四则运算,求指导(虽然我知道没人看。。。)
- HEVC参考帧列表管理代码部分
- 面向对象第三次作业部分参考代码
- 【代码参考网上的】linux串口编程学习笔记
- JDBC修改数据(部分代码参考查询部分)
- 经过不断摸索,我发现直接调用mysql C API更好,出了错也容易把握。以下是部分代码,贴出来给大家参考参考
- poj 2513(字典树等部分借鉴网上代码)
- 不同系统下的文件命名规则(部分参考网上资料)
- 常见的单端阻抗计算模式(参考网上的部分文章)
- 今天用httpclient访问jenkins遇到请求被转发到登录页面,看了网上的一些教程也没能解决。后来参考了下官方文档解决了,代码如下
- 重言式判定------参考了别人的代码。。
- 18. Hibernate_检索策略(lazy、fetch、batch-size等)
- 界面适配华为手机的虚拟按键的解决方案
- android 不同型号的终端的UI适配--一种解决方法
- SGU 438 动态网络流,SGU挂了,我也不知道A没A
- 网站性能体验34守则
- 参考了 网上 部分 代码 单链表
- iOS跳转微信朋友圈和扫一扫页面(已废弃)
- 一个简单的Makefile教程
- qrcode.js生成二维码
- 专利驳回的三大基本原因
- ORACLE PLSQL中文乱码解决,请留意细节
- Python中optparse模块使用学习
- java中graphics 2D的一些用法
- SuperMap iServer正式版war包在linux上的部署