稀疏数组
package 稀疏数组;
public class SparseArray {
public static void main(String[] args) {
int[][] array=ShuZu();
int sum=0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if (array[i][j]!=0){
sum++;
}
}
}
int[][] arr=new int[sum+1][3];
arr[0][0]=11;
arr[0][1]=11;
arr[0][2]=sum;
int a =1;
int b =0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j]!=0){
arr[a][b]=i;
arr[a][b+1]=j;
arr[a][b+2]=array[i][j];
a++;
}
}
}
System.out.println("-------------新的的二维数组--------------");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]);
System.out.print(" ");
}
System.out.println();
}
int[][] newarray=new int[arr[0][0]][arr[0][0]];
for (int i = 1; i <arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
newarray[arr[i][0]][arr[i][1]]=arr[i][2];
}
}
System.out.println("-----------还原后的二维数组------------");
for (int i = 0; i < newarray.length; i++) {
for (int j = 0; j < newarray[i].length; j++) {
System.out.print(newarray[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
public static int[][] ShuZu(){
int[][] array=new int[11][11];
array[1][2]=1;
array[2][3]=2;
System.out.println("-------------原始的二维数组--------------");
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]);
System.out.print(" ");
}
System.out.println();
}
return array;
}
}
队列
package 队列;
import java.util.Scanner;
public class 数组模拟环形队列 {
public static void main(String[] args) {
aa aa = new aa();
aaa:
for (; ; ) {
System.out.println("----------###菜单栏###----------");
System.out.println("----------1 存数据 -----------");
System.out.println("----------2 取数据 ----------");
System.out.println("----------3 显数据 ---------");
System.out.println("----------4 退出 ------------");
int bb = new Scanner(System.in).nextInt();
switch (bb) {
case 1:
aa.add();
break;
case 2:
aa.get();
break;
case 3:
aa.show();
break;
case 4:
System.out.println("退出成功...");
break aaa;
default:
System.out.println("你的输入有误...");
}
}
}
}
class aa {
int[] array = new int[4];
int maxsize = array.length;
int start = 0;
int end = 0;
public boolean isempty() {
return start == end;
}
public boolean isfull() {
return (end + 1) % maxsize == start;
}
public void add() {
if (isfull()) {
System.out.println("数据已经满了...");
} else {
System.out.println("输入你要存入的数据..");
int num = new Scanner(System.in).nextInt();
array[end] = num;
end = (end + 1) % maxsize;
}
}
public void show() {
if (isempty()) {
System.out.println("列表为空...");
} else {
for (int i = start; i < start + size(); i++) {
System.out.printf("array[%d]=%d\n", i % maxsize, array[i % maxsize]);
}
}
}
public void get() {
if (isempty()) {
System.out.println("你的列表为空,无法取出...");
} else {
int n = array[start];
start = (start + 1) % maxsize;
System.out.println("取出的数是:" + n);
}
}
public int size() {
return (end + maxsize - start) % maxsize;
}
}
单链表
package 单链表;
import java.util.Stack;
public class 单链表的总结 {
public static void main(String[] args) {
AAA a0=new AAA(1,"周浩","帅气");
AAA a1=new AAA(3,"周浩","帅气");
AAA a2=new AAA(2,"周浩","帅气");
AAA a3=new AAA(5,"周浩","帅气");
AAA a4=new AAA(2,"周浩","帅气");
AAA a5=new AAA(2,"周浩","帅气");
AAA a6=new AAA(2,"小浩纸~","ku");
AAA a7=new AAA(10,"小浩纸~","ku");
dome dome = new dome();
System.out.println("------------直接插入的链表的最后--------------");
System.out.println("------------按顺序插入--------------");
dome.add2(a0);
dome.add2(a1);
dome.add2(a2);
dome.add2(a3);
dome.add2(a4);
dome.add2(a5);
dome.show();
System.out.println("------------根据no修改后的数据------------");
dome.update(a6);
dome.update(a7);
dome.show();
System.out.println("------------根据no删除链表数据------------");
dome.delete(5);
dome.delete(1);
dome.add2(a3);
dome.add2(a7);
dome.show();
System.out.println("链接节点的个数共有:"+dome.count());
System.out.println("显示倒数第 1 个链表-----");
dome.after(dome.count()-1);
System.out.println("显示倒数第 2 个链表-----");
dome.after(dome.count()-2);
System.out.println("----------链表的反转---------");
dome.show();
dome.revirs();
System.out.println("------------");
dome.show();
System.out.println("------------利用栈 来逆序打印 且不破会链表的结构");
dome.nixu();
}
}
class AAA{
public int no;
private String name;
private String nickname;
public AAA node;
public AAA(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "aa{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
class dome{
private AAA head=new AAA(0,"","");
public void add1(AAA newnode){
AAA temp=head;
while(true){
if (temp.node==null){
break;
}
temp=temp.node;
}
temp.node=newnode;
}
public void add2(AAA newnode){
AAA temp=head;
while(true){
if (temp.node==null){
temp.node=newnode;
break;
}else if (temp.node.no> newnode.no){
newnode.node=temp.node;
temp.node=newnode;
break;
}else if (temp.node.no==newnode.no){
System.out.println("编号重复了...."+temp.node.no);
return;
}
temp=temp.node;
}
}
public void show(){
AAA temp=head;
while(true){
if (temp.node==null){
break;
}
temp=temp.node;
System.out.println(temp);
}
}
public void update(AAA newnode){
AAA temp=head;
while(true){
if (temp.node==null){
System.out.println("没有找到要修改的数据...");
return;
}else if (temp.node.no== newnode.no){
newnode.node=temp.node.node;
temp.node=newnode;
return;
}
temp=temp.node;
}
}
public void delete(int num){
AAA temp=head;
while(true){
if (temp.node==null){
System.out.println("没有你要删除的数据");
return;
}else if (temp.node.no==num){
temp.node=temp.node.node;
return;
}
temp=temp.node;
}
}
public int count(){
AAA temp=head;
int num=0;
while(true){
if (temp.node==null){
break;
}
num++;
temp=temp.node;
}
return num;
}
public void after(int k){
AAA temp=head;
int i = 0;
for (; i <= k; i++) {
if (temp.node==null){
System.out.println("没有这个数据...");
return;
}
temp=temp.node;
}
if (i==0){
System.out.println("没有这个数据...");
}else{
System.out.println(temp);
}
}
public void revirs() {
AAA temp=head.node;
AAA flag=null;
AAA tag = new AAA(0, "", "");
while(true){
if (temp==null){
break;
}
flag=temp.node;
temp.node=tag.node;
tag.node=temp;
temp=flag;
}
head.node=tag.node;
}
public void nixu(){
AAA temp=head.node;
Stack<AAA> stack=new Stack<>();
while(true){
if (temp==null){
break;
}
stack.push(temp);
temp=temp.node;
}
while(stack.size()>0){
System.out.println(stack.pop());
}
}
}
双链表
约瑟夫环
栈,栈实现计算器
前缀表达式
中缀表达式
后缀表达式
逆波兰计算器的实现
递归
迷宫回溯
八皇后
排序算法基础
冒泡排序
选择排序
插入排序
希尔排序
快速排序
归并排序
基数排序
各种排序的比较
二叉排序树
删除一颗树的节点
二叉平衡树
图
图的深度优先和广度优先
动态规划
暴力匹配和KMP算法
贪心算法