首页 > 代码库 > 数据结构----顺序表与单链表(JAVA)
数据结构----顺序表与单链表(JAVA)
下面为学习顺序表和单链表的一些基本操作函数:
1 public class SeqList<T> extends Object { 2 protected int n; 3 protected Object[] element; 4 5 public SeqList(int length) { 6 this.element = new Object[length]; 7 this.n = 0; 8 } 9 10 public SeqList() { 11 this(64); 12 } 13 14 public SeqList(T[] values) { 15 this(values.length); 16 for (int i = 0; i < values.length; i++) { 17 this.element[i] = values[i]; 18 } 19 this.n = element.length; 20 21 } 22 23 public boolean isEmpty() { 24 if (this.n == 0) 25 return true; 26 return false; 27 } 28 29 public int size() { 30 return this.n; 31 } 32 33 public T get(int i) { 34 if (i >= 0 && i < this.n) { 35 return (T) this.element[i]; 36 } 37 return null; 38 } 39 40 public void set(int i, T x) { 41 if (x == null) 42 throw new NullPointerException("x==null"); 43 if (i >= 0 && i < this.n) 44 this.element[i] = x; 45 else 46 throw new java.lang.IndexOutOfBoundsException(i + ""); 47 } 48 49 public int insert(int i, T x) { // 插入x元素 50 if (x == null) 51 throw new NullPointerException("x==null"); 52 if (i < 0) 53 i = 0; 54 if (i > this.n) 55 i = this.n; 56 Object[] source = this.element; 57 if (this.n == element.length) { 58 this.element = new Object[source.length * 2]; // 申请一个2倍大的数组 59 for (int j = 0; j < i; j++) { 60 this.element[j] = source[j]; 61 62 } 63 } 64 for (int j = this.n - 1; j >= i; j--) { 65 element[j + 1] = source[j]; 66 67 } 68 69 this.element[i] = x; 70 this.n++; 71 return i; 72 } 73 74 public int insert(T x) { // 表尾插入x元素,成员方法重载 75 return this.insert(this.n, x); 76 } 77 78 public T remove(int i) { // 删除数组下标为i的元素 79 if (this.n >= 0 && i >= 0 && i < this.n) { 80 T old = (T) this.element[i]; 81 for (int j = i; j < this.n - 1; j++) { 82 element[j] = element[j + 1]; 83 84 } 85 this.element[this.n - 1] = null; 86 this.n--; 87 return old; 88 } 89 return null; 90 91 } 92 93 public void clear() { // 删除所有元素 94 this.n = 0; 95 }
1 public class Node<T> { // 结点类 2 public T data; 3 public Node<T> next; 4 5 public Node(T data, Node<T> next) { 6 this.data =http://www.mamicode.com/ data; 7 this.next = next; 8 } 9 10 public Node() { 11 this(null, null); 12 } 13 14 } 15 public class SinglyList<T> extends Object { //带头结点的单链表类 16 public Node<T> head; 17 18 public SinglyList() { 19 this.head = new Node<T>(); 20 } 21 22 public SinglyList(T[] values) { 23 this(); 24 Node<T> rear = this.head; 25 for (int i = 0; i < values.length; i++) { 26 rear.next = new Node<T>(values[i], null); 27 rear = rear.next; 28 } 29 } 30 31 public boolean isEmpty() { 32 return this.head.next == null; 33 } 34 35 public T get(int i) { 36 Node<T> p = this.head.next; 37 for (int j = 0; p != null && j < i; j++) { 38 p = p.next; 39 } 40 return (i >= 0 && p != null) ? p.data : null; 41 } 42 43 public void set(int i, T x) { 44 if (i < 0 || i > size()) 45 throw new IndexOutOfBoundsException(i + ""); 46 if (x == null) 47 throw new NullPointerException("x==null"); 48 Node<T> p = this.head.next; 49 for (int j = 0; p != null && j < i; j++) { 50 p = p.next; 51 } 52 p.data =http://www.mamicode.com/ x; 53 54 } 55 56 public int size() { 57 int len = 0; 58 Node<T> p = this.head.next; 59 if (p == null) 60 return -1; 61 while (p != null) { 62 len++; 63 p = p.next; 64 65 } 66 return len; 67 } 68 69 public Node<T> insert(int i, T x) { 70 if (x == null) 71 throw new NullPointerException("x==null"); 72 Node<T> front = this.head; 73 for (int j = 0; front.next != null && j < i; j++) { 74 front = front.next; 75 } 76 front.next = new Node<T>(x, front.next); 77 return front.next; 78 79 } 80 81 public Node<T> insert(T t) { 82 return insert(Integer.MAX_VALUE, t); 83 } 84 85 public T remove(int i) { 86 Node<T> front = this.head; 87 for (int j = 0; front.next != null && j < i; j++) { 88 front = front.next; 89 } 90 if (i >= 0 && front.next != null) { 91 T old = front.next.data; 92 front.next = front.next.next; 93 return old; 94 } 95 return null; 96 97 } 98 99 public void clear() { 100 this.head.next = null; 101 }
数据结构----顺序表与单链表(JAVA)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。