数据结构是算法的基础
数据结构是一门研究组织数据的方式
程序 = 数据结构+算法
数据结构是数据算法的基础
线性结构和非线性结构
线性结构:有两种不同存储方式:顺序存储结构(成为顺序表),链式存储结构(链表)
特点:数据元素之间存在一对一的线性关系
顺序存储结构是连续的(数组),链式存储结构不一定是连续的(如链表)
常见结构:数组,队列,链表,栈
非线性结构:二维数组,多维数组,广义表,树结构,图结构
稀疏数组的应用场景
当一个数组中大部分元素为0,或者为同一个值得数组时,可以使用稀疏数组来保存数组
稀疏数组第一行记录原始数组的总的行列 和 非零数的个数 后面记录非零数的行 列 和值
实例:
过程:
代码实现
public class 稀疏数组 {
public static void main(String[] args) {
int chessArri[][] = new int[11][11];
chessArri[1][2] = 1;
chessArri[2][3]= 2;
// 输出原始的二维数组
System.out.println("原始的二维数组");
for(int[] row:chessArri){
for (int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
// 得到原始数组中的非零的个数
int sum =0;
for (int i = 0 ;i<11;i++){
for (int j = 0 ; j<11;j++){
if (chessArri[i][j]!= 0){
sum++;
}
}
}
System.out.printf("%d",sum);
// 创建稀疏数组
int sparseArri[][] = new int [sum+1][3];
sparseArri[0][0]=11;
sparseArri[0][1]=11;
sparseArri[0][2]=sum;
// 给稀疏数组赋值
// 设置计数器count来计算非零数的个数
int count = 0;
for (int i = 0 ;i<11;i++){
for (int j = 0 ; j<11;j++){
if (chessArri[i][j]!= 0){
count++;
sparseArri[count][0] = i;//记录第count个非零数的横坐标
sparseArri[count][1] = j;//记录第count个非零数的纵坐标
sparseArri[count][2] = chessArri[i][j];//记录非零数的值
}
}
}
System.out.println();//换行
System.out.println("得到的稀疏数组为:");
for (int n= 0 ;n <sparseArri.length;n++){
System.out.printf(" %d %d %d\n ", sparseArri[n][0], sparseArri[n][1], sparseArri[n][2]);
}
System.out.println();
// 将稀疏数组转化为原始数组
int chessArr2[][]= new int[sparseArri[0][0]][sparseArri[0][1]];
// 读取稀疏数组时必须从稀疏数组第二行开始读
for (int i = 1;i<=sparseArri[0][2];i++) {
chessArr2[sparseArri[i][0]][sparseArri[i][1]] = sparseArri[i][2];
}
//输出原始数组
System.out.printf("得到的原始数组为:");
System.out.println();
for(int[] row:chessArr2){
for (int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}
队列
队列是一个有序列表,可以用 数组(顺序存储)也可以用链表(链式存储)
遵循先入先出的原则
数组实现队列
·maxsize是容量,数组是从零开始
代码实现:
import java.util.Scanner;
public class queue {
public static void main(String[] args) {
// 实验
Arrayqueue arrayQueue = new Arrayqueue(3);
Scanner scanner = new Scanner(System.in);
char key = ' ';//接受用户输入
//构造菜单
boolean loop = true;
while (loop){
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列中取出数据");
System.out.println("h(head):查看头数据");
key = scanner.next().charAt(0);//只接受一个字符
switch (key){
case 's':arrayQueue.showQueue();
break;
case 'a':
System.out.println("请输入一个数");
int value = scanner.nextInt();
arrayQueue.addQueue(value);
break;
case 'g'://取出数据 取出数据时会遇到异常 队列为空 所有用try catch 为了不让程序直接结束;
try{
int res = arrayQueue.getQueue();
System.out.printf("取出的数据是%d",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try{
int res = arrayQueue.headQueue();
System.out.printf("头数据为:%d",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
case 'e':scanner.close();
loop = false;//这样程序就会终止
break;
default:break;
}
}
System.out.println("程序已退出--");
}
}
// 编写一个数组模拟队列
class Arrayqueue {
private int maxSize;//表示数组的最大值
private int rear ;// 队列尾
private int front ;//队列头
private int[] arr;//用于存放数据模拟队列
// 创建队列的构造器
public Arrayqueue(int arrMaxSize){
maxSize = arrMaxSize;
rear = -1;
front = -1;
arr = new int[maxSize];
}
//判断是否满了
public boolean isFull(){
return maxSize-1 == rear;
}
//判断是否为空
public boolean isEmpty(){
return rear == front;
}
//添加数据到队列
public void addQueue(int n ) {
//判断是否为满
if (isFull()) {
System.out.println("队列已满");
}
rear++;
arr[rear] = n;
}
// 获取队列头一个数据 出队列
public int getQueue(){
if (isEmpty()){
throw new RuntimeException("队列为空,不能取数据");
}
front++;
return arr[front];
}
//显示 所有的数据
public void showQueue(){
if (isEmpty()){
System.out.println("没有数据,队列为空");
return;
}
for (int i =0;i<arr.length;i++){
System.out.printf("arr[%d] = %d \n",i,arr[i]);
}
}
public int headQueue(){
//显示队列的头数据
if (isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}
return arr[front+1];
}
}
问题分析及优化
目前数组使用一次之后就不能用了 (当加满后 maxsize-1 == rear rear的值就永远改变不了)
解决方法:环形队列,取模
使用数组实现数组环形队列
import java.util.Scanner;
public class loopqueue {
public static void main(String[] args) {
// 实验
loopArrayqueue arrayQueue = new loopArrayqueue(4);//最大有效数列为3
Scanner scanner = new Scanner(System.in);
char key = ' ';//接受用户输入
//构造菜单
boolean loop = true;
while (loop){
System.out.println("s(show):显示队列");
System.out.println("e(exit):退出程序");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):从队列中取出数据");
System.out.println("h(head):查看头数据");
key = scanner.next().charAt(0);//只接受一个字符
switch (key){
case 's':arrayQueue.showData();
break;
case 'a':
System.out.println("请输入一个数");
int value = scanner.nextInt();
arrayQueue.addData(value);
break;
case 'g'://取出数据 取出数据时会遇到异常 队列为空 所有用try catch 为了不让程序直接结束;
try{
int res = arrayQueue.getData();
System.out.printf("取出的数据是%d",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'h':
try{
int res = arrayQueue.headData();
System.out.printf("头数据为:%d",res);
}catch (Exception e){
System.out.println(e.getMessage());
}
break;
case 'e':scanner.close();
loop = false;//这样程序就会终止
break;
default:break;
}
}
System.out.println("程序已退出--");
}
}
//创建环形队列
class loopArrayqueue{
private int maxSize;//表示数组的最大值
private int rear ;// 队列尾
private int front ;//队列头
private int[] arr;//用于存放数据模拟队列
public loopArrayqueue(int arrMaxSize){
maxSize = arrMaxSize;
rear = 0;
front = 0;
arr = new int[maxSize];
}
public boolean isFull(){
return (rear+1)%maxSize == front;
}
public boolean isEmpty(){
return rear == front;
}
// 添加数据
public void addData(int n){
if(isFull()){
System.out.println("队列已满");
}
arr[rear] = n;
rear = (rear+1)%maxSize;//必须%maxsize因为rear的值最大为maxsize-1就回到0
}
//出数据
public int getData(){
if (isEmpty()){
throw new RuntimeException("队列为空,不能取数据");
}
int value = arr[front];
front =(front+1)%maxSize;
return value;
}
//输出所有数据
public void showData(){
if (isFull()){
System.out.println("队列为空");
}
for (int i =front;i<front+(maxSize-front+rear)%maxSize;i++){
System.out.printf("arr[%d]==%d\n",i%maxSize,arr[i%maxSize]);
}
}
//显示头元素
public int headData(){
if (isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}
return arr[front];
}
}
四个注意点
:·环形队列的有效值为maxsize-1
·判断队列满的方法:(rear+1) %maxsize == front; rear始终为空 是判断是否为满所做的约定 队头和队尾之间一直有一个空位
·判端队列为空:rear==front;
·再添加数据的函数中(addData())rear始终指向的是一个空位 rear = (rear+1)%maxsize rear 不能取maxsize,如果取maxsize就超额了
·在出数据时getData()中,front始终表示队列第一位,但是front值可以一直增加 front =(front+1)%maxsize
链表 链表的创建和增加数据
代码实现 不按顺序 形成单链表
public class linkedlist {
public static void main(String[] args) {
heroNode hero1 = new heroNode(1,"宋江","及时雨");
heroNode hero2 = new heroNode(2,"卢俊义","玉麒麟");
heroNode hero3 = new heroNode(3,"吴用","智多星");
heroNode hero4= new heroNode(4,"林冲","豹子头");
//创建单链表
SinglelinkedListDemo single = new SinglelinkedListDemo();
// single.add(hero1);
// single.add(hero2);
// single.add(hero3);
// single.add(hero4);
single.list();
}
}
//定义一个单链表管理heroNode
class SinglelinkedListDemo{
private heroNode headNode = new heroNode(0,"","");
//添加数据到单链
public void add(heroNode n ){
heroNode temp = headNode;
//遍历链表到最后
while (true) {
if(temp.next==null){
break;
}
temp = temp.next;//如果不是最后一个temp后移
}
//将新英雄添加到最后
temp.next = n;
}
//显示列表
public void list(){
if (headNode.next==null){
System.out.println("链表为空");
return;
}
//因为头节点不能动所以只能 设辅助变量
heroNode temp = headNode.next;//吧headNode排除在外
while (true){
//判断temp是不是到了最后
if (temp==null){
break;
}
System.out.println(temp);//因为已经的重写了toString()所以直接写temp
temp=temp.next;
}
}
}
//定义一个heroNode 每一个heronode对象都是一个节点
class heroNode{
public int no;
public String name;
public String nickName;
public heroNode next; //用heroNode类创建next
//构造器
public heroNode(int no,String name,String nickName){
this.no =no;
this.name = name;
this.nickName = nickName;
}
//为了显示方便重写TOstring();
@Override
public String toString() {
return "heroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
按顺序排名插入节点 实现单链表
思路
注意::!!!!在addByOrder()函数中 newNode.next =temp.next; temp.next= newNode; 的顺序不能变
代码实现
public class linkedlist {
public static void main(String[] args) {
heroNode hero1 = new heroNode(1,"宋江","及时雨");
heroNode hero2 = new heroNode(2,"卢俊义","玉麒麟");
heroNode hero3 = new heroNode(3,"吴用","智多星");
heroNode hero4= new heroNode(4,"林冲","豹子头");
//创建单链表
SinglelinkedListDemo single = new SinglelinkedListDemo();
single.addByOrder(hero1);
single.addByOrder(hero2);
single.addByOrder(hero3);
single.addByOrder(hero4);
single.list();
}
}
//定义一个单链表管理heroNode
class SinglelinkedListDemo{
private heroNode headNode = new heroNode(0,"","");
//添加数据到单链
public void add(heroNode n ){
heroNode temp = headNode;
//遍历链表到最后
while (true) {
if(temp.next==null){
break;
}
temp = temp.next;//如果不是最后一个temp后移
}
//将新英雄添加到最后
temp.next = n;
}
//有顺序的添加数据集
public void addByOrder(heroNode newNode){
//创建辅助节点,因为头结点不能移动
heroNode temp=headNode;
//设置flag判断是否已经存在该数
boolean flag = false;
//遍历单链比较大小
while (true){
//判断单链是否到了最后
if (temp.next == null){
break;
}else if (temp.next.no == newNode.no){
flag = true;
break;
}else if (temp.next.no>newNode.no){
break;
}
temp = temp.next;
}
if (flag){
System.out.printf("英雄编号%d已经存在",newNode.no);
}
newNode.next =temp.next;
temp.next= newNode;
}
//显示列表
public void list(){
if (headNode.next==null){
System.out.println("链表为空");
return ;
}
//因为头节点不能动所以只能 设辅助变量
heroNode temp = headNode.next;//吧headNode排除在外
while (true){
//判断temp是不是到了最后
if (temp==null){
break;
}
System.out.println(temp);//因为已经的重写了toString()所以直接写temp
temp=temp.next;
}
}
}
//定义一个heroNode 每一个heronode对象都是一个节点
class heroNode{
public int no;
public String name;
public String nickName;
public heroNode next; //用heroNode类创建next
//构造器
public heroNode(int no,String name,String nickName){
this.no =no;
this.name = name;
this.nickName = nickName;
}
//为了显示方便重写TOstring();
@Override
public String toString() {
return "heroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
单链表节点的修改
··代码实现··
原理:通过辅助节点temp遍历链表,在链表不为空的情况下,找到和要找节点相同的no值得节点 对其进行修改
public void update(heroNode newData){
heroNode temp = headNode;
if (headNode == null){
System.out.println("链表为空");
}
//如果不为空就要找到需要修改的节点
// 定义一个flag来判断是否存在那个点
boolean flag = false;
while (true){
if (temp == null){
//当到链表最后节点的下一个时还没有就退出
break;
}else if(temp.no==newData.no){
flag =true;
break;
}temp = temp.next;
}
//根据flag表示是否找到了该数据
if (flag){
temp.nickName = newData.nickName;
temp.name = newData.name;
}else {
System.out.println("没有找到这个点");
}
}
单链表节点的删除
思路:找到待删除节点的前一个节点 temp.next =temp.next.next
代码实现
class SinglelinkedListDemo{
private heroNode headNode = new heroNode(0,"","");
//添加数据到单链
public void add(heroNode n ){
heroNode temp = headNode;
//遍历链表到最后
while (true) {
if(temp.next==null){
break;
}
temp = temp.next;//如果不是最后一个temp后移
}
//将新英雄添加到最后
temp.next = n;
}
//有顺序的添加数据集
public void addByOrder(heroNode newNode){
//创建辅助节点,因为头结点不能移动
heroNode temp=headNode;
//设置flag判断是否已经存在该数
boolean flag = false;
//遍历单链比较大小
while (true){
//判断单链是否到了最后
if (temp.next == null){
break;
}else if (temp.next.no == newNode.no){
flag = true;
break;
}else if (temp.next.no>newNode.no){
break;
}
temp = temp.next;
}
if (flag){
System.out.printf("英雄编号%d已经存在",newNode.no);
}
newNode.next =temp.next;
temp.next= newNode;
}
//对单链节点进行修改
public void update(heroNode newData){
heroNode temp = headNode;
if (headNode == null){
System.out.println("链表为空");
}
//如果不为空就要找到需要修改的节点
// 定义一个flag来判断是否存在那个点
boolean flag = false;
while (true){
if (temp == null){
//当到链表最后节点的下一个时还没有就退出
break;
}else if(temp.no==newData.no){
flag =true;
break;
}temp = temp.next;
}
//根据flag表示是否找到了该数据
if (flag){
temp.nickName = newData.nickName;
temp.name = newData.name;
}else {
System.out.println("没有找到这个点");
}
}
//对单链表节点进行删除
public void delete(heroNode newData){
if (headNode == null){
System.out.println("单链为空,没有数据");
}
heroNode temp = headNode;
boolean flag = false;
while (true){
if (temp.next ==null){
//判断是否到了最后一位
// if (temp.no==newData.no){
// flag=true;
// break;
// }
break;
}else if (temp.next.no==newData.no){//找到 目标位置的上一位
flag = true;
break;
}
temp = temp.next;
}
temp.next = temp.next.next;
}
//显示列表
public void list(){
if (headNode.next==null){
System.out.println("链表为空");
return ;
}
//因为头节点不能动所以只能 设辅助变量
heroNode temp = headNode.next;//吧headNode排除在外
while (true){
//判断temp是不是到了最后
if (temp==null){
break;
}
System.out.println(temp);//因为已经的重写了toString()所以直接写temp
temp=temp.next;
}
}
}
新浪面试题单链表的反转
思路:
//倒叙排列某个单链表
//利用头插入法 ,先将原单链表遍历,在新建一个头结点,将原单链表的最后一位插入 头结点的后面
public static void reverseIndexList(heroNode head){
heroNode newHead = new heroNode(0,"","");
heroNode temp = head.next;
int length = getLength(head);
if (length==0){
return;
}
while(temp!=null){
heroNode next = temp.next;//将temp.next先保存起来后面要用
temp.next = newHead.next;//temp指向第一项
newHead.next = temp;//temp作为第一向
temp = next;//将temp后移
}
//head.next 指向 newHead.next实现单链表的反转
head.next = newHead.next;//此时HEAD作为新单链表的头箭头 与原来的单链表断开了
}
}
百度面试题 单链表倒叙打印
方法一:先将单链表倒叙在打印,这样破坏了原来的结构
方法二:运用栈
栈的应用:先进后出
public static void reversePrint(heroNode head){
if (head.next == null){
return;
}
//创建一个栈将各个节点压入栈
Stack<heroNode> stack = new Stack<heroNode>();
heroNode cur = head.next;
while (cur!=null){
stack.add(cur);
cur=cur.next;
}
//打印
while (stack.size()>0){
System.out.println(stack.pop());
}
}
}