数据结构--day2

简介: 数据结构--day2

数组模拟队列

f3e0dbda59ea3c79cbb67e3d354b5f2b_1f339ab04e674762bf8a923cb156f1d7.png

思路分析

cd79d5149f9eaa4a78bfd6d7e49070b4_fcf9d66de8f44fb9915b020209667848.png

代码实现

package com.atguigu.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
  public static void main(String[] args) {
    //测试一把
    System.out.println("测试数组模拟环形队列的案例~~~");
    // 创建一个环形队列
    CircleArray queue = new CircleArray(4); //说明设置4, 其队列的有效数据最大是3
    char key = ' '; // 接收用户输入
    Scanner scanner = new Scanner(System.in);//
    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':
        queue.showQueue();
        break;
      case 'a':
        System.out.println("输出一个数");
        int value = scanner.nextInt();
        queue.addQueue(value);
        break;
      case 'g': // 取出数据
        try {
          int res = queue.getQueue();
          System.out.printf("取出的数据是%d\n", res);
        } catch (Exception e) {
          // TODO: handle exception
          System.out.println(e.getMessage());
        }
        break;
      case 'h': // 查看队列头的数据
        try {
          int res = queue.headQueue();
          System.out.printf("队列头的数据是%d\n", res);
        } catch (Exception e) {
          // TODO: handle exception
          System.out.println(e.getMessage());
        }
        break;
      case 'e': // 退出
        scanner.close();
        loop = false;
        break;
      default:
        break;
      }
    }
    System.out.println("程序退出~~");
  }
}
class CircleArray {
  private int maxSize; // 表示数组的最大容量
  //front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素 
  //front 的初始值 = 0
  private int front; 
  //rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
  //rear 的初始值 = 0
  private int rear; // 队列尾
  private int[] arr; // 该数据用于存放数据, 模拟队列
  public CircleArray(int arrMaxSize) {
    maxSize = arrMaxSize;
    arr = new int[maxSize];
  }
  // 判断队列是否满
  public boolean isFull() {
    return (rear  + 1) % maxSize == front;
  }
  // 判断队列是否为空
  public boolean isEmpty() {
    return rear == front;
  }
  // 添加数据到队列
  public void addQueue(int n) {
    // 判断队列是否满
    if (isFull()) {
      System.out.println("队列满,不能加入数据~");
      return;
    }
    //直接将数据加入
    arr[rear] = n;
    //将 rear 后移, 这里必须考虑取模
    rear = (rear + 1) % maxSize;
  }
  // 获取队列的数据, 出队列
  public int getQueue() {
    // 判断队列是否空
    if (isEmpty()) {
      // 通过抛出异常
      throw new RuntimeException("队列空,不能取数据");
    }
    // 这里需要分析出 front是指向队列的第一个元素
    // 1. 先把 front 对应的值保留到一个临时变量
    // 2. 将 front 后移, 考虑取模
    // 3. 将临时保存的变量返回
    int value = arr[front];
    front = (front + 1) % maxSize;
    return value;
  }
  // 显示队列的所有数据
  public void showQueue() {
    // 遍历
    if (isEmpty()) {
      System.out.println("队列空的,没有数据~~");
      return;
    }
    // 思路:从front开始遍历,遍历多少个元素
    // 动脑筋
    for (int i = front; i < front + size() ; i++) {
      System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
    }
  }
  // 求出当前队列有效数据的个数
  public int size() {
    // rear = 2
    // front = 1
    // maxSize = 3 
    return (rear + maxSize - front) % maxSize;   
  }
  // 显示队列的头数据, 注意不是取出数据
  public int headQueue() {
    // 判断
    if (isEmpty()) {
      throw new RuntimeException("队列空的,没有数据~~");
    }
    return arr[front];
  }
}

存在的问题

4a162877965bb271603a13dd5a9b71cf_73d9fb16538241208dafffc699b3d508.png

思路

0e004a625a51087e271d6c5ef69c814a_2f6e24cf46af47b2aee02742a587019a.png

注意:这里预留的位置不放数据,所以真正放数据的空间为maxSize-1,并且front指向的位置永远都是空值

代码实现

package com.atguigu.queue;
import java.util.Scanner;
public class CircleArrayQueueDemo {
  public static void main(String[] args) {
    //测试一把
    System.out.println("测试数组模拟环形队列的案例~~~");
    // 创建一个环形队列
    CircleArray queue = new CircleArray(4); //说明设置4, 其队列的有效数据最大是3
    char key = ' '; // 接收用户输入
    Scanner scanner = new Scanner(System.in);//
    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':
        queue.showQueue();
        break;
      case 'a':
        System.out.println("输出一个数");
        int value = scanner.nextInt();
        queue.addQueue(value);
        break;
      case 'g': // 取出数据
        try {
          int res = queue.getQueue();
          System.out.printf("取出的数据是%d\n", res);
        } catch (Exception e) {
          // TODO: handle exception
          System.out.println(e.getMessage());
        }
        break;
      case 'h': // 查看队列头的数据
        try {
          int res = queue.headQueue();
          System.out.printf("队列头的数据是%d\n", res);
        } catch (Exception e) {
          // TODO: handle exception
          System.out.println(e.getMessage());
        }
        break;
      case 'e': // 退出
        scanner.close();
        loop = false;
        break;
      default:
        break;
      }
    }
    System.out.println("程序退出~~");
  }
}
class CircleArray {
  private int maxSize; // 表示数组的最大容量
  //front 变量的含义做一个调整: front 就指向队列的第一个元素, 也就是说 arr[front] 就是队列的第一个元素 
  //front 的初始值 = 0
  private int front; 
  //rear 变量的含义做一个调整:rear 指向队列的最后一个元素的后一个位置. 因为希望空出一个空间做为约定.
  //rear 的初始值 = 0
  private int rear; // 队列尾
  private int[] arr; // 该数据用于存放数据, 模拟队列
  public CircleArray(int arrMaxSize) {
    maxSize = arrMaxSize;
    arr = new int[maxSize];
  }
  // 判断队列是否满
  public boolean isFull() {
    return (rear  + 1) % maxSize == front;
  }
  // 判断队列是否为空
  public boolean isEmpty() {
    return rear == front;
  }
  // 添加数据到队列
  public void addQueue(int n) {
    // 判断队列是否满
    if (isFull()) {
      System.out.println("队列满,不能加入数据~");
      return;
    }
    //直接将数据加入
    arr[rear] = n;
    //将 rear 后移, 这里必须考虑取模
    rear = (rear + 1) % maxSize;
  }
  // 获取队列的数据, 出队列
  public int getQueue() {
    // 判断队列是否空
    if (isEmpty()) {
      // 通过抛出异常
      throw new RuntimeException("队列空,不能取数据");
    }
    // 这里需要分析出 front是指向队列的第一个元素
    // 1. 先把 front 对应的值保留到一个临时变量
    // 2. 将 front 后移, 考虑取模
    // 3. 将临时保存的变量返回
    int value = arr[front];
    front = (front + 1) % maxSize;
    return value;
  }
  // 显示队列的所有数据
  public void showQueue() {
    // 遍历
    if (isEmpty()) {
      System.out.println("队列空的,没有数据~~");
      return;
    }
    // 思路:从front开始遍历,遍历多少个元素
    // 动脑筋
    for (int i = front; i < front + size() ; i++) {
      System.out.printf("arr[%d]=%d\n", i % maxSize, arr[i % maxSize]);
    }
  }
  // 求出当前队列有效数据的个数
  public int size() {
    // rear = 2
    // front = 1
    // maxSize = 3 
    return (rear + maxSize - front) % maxSize;   
  }
  // 显示队列的头数据, 注意不是取出数据
  public int headQueue() {
    // 判断
    if (isEmpty()) {
      throw new RuntimeException("队列空的,没有数据~~");
    }
    return arr[front];
  }
}

单链表

9e2d36bfe12fa587e64d99a0661028f1_35942785dc93471cad6fd2be63909282.png

代码实现

package com.atguigu.linkedlist;
import java.util.Stack;
public class SingleLinkedListDemo {
  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, "林冲", "豹子头");
    //创建要给链表
    SingleLinkedList singleLinkedList = new SingleLinkedList();
    //加入
    singleLinkedList.addByOrder(hero1);
    singleLinkedList.addByOrder(hero4);
    singleLinkedList.addByOrder(hero2);
    singleLinkedList.addByOrder(hero3);
    singleLinkedList.list();
//    // 测试一下单链表的反转功能
//    System.out.println("原来链表的情况~~");
//    singleLinkedList.list();
//
    System.out.println("反转单链表~~");
    reversetList(singleLinkedList.getHead());
    singleLinkedList.list();
//
//    System.out.println("测试逆序打印单链表, 没有改变链表的结构~~");
//    reversePrint(singleLinkedList.getHead());
/*    
    //加入按照编号的顺序
    singleLinkedList.addByOrder(hero1);
    singleLinkedList.addByOrder(hero4);
    singleLinkedList.addByOrder(hero2);
    singleLinkedList.addByOrder(hero3);
    //显示一把
    singleLinkedList.list();
    //测试修改节点的代码
    HeroNode newHeroNode = new HeroNode(2, "小卢", "玉麒麟~~");
    singleLinkedList.update(newHeroNode);
    System.out.println("修改后的链表情况~~");
    singleLinkedList.list();
    //删除一个节点
    singleLinkedList.del(1);
    singleLinkedList.del(4);
    System.out.println("删除后的链表情况~~");
    singleLinkedList.list();
    //测试一下 求单链表中有效节点的个数
    System.out.println("有效的节点个数=" + getLength(singleLinkedList.getHead()));//2
    //测试一下看看是否得到了倒数第K个节点
    HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 3);
    System.out.println("res=" + res);
*/    
  }
  //方式2:
  //可以利用栈这个数据结构,将各个节点压入到栈中,然后利用栈的先进后出的特点,就实现了逆序打印的效果
  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.push(cur);
      cur = cur.next; //cur后移,这样就可以压入下一个节点
    }
    //将栈中的节点进行打印,pop 出栈
    while (stack.size() > 0) {
      System.out.println(stack.pop()); //stack的特点是先进后出
    }
  }
  //将单链表反转
  public static void reversetList(HeroNode head) {
    //如果当前链表为空,或者只有一个节点,无需反转,直接返回
    if(head.next == null || head.next.next == null) {
      return ;
    }
    //定义一个辅助的指针(变量),帮助我们遍历原来的链表
    HeroNode cur = head.next;
    HeroNode next = null;// 指向当前节点[cur]的下一个节点
    HeroNode reverseHead = new HeroNode(0, "", "");
    //遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表reverseHead 的最前端
    //动脑筋
    while(cur != null) { 
      next = cur.next;//先暂时保存当前节点的下一个节点,因为后面需要使用
      cur.next = reverseHead.next;//将cur的下一个节点指向新的链表的最前端
      reverseHead.next = cur; //将cur 连接到新的链表上
      cur = next;//让cur后移
    }
    //将head.next 指向 reverseHead.next , 实现单链表的反转
    head.next = reverseHead.next;
  }
  //查找单链表中的倒数第k个结点 【新浪面试题】
  //思路
  //1. 编写一个方法,接收head节点,同时接收一个index 
  //2. index 表示是倒数第index个节点
  //3. 先把链表从头到尾遍历,得到链表的总的长度 getLength
  //4. 得到size 后,我们从链表的第一个开始遍历 (size-index)个,就可以得到
  //5. 如果找到了,则返回该节点,否则返回nulll
  public static HeroNode findLastIndexNode(HeroNode head, int index) {
    //判断如果链表为空,返回null
    if(head.next == null) {
      return null;//没有找到
    }
    //第一个遍历得到链表的长度(节点个数)
    int size = getLength(head);
    //第二次遍历  size-index 位置,就是我们倒数的第K个节点
    //先做一个index的校验
    if(index <=0 || index > size) {
      return null; 
    }
    //定义给辅助变量, for 循环定位到倒数的index
    HeroNode cur = head.next; //3 // 3 - 1 = 2
    for(int i =0; i< size - index; i++) {
      cur = cur.next;
    }
    return cur;
  }
  //方法:获取到单链表的节点的个数(如果是带头结点的链表,需求不统计头节点)
  /**
   * 
   * @param head 链表的头节点
   * @return 返回的就是有效节点的个数
   */
  public static int getLength(HeroNode head) {
    if(head.next == null) { //空链表
      return 0;
    }
    int length = 0;
    //定义一个辅助的变量, 这里我们没有统计头节点
    HeroNode cur = head.next;
    while(cur != null) {
      length++;
      cur = cur.next; //遍历
    }
    return length;
  }
}
//定义SingleLinkedList 管理我们的英雄
class SingleLinkedList {
  //先初始化一个头节点, 头节点不要动, 不存放具体的数据
  private HeroNode head = new HeroNode(0, "", "");
  //返回头节点
  public HeroNode getHead() {
    return head;
  }
  //添加节点到单向链表
  //思路,当不考虑编号顺序时
  //1. 找到当前链表的最后节点
  //2. 将最后这个节点的next 指向 新的节点
  public void add(HeroNode heroNode) {
    //因为head节点不能动,因此我们需要一个辅助遍历 temp
    HeroNode temp = head;
    //遍历链表,找到最后
    while(true) {
      //找到链表的最后
      if(temp.next == null) {//
        break;
      }
      //如果没有找到最后, 将将temp后移
      temp = temp.next;
    }
    //当退出while循环时,temp就指向了链表的最后
    //将最后这个节点的next 指向 新的节点
    temp.next = heroNode;
  }
  //第二种方式在添加英雄时,根据排名将英雄插入到指定位置
  //(如果有这个排名,则添加失败,并给出提示)
  public void addByOrder(HeroNode heroNode) {
    //因为头节点不能动,因此我们仍然通过一个辅助指针(变量)来帮助找到添加的位置
    //因为单链表,因为我们找的temp 是位于 添加位置的前一个节点,否则插入不了
    HeroNode temp = head;
    boolean flag = false; // flag标志添加的编号是否存在,默认为false
    while(true) {
      if(temp.next == null) {//说明temp已经在链表的最后
        break; //
      } 
      if(temp.next.no > heroNode.no) { //位置找到,就在temp的后面插入
        break;
      } else if (temp.next.no == heroNode.no) {//说明希望添加的heroNode的编号已然存在
        flag = true; //说明编号存在
        break;
      }
      temp = temp.next; //后移,遍历当前链表
    }
    //判断flag 的值
    if(flag) { //不能添加,说明编号存在
      System.out.printf("准备插入的英雄的编号 %d 已经存在了, 不能加入\n", heroNode.no);
    } else {
      //插入到链表中, temp的后面
      heroNode.next = temp.next;
      temp.next = heroNode;
    }
  }
  //修改节点的信息, 根据no编号来修改,即no编号不能改.
  //说明
  //1. 根据 newHeroNode 的 no 来修改即可
  public void update(HeroNode newHeroNode) {
    //判断是否空
    if(head.next == null) {
      System.out.println("链表为空~");
      return;
    }
    //找到需要修改的节点, 根据no编号
    //定义一个辅助变量
    HeroNode temp = head.next;
    boolean flag = false; //表示是否找到该节点
    while(true) {
      if (temp == null) {
        break; //已经遍历完链表
      }
      if(temp.no == newHeroNode.no) {
        //找到
        flag = true;
        break;
      }
      temp = temp.next;
    }
    //根据flag 判断是否找到要修改的节点
    if(flag) {
      temp.name = newHeroNode.name;
      temp.nickname = newHeroNode.nickname;
    } else { //没有找到
      System.out.printf("没有找到 编号 %d 的节点,不能修改\n", newHeroNode.no);
    }
  }
  //删除节点
  //思路
  //1. head 不能动,因此我们需要一个temp辅助节点找到待删除节点的前一个节点
  //2. 说明我们在比较时,是temp.next.no 和  需要删除的节点的no比较
  public void del(int no) {
    HeroNode temp = head;
    boolean flag = false; // 标志是否找到待删除节点的
    while(true) {
      if(temp.next == null) { //已经到链表的最后
        break;
      }
      if(temp.next.no == no) {
        //找到的待删除节点的前一个节点temp
        flag = true;
        break;
      }
      temp = temp.next; //temp后移,遍历
    }
    //判断flag
    if(flag) { //找到
      //可以删除
      temp.next = temp.next.next;
    }else {
      System.out.printf("要删除的 %d 节点不存在\n", no);
    }
  }
  //显示链表[遍历]
  public void list() {
    //判断链表是否为空
    if(head.next == null) {
      System.out.println("链表为空");
      return;
    }
    //因为头节点,不能动,因此我们需要一个辅助变量来遍历
    HeroNode temp = head.next;
    while(true) {
      //判断是否到链表最后
      if(temp == null) {
        break;
      }
      //输出节点的信息
      System.out.println(temp);
      //将temp后移, 一定小心
      temp = temp.next;
    }
  }
}
//定义HeroNode , 每个HeroNode 对象就是一个节点
class HeroNode {
  public int no;
  public String name;
  public String nickname;
  public 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 + "]";
  }
}


相关文章
|
5月前
|
存储 C++
c++数据结构
c++数据结构
42 0
|
存储 JavaScript 前端开发
数据结构之List | 让我们一块来学习数据结构
数据结构之List | 让我们一块来学习数据结构
129 0
|
存储 canal 前端开发
数据结构之Queue | 让我们一块来学习数据结构
数据结构之Queue | 让我们一块来学习数据结构
74 0
|
存储 canal 算法
数据结构之Stack | 让我们一块来学习数据结构
数据结构之Stack | 让我们一块来学习数据结构
51 0
|
5月前
|
存储 算法 索引
数据结构每日回顾
数据结构每日回顾
33 1
|
11月前
|
存储 算法
算法学习--数据结构
算法学习--数据结构
|
12月前
|
存储 算法 搜索推荐
【BaseArray 数据结构】
【BaseArray 数据结构】
|
SQL JavaScript 前端开发
数据结构之Set | 让我们一块来学习数据结构
数据结构之Set | 让我们一块来学习数据结构
47 0
数据结构3-什么是数据结构1 原
数据结构3-什么是数据结构1 原
43 0
数据结构3-什么是数据结构1 原