单链表---java实现
来源:互联网 发布:java主方法有什么作用 编辑:程序博客网 时间:2024/06/05 07:59
单链表优点:1.不需要预先给出元素个数。
2.单链表插入删除时不需要移动数据元素。
单链表缺点:1.每个节点有指针,空间利用率低。
2.单链表不支持随机读取数据。
Node.java
package com.sheepmu;public class Node { Object value;//结点的数据值 Node next;//下一个结点对象的引用public Node(Object value, Node next) {//一般结点的构造函数super();this.value = value;this.next = next;}public Node(Object value){ //!!!!this(value,null);}public Object getValue() {return value;}public void setValue(Object value) {this.value = value;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}public String toString(){return value.toString();}}
SingleLinkedList.java
package com.sheepmu; public class SingleLinkedList{ private Node head; private Node current; private int size; public SingleLinkedList(){ head=current=new Node(null);//初始时令head和current都是头指针,即下一结点为头结点,即数据域为空。 size=0; } public boolean isEmpty(){return size==0;} public int getSize(){return size;}//public void addToFirst(Object obj){//在表头添加//Node newNode=new Node(obj);// newNode.next=head;// head=newNode;//size++;//} public void addToLast(Object obj){//在表尾插入结点Node newNode=new Node(obj);current=head.next;while(current.next!=null) current=current.next;//得到当前尾结点current.next=newNode; newNode.next=null;size++; }public void insert(int i,Object obj){// 单链表 指定位置前插入Node newNode=new Node(obj);Node prve=head; current=head.next; int j=0; while(current!=null&&j<i){ prve=current; current=current.next; j++; } newNode.next=current; prve.next=newNode; size++;}public void delete(int i){//单链表删除指定位置Node prve=head;current=head.next;int j=0;while(current!=null&&j<i){ prve=current; current=current.next;j++;}prve.next=current.next;size--;} public Object getValue(int i){//得到结点值current=head.next;int j=0;while(current!=null&&j<i){current=current.next;j++;}return current.value;}public void print(){//遍历打印单链表if(isEmpty())System.out.println("链表为空");else{for(Node current=head.next;current!=null;current=current.next){System.out.print(current.value+" ");}}} }
SingleLinkedListTest.java
package com.sheepmu;/** * 建立一个线性表,依次输入元素0,1,2...9;然后在第4个位置插入9 ,然后删除数据元素7。最后依次显示当前线性表元素。 * 采用单链表实现。 * @author SheepMu * */public class SingleLinkedListText {public static void main(String[] args) throws Exception{SingleLinkedList singleLinkedList=new SingleLinkedList();System.out.println("初始线性表:");for(int i=0;i<10;i++){singleLinkedList.insert(i, new Integer(i));}singleLinkedList.print();System.out.println("在位置4插入元素9后的线性表:");singleLinkedList.insert(4, new Integer(9)); singleLinkedList.print();//System.out.println("表头插入元素0后的线性表:");//singleLinkedList.addToFirst(new Integer(0));//singleLinkedList.print();System.out.println("表尾插入元素0后的线性表:");singleLinkedList.addToLast(new Integer(0));singleLinkedList.print();System.out.println("删除第5个元素后的线性表:");singleLinkedList.delete(5);singleLinkedList.print(); }}
0 0
- 【算法数据结构Java实现】Java实现单链表
- java实现单链表
- Java实现单链表
- java--实现单链表
- Java单链表的实现
- java实现单链表
- java实现单链表
- 单链表java语言实现
- 单链表之Java实现
- java单链表实现
- 单链表操作java实现
- 单链表---java实现
- Java实现单链表
- Java实现单链表
- java语言实现单链表
- 单链表 java实现
- 用java实现单链表
- Java 实现单链表反序
- ffmpeg
- 黑马程序员---JAVA基础--正则表达式(十六)
- 《请求转发与重定向的区别》
- 关于安卓连接postgresql需要注意的问题
- Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'的解决
- 单链表---java实现
- Java 类访问权限
- 微信公众帐号开发教程第4篇-消息及消息处理工具的封装
- 《浅学IO流》
- MySQL编码latin1转utf8
- Common Lisp学习之六:文件IO操作与路径名抽象
- 微信公众帐号开发教程第5篇-各种消息的接收与响应
- “当前不会命中断点。源代码与原始版本不同”的问题的有效解决办法
- Crawler学习:3.Crawler Design