数据结构----顺序表与单链表(JAVA)

Posted COOL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构----顺序表与单链表(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 = 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 = 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)的主要内容,如果未能解决你的问题,请参考以下文章

线性表——顺序表与单链表学习小结

用模板实现顺序表与单链表

单链表与顺序表的对比

C数据结构单链表接口函数逻辑解析与代码实现(含详细代码注释)

本科课程数据结构与算法实验2——单链表与双向循环链表的插入删除操作(C++实现)

#yyds干货盘点# 数据结构与算法之单链表