当前位置: 代码迷 >> 综合 >> 顺序表和单链表的基本实现(基于Java
  详细解决方案

顺序表和单链表的基本实现(基于Java

热度:94   发布时间:2024-01-19 14:21:17.0

顺序表: 

package test;public class SeqList {protected Object[] element;//设置一定长度的对象数组,利用数组实现顺序表的顺序存储和随机存取protected int n;public SeqList(int length){this.element = new Object[length];this.n = 0;//n为顺序表中对象数量,即顺序表实际长度}public SeqList(){this(64);}public SeqList(Object[] values){this(values.length);for(int i = 0;i<values.length;i++){this.element[i] = values[i];}this.n = element.length; //当利用形参数组values对顺序表存储对象结束后,更新实际长度n}public boolean isEmpty(){return this.n == 0;}public int size(){return this.n;}public Object get(int i){if(i>=0 && i<this.n){return this.element[i];}return null;}public int search(Object key){for(int i = 0;i<this.n;i++){if(key.equals(this.element[i])){return i;}}return -1;}public void set(int i, Object x){if(x == null){throw new NullPointerException("x == null");}if(i>=0 && i<this.n){this.element[i] = x;}else{throw new java.lang.IndexOutOfBoundsException(i+" ");}}public Object remove(int i){if(this.n>0 && i>=0 && i<this.n){Object oldguy = this.element[i];for(int j = i;j<this.n-1;j++){this.element[j] = this.element[j+1];}this.element[this.n-1] = null;this.n = this.n - 1;return oldguy;}return null;}public String toString(){String str = this.getClass().getName()+"(";if(this.n>0){str += this.element[0].toString();}for(int i = 1;i<this.n;i++){str += ","+this.element[i].toString();}return str+")";}public int insert(int i, Object x){if(x == null){throw new NullPointerException("x==null");}if(!(i>=0 && i<=this.n)){throw new java.lang.IndexOutOfBoundsException(i+" ");}else{if(1+this.n>this.element.length){Object[] buffer = new Object[2*this.element.length];for(int j = 0;j<this.n;j++){buffer[j] = this.element[j];}this.element = buffer;this.n += 1;}for(int j = this.n-1;j>=i;j--){this.element[j+1] = this.element[j];}this.element[i] = x;}return i;}public static void main(String[] args){Integer[] values = new Integer[20];for(int i = 0;i<20;i++){values[i] = Integer.valueOf(i);}SeqList sl = new SeqList(values);sl.remove(20);System.out.println(sl.toString());}
}

单链表:

package test;public class SinglyList {public Node<Integer> head;public SinglyList(){this.head = new Node<Integer>();}//初始化单链表,只有一个空的头结点public SinglyList(Integer[] values){this();Node<Integer> rear = this.head;for(int i = 0;i<values.length;i++){rear.next = new Node<Integer>(values[i],null);rear = rear.next;}}public boolean isEmpty(){return this.head == null?true:false;}public Integer get(int i){Node<Integer> p = this.head.next;for(int j = 0;j<i && p != null;j++){p = p.next;}return (i>=0 && p != null)?p.data:null;}public void set(int i, Integer x){Node<Integer> p = this.head.next;for(int j = 0;j<i;j++){p = p.next;}p.data = x;}public int size(){int i = 0;Node<Integer> p = this.head;while(p.next != null){p = p.next;i++;}return i;}public String toString(){String str = this.getClass().getName()+"(";Node<Integer> p = this.head;while(p.next != null){p = p.next;str += p.data.toString();if(p.next != null){str += ",";}}return str+")";}public Node<Integer> insert(int i,Integer x){if(x == null){throw new NullPointerException("x == null");}Node<Integer> front = this.head;for(int j = 0; j<i && front != null;j++){front = front.next;}Node<Integer> addguy = new Node<Integer>(x,null);addguy.next = front.next;front.next = addguy;return front.next;}public Integer remove(int i){Node<Integer> p = this.head;for(int j = 0;j<i && p.next != null;j++){p = p.next;}if(i>=0 && p.next != null){Integer old = p.next.data;p.next = p.next.next;return old;}return null;}public static void main(String[] args){Integer[] values = new Integer[20];for(int i = 0;i<values.length;i++){values[i] = i;}SinglyList sl = new SinglyList(values);System.out.println(sl.toString());sl.insert(1,111);System.out.println(sl.toString());sl.remove(1);System.out.println(sl.toString());}
}
package test;public class Node<Integer> {public Integer data;public Node<Integer> next;public Node(Integer data,Node<Integer> next){this.data = data;this.next = next;}public Node(){this(null,null);}public String toString(){return this.data.toString();}}