总结以下Java数据结构
1.数组
- 这里我们使用面向对象编程,使用自定义类封装数组
添加类方法来实现数据操作
MyArray.java
package ch01;
public class MyArray {
private long[] arr;
//表示有效数据的长度
private int element;
public MyArray() {
arr = new long[50];
}
public MyArray(int maxsize) {
arr = new long[maxsize];
}
/**
* 添加数据
*/
public void insert(long value) {
arr[element] = value;
element++;
}
/**
* 显示数据
*/
public void display() {
System.out.print("[");
for(int i = 0; i < element;i++) {
System.out.print(arr[i]+" ");
}
System.out.println("]");
}
/**
* 查找数据
*/
public int search(long value) {
int i;
for (i = 0; i < element; i++) {
if (value == arr[i]) {
break;
}
}
if (i == element) {
return -1;
}else {
return i;
}
}
/**
* 根据索引来查
*/
public long get(int index) {
if (index>= element || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
return arr[index];
}
}
/**
* 删除数据
*/
public void delete(int index) {
if (index>= element || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
for (int i = index; i < element; i++) {
arr[index] = arr[index+1];
}
element--;
}
}
/**
* 更新数据
*/
public void change(int index, int newvalue) {
if (index>= element || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
arr[index] = newvalue;
}
}
}
进行以下简单的测试
TestMyArray.java
package ch01;
public class TestMyArray {
public static void main(String[] args) {
MyArray myArray = new MyArray();
myArray.insert(13);
myArray.insert(34);
myArray.insert(90);
myArray.display();
System.out.println(myArray.search(34));
System.out.println(myArray.get(2));
//
// myArray.delete(1);
// myArray.display();
myArray.change(0, 12);
myArray.display();
}
}
以下为结果:
[13 34 90 ]
1
90
[12 34 90 ]
有序数组:存放时按照一定的大小顺序存放
MyOrderArray.java
package ch01;
public class MyOrderArray {
private long[] arr;
//表示有效数据的长度
private int element;
public MyOrderArray() {
arr = new long[50];
}
public MyOrderArray(int maxsize) {
arr = new long[maxsize];
}
/**
* 添加数据
*/
public void insert(long value) {
int i;
for (i = 0; i < element; i++) {
if (arr[i]>value) {
break;
}
}
for(int j = element; j > i; j--) {
arr[j] = arr[j - 1];
}
arr[i] = value;
element++;
}
/**
* 显示数据
*/
public void display() {
System.out.print("[");
for(int i = 0; i < element;i++) {
System.out.print(arr[i]+" ");
}
System.out.println("]");
}
/**
* 查找数据
*/
public int search(long value) {
int i;
for (i = 0; i < element; i++) {
if (value == arr[i]) {
break;
}
}
if (i == element) {
return -1;
}else {
return i;
}
}
/**
* 二分查找
*/
public int binarySearch(long value) {
int middle = 0;
int low = 0;
int pow = element;
while(true) {
middle = (low+pow)/2;
if (arr[middle]==value) {
return middle;
} else if (low>pow) {
return -1;
}else {
if (arr[middle] > value) {
pow = middle - 1;
} else {
low = middle + 1;
}
}
}
}
/**
* 根据索引来查
*/
public long get(int index) {
if (index>= element || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
return arr[index];
}
}
/**
* 删除数据
*/
public void delete(int index) {
if (index>= element || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
for (int i = index; i < element; i++) {
arr[index] = arr[index+1];
}
element--;
}
}
/**
* 更新数据
*/
public void change(int index, int newvalue) {
if (index>= element || index < 0) {
throw new ArrayIndexOutOfBoundsException();
} else {
arr[index] = newvalue;
}
}
}
TestMyOrderArray.java
package ch01;
public class TestMyOrderArray {
public static void main(String[] args) {
MyOrderArray array = new MyOrderArray();
array.insert(90);
array.insert(30);
array.insert(80);
array.insert(10);
array.display();
System.out.println(array.binarySearch(10));
}
}
[10 30 80 90 ]
0
2.简单排序
占坑,下次写
3.栈和队列
- 栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。
package ch03;
public class MyStack {
//底层是一个数组
private long[] arr;
private int top;
/**
* 默认构造方法
*/
public MyStack() {
arr = new long[10];
top = -1;
}
/**
* 带参构造方法,参数为数组初始化大小
*/
public MyStack(int maxsize) {
arr = new long[maxsize];
top = -1;
}
/**
* 添加数据
*/
public void push(int value) {
arr[++top] = value;
}
/**
* 移除数据
*/
public long pop() {
return arr[top--];
}
/**
* 查看数据
*/
public long peek() {
return arr[top];
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return top == -1;
}
/**
* 判断是否满了
*/
public boolean isFull() {
return top == arr.length-1;
}
}
TestMyStack.java
package ch03;
public class TestMyStack {
public static void main(String[] args) {
MyStack ms = new MyStack(4);
ms.push(23);
ms.push(12);
ms.push(1);
ms.push(90);
System.out.println(ms.isEmpty());
System.out.println(ms.isFull());
System.out.println(ms.peek());
System.out.println(ms.peek());
while(!ms.isEmpty()) {
System.out.print(ms.pop() + " ");
}
System.out.println(ms.isEmpty());
}
}
false
true
90
90
90 1 12 23 true
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
MyQueue.java
package ch03;
public class MyQueue {
//底层使用数组
private long[] arr;
//有效数据的大小
private int element;
//队头
private int front;
//队尾
private int end;
/**
* 默认构造方法
*/
public MyQueue() {
arr = new long[10];
element = 0;
front = 0;
end = -1;
}
/**
* 带参数的构造方法,参数为数组的大小
*/
public MyQueue(int maxsize) {
arr = new long[maxsize];
element = 0;
front = 0;
end = -1;
}
/**
* 添加数据,从队尾插入
*/
public void insert(long value) {
arr[++end] = value;
element++;
}
/**
* 删除数据,从队头删除
*/
public long remove() {
element--;
return arr[front++];
}
/**
* 查看数据
*/
public long peek() {
return arr[front];
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return element == 0;
}
/**
* 判断是否满了
*/
public boolean isFull() {
return element == arr.length;
}
}
TestMyQueue.java
package ch03;
public class TestMyQueue {
public static void main(String[] args) {
MyQueue myQueue = new MyQueue(4);
myQueue.insert(23);
myQueue.insert(45);
myQueue.insert(13);
myQueue.insert(1);
System.out.println(myQueue.isFull());
System.out.println(myQueue.isEmpty());
System.out.println(myQueue.peek());
System.out.println(myQueue.peek());
while(!myQueue.isEmpty()) {
System.out.print(myQueue.remove()+ " ");
}
}
}
true
false
23
23
23 45 13 1
4.链表
- 链表链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。
首先我们先创建结点Node
Node.java
package ch04;
/**
* 链结点,相当于车厢
* @author yangjiaxin
*
*/
public class Node {
//数据域
public long data;
//节点域 指针域
public Node next;
public Node(long value) {
this.data = value;
}
/**
* 显示方法
*/
public void display() {
System.out.print(data+" ");
}
}
之后创建LinkedList.java,创建链表,并对链表进行操作
package ch04;
/**
* 链表,相当于火车
* @author yangjiaxin
*
*/
public class LinkList {
//头节点
private Node first;
public LinkList() {
first = null;
}
/**
* 插入一个结点,在头结点后进行插入
*
*/
public void insertFirst(long value) {
Node node = new Node(value);
node.next = first;
first = node;
}
/**
* 删除一个结点,在头节点后进行删除
*/
public Node deleteFirst() {
Node temp = first;
first = temp.next;
return temp;
}
/**
* 显示方法
*/
public void display() {
Node current = first;
while(current != null) {
current.display();
current = current.next;
}
System.out.println();
}
/**
* 查找方法
*/
public Node find(long value) {
Node current = first;
while(current.data != value) {
if (current.next ==null) {
return null;
}
current = current.next;
}
return current;
}
/**
* 删除方法,根据数据域进行删除
*/
public Node delete(long value) {
Node current = first;
Node previous = first;
while(current.data != value) {
if (current.next == null) {
return null;
}
previous = current;
current = current.next;
}
if (current == first) {
first = first.next;
} else {
previous.next = current.next;
}
return current;
}
}
之后进行测试:
TestLinkList.java
package ch04;
public class TestLinkList {
public static void main(String[] args) {
LinkList linkList = new LinkList();
linkList.insertFirst(34);
linkList.insertFirst(23);
linkList.insertFirst(12);
linkList.insertFirst(0);
linkList.insertFirst(-1);
linkList.display();
// linkList.deleteFirst();
// linkList.display();
//
// Node node = linkList.find(23);
// node.display();
//
Node node1= linkList.delete(0);
node1.display();
System.out.println();
linkList.display();
}
}
-1 0 12 23 34
0
-1 12 23 34
5.双向链表
这里我们要修改以下Node.java使结点具有向前的指针域
Node.java
package ch05;
/**
* 链结点,相当于车厢
* @author yangjiaxin
*
*/
public class Node {
//数据域
public long data;
//节点域 指针域
public Node next;
public Node previous;
public Node(long value) {
this.data = value;
}
/**
* 显示方法
*/
public void display() {
System.out.print(data+" ");
}
}
之后创建DoubleLinkedList.java
package ch05;
/**
* 双向链表
* @author yangjiaxin
*
*/
public class DoubleLinkList {
//头节点
private Node first;
private Node last;
public DoubleLinkList() {
first = null;
last = null;
}
/**
* 插入一个结点,在头结点后进行插入
*
*/
public void insertFirst(long value) {
Node node = new Node(value);
if (isEmpty()) {
last = node;
} else {
first.previous = node;
}
node.next = first;
first = node;
}
/**
* 插入一个结点,从尾节点进行插入
*/
public void insertLast(long value) {
Node node = new Node(value);
if (isEmpty()) {
first = node;
} else {
last.next = node;
node.previous = last;
}
last = node;
}
/**
* 删除一个结点,在头节点后进行删除
*/
public Node deleteFirst() {
Node temp = first;
if (first.next == null) {
last = null;
} else {
first.next.previous = null;
}
first = temp.next;
return temp;
}
/**
* 删除结点,从尾部删除
*/
public Node deleteLast() {
if (first.next == null) {
first = null;
} else {
last.previous.next = null;
}
last = last.previous;
return last;
}
/**
* 显示方法
*/
public void display() {
Node current = first;
while(current != null) {
current.display();
current = current.next;
}
System.out.println();
}
/**
* 查找方法
*/
public Node find(long value) {
Node current = first;
while(current.data != value) {
if (current.next ==null) {
return null;
}
current = current.next;
}
return current;
}
/**
* 删除方法,根据数据域进行删除
*/
public Node delete(long value) {
Node current = first;
while(current.data != value) {
if (current.next == null) {
return null;
}
current = current.next;
}
if (current == first) {
first = first.next;
} else {
current.previous.next = current.next;
}
return current;
}
/**
* 判断是否为空
*/
public boolean isEmpty() {
return (first == null);
}
}
6.递归的应用
程序调用自身的编程技巧称为递归( recursion)。
这里简单说一下汉诺塔:
package ch07;
public class HanoiTower {
/**
* 移动盘子
* @param topN 移动的盘子数
* @param from 起始塔座
* @param inter 中间塔座
* @param to 目标塔座
*/
public static void doTower(int topN,char from,char inter,char to) {
if (topN == 1) {
System.out.println("盘子1,从"+ from +"塔座到"+ to + "塔座");
} else {
doTower(topN-1, from, to, inter);
System.out.println("盘子"+topN+",从"+from+"塔座到"+to+"塔座");
doTower(topN - 1, inter, from, to);
}
}
}
Test.java
package ch07;
public class Test {
public static void main(String[] args) {
HanoiTower.doTower(4, 'A', 'B', 'C');
}
}
盘子1,从A塔座到B塔座
盘子2,从A塔座到C塔座
盘子1,从B塔座到C塔座
盘子3,从A塔座到B塔座
盘子1,从C塔座到A塔座
盘子2,从C塔座到B塔座
盘子1,从A塔座到B塔座
盘子4,从A塔座到C塔座
盘子1,从B塔座到C塔座
盘子2,从B塔座到A塔座
盘子1,从C塔座到A塔座
盘子3,从B塔座到C塔座
盘子1,从A塔座到B塔座
盘子2,从A塔座到C塔座
盘子1,从B塔座到C塔座
持续更新