单链表---java实现
2013-12-29 22:41
267 查看
单链表优点:1.不需要预先给出元素个数。
2.单链表插入删除时不需要移动数据元素。
单链表缺点:1.每个节点有指针,空间利用率低。
2.单链表不支持随机读取数据。
Node.java
SingleLinkedList.java
SingleLinkedListTest.java
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(); } }
相关文章推荐
- 剑指offer面试题java实现之题5:逆序打印链表
- Java实现-重排链表
- java链表实现Stack以及后缀表达式的实现
- java实现链表的反转
- Java实现寻找链表的中间节点
- Java实现链表逆序
- Java模拟单向链表和双向链表的实现
- java实现双向循环链表
- 剑指offer----两个链表的第一个公共节点----java实现
- 剑指offer编程题Java实现——面试题5从头到尾打印链表
- Java通过链表实现栈
- Java数组和链表的底层实现
- 约瑟夫环(丢手帕游戏)Java 链表实现(程序正确无误)
- 【LintCode-167】链表求和(Java实现)
- 剑指offer--面试题13:在O(1)时间删除链表结点--Java实现
- 链表的java实现与时间和空间复杂度分析
- java队列的链表实现
- java 实现节点链表
- 链表问题之链表反转<Java实现>
- 《剑指offer》编程题java实现(三):从尾到头打印链表