•         **头哥数据结构和算法***
    
  • 一、Java数据结构-循环链表的设计与实现
  •   第一关  单循环链表的实现—链表的添加、遍历
    
  • package step1;
    /**
  • Created by sykus on 2018/1/15.
    /
    public class MyCircleLinkedList {
    private Node head;//头结点, 不存数据
    private Node tail;//尾结点, 指向链表的最后一个节点
    private int size;
    public MyCircleLinkedList() {
    head = new Node(Integer.MIN_VALUE, null);
    head.next = head;
    tail = head;
    size = 0;
    }
    /
    *

    • 添加到链表尾部
    • @param item
      /
      public void add(int item) {
      /
      ********* Begin /
      Node node = new Node(item, tail.next);
      tail.next = node;
      tail = node;
      ++size;
      /
      * End *******/
      }
      /
    • 遍历链表并输出元素
      /
      public void output() {
      /
      ********* Begin /
      Node p = head;
      while (p.next != head) {
      p = p.next;
      System.out.println(p.item);
      }
      /
      * End *********/
      }
      public boolean isEmpty() {
      return head.next == head;
      }
      public int size() {
      return size;
      }
      //结点内部类
      private static class Node {
      int item;
      Node next;
      Node(int item, Node next) {
      this.item = item;
      this.next = next;
      }
      }
      }

第二关 单循环链表的实现—链表的删除
package step2;
/**

  • Created by sykus on 2018/1/15.
    /
    public class MyCircleLinkedList {
    private Node head;//头结点, 不存数据
    private Node tail;//尾结点, 指向链表的最后一个节点
    private int size;
    public MyCircleLinkedList() {
    head = new Node(Integer.MIN_VALUE, null);
    head.next = head;
    tail = head;
    size = 0;
    }
    /
    *

    • 添加到链表尾部
    • @param item
      /
      public void add(int item) {
      Node node = new Node(item, tail.next);
      tail.next = node;
      tail = node;
      ++size;
      }
      /
      *
    • 遍历链表并输出元素
      /
      public void output() {
      Node p = head;
      while (p.next != head) {
      p = p.next;
      System.out.println(p.item);
      }
      }
      /
      *
    • 删除从头结点开始的第index个结点
    • index从0开始
    • @param index
    • @return
      /
      public int remove(int index) {
      checkPosIndex(index);
      /
      ********* Begin /
      Node f = head;
      while ((index–) > 0) {
      f = f.next;
      }
      Node del = f.next;
      if (del == tail) {//要删除的是尾结点
      tail = f;//使tail依然指向末尾结点
      }
      f.next = del.next;
      del.next = null;
      int oldVal = del.item;
      del = null;
      –size;
      return oldVal;
      /
      * End *******/
      }
      public boolean isEmpty() {
      return head.next == head;
      }
      public int size() {
      return size;
      }
      private void checkPosIndex(int index) {
      if (index < 0 || index >= size) {
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
      }
      }
      //结点内部类
      private static class Node {
      int item;
      Node next;
      Node(int item, Node next) {
      this.item = item;
      this.next = next;
      }
      }
      }
      第三关 双向循环链表的实现—链表的插入
      package step3;
      /
  • Created by sykus on 2018/1/15.
    /
    public class MyDoubleLinkedList {
    private Node head;//头结点
    private Node tail;//指向链表的尾结点
    private int size;
    public MyDoubleLinkedList() {
    head = new Node(null, Integer.MIN_VALUE, null);
    head.next = head.prev = head;
    tail = head;
    size = 0;
    }
    /
    *

    • 添加元素到表尾
    • @param item
      /
      public void add(int item) {
      /
      ********* Begin /
      Node newNode = new Node(null, item, null);
      tail.next = newNode;
      newNode.prev = tail;
      newNode.next = head;
      head.prev = newNode;
      tail = newNode;
      ++size;
      /
      * End *******/
      }
      /
    • 打印双向链表
    • @param flag true从左向右顺序打印, false从右向左顺序打印
      */
      public void printList(boolean flag) {
      Node f = head;
      if (flag) {//向右
      while (f.next != head) {
      f = f.next;
      System.out.print(f.item + " ");
      }
      } else {//向左
      while (f.prev != head) {
      f = f.prev;
      System.out.print(f.item + " ");
      }
      }
      }
      public int size() {
      return size;
      }
      //结点内部类
      private static class Node {
      int item;
      Node next;//直接后继引用
      Node prev;//直接前驱引用
      Node(Node prev, int item, Node next) {
      this.prev = prev;
      this.item = item;
      this.next = next;
      }
      }
      }

第4关:双向循环链表的实现—链表的删除
package step4;
/**

  • Created by sykus on 2018/1/15.
    /
    public class MyDoubleLinkedList {
    private Node head;//头结点
    private Node tail;//指向链表的尾结点
    private int size;
    public MyDoubleLinkedList() {
    head = new Node(null, Integer.MIN_VALUE, null);
    head.next = head.prev = head;
    tail = head;
    size = 0;
    }
    /
    *

    • 添加元素到表尾
    • @param item
      /
      public void add(int item) {
      Node newNode = new Node(null, item, null);
      tail.next = newNode;
      newNode.prev = tail;
      newNode.next = head;
      head.prev = newNode;
      tail = newNode;
      ++size;
      }
      /
      *
    • 删除指定位置index出的结点,并返回其值
    • @param index
    • @return
      /
      public int remove(int index) {
      checkPosIndex(index);//
      /
      ********* Begin /
      Node p = head.next;
      while ((index–) > 0) {
      p = p.next;
      }
      if (p == tail) {
      tail = p.prev;
      }
      p.prev.next = p.next;
      p.next.prev = p.prev;
      int val = p.item;
      p = null;
      –size;
      return val;
      /
      * End *******/
      }
      /
    • 打印双向链表
    • @param flag true从左向右顺序打印, false从右向左顺序打印
      */
      public void printList(boolean flag) {
      Node f = head;
      if (flag) {//向右
      while (f.next != head) {
      f = f.next;
      System.out.print(f.item + " ");
      }
      } else {//向左
      while (f.prev != head) {
      f = f.prev;
      System.out.print(f.item + " ");
      }
      }
      }
      public int size() {
      return size;
      }
      private void checkPosIndex(int index) {
      if (index < 0 || index >= size) {
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
      }
      }
      //结点内部类
      private static class Node {
      int item;
      Node next;//直接后继引用
      Node prev;//直接前驱引用
      Node(Node prev, int item, Node next) {
      this.prev = prev;
      this.item = item;
      this.next = next;
      }
      }
      }

头哥数据结构和算法答案相关推荐

  1. 头歌 数据结构与算法答案 善用目录

    头歌 数据结构与算法答案 其他作业链接 非盈利文章,谢谢大家的分享和支持,如果大家有想要投稿的答案,也可以点击下面链接联系作者. 点击联系作者 作者博客 选择题加粗为正确答案 头歌java实训答案集 ...

  2. 【educoder】头歌 数据结构与算法 答案

  3. 数据结构及算法答案(电子科技大学MOOC)

    第一章 绪论 1单选(10分)下面()术语与数据的存储结构无关 A.顺序表 B.链表 C.队列 D.顺序队列 答案:C 2单选(10分)算法分析的目的是() A.找出数据结构的合理性 B.研究算法的输 ...

  4. 头哥Numpy初体验答案

    第一关: 任务描述 相关知识 编程要求 测试说明 任务描述 本关的小目标是,使用 Numpy 创建一个多维数组. 相关知识 在 Python 中创建数组有许多的方法,这里我们使用 Numpy 中的ar ...

  5. 头歌-数据结构与算法-字符串匹配

    第1关:实现朴素的字符串匹配 #include <stdio.h> #include <stdlib.h> #include "mystr.h" #prag ...

  6. 头歌-数据结构与算法 - 线性表

    第1关:实现一个顺序存储的线性表 #include <stdio.h> #include <stdlib.h> #include "Seqlist.h" S ...

  7. C语言单链表实现FCFS算法,数据结构与算法复习题(含答案).doc

    <数据结构与算法>2015-2016学年第1学期考试复习题 选择题(下面各小题有一个正确答案,请将正确答案的编号填写在各小题的括号内). 1.在一棵具有5层的满二叉树中结点总数为( A ) ...

  8. 《数据结构与算法》第二版-陈卫卫-陆军工程大学811数据结构教材 第1-2章 参考答案

    <数据结构与算法>(第二版)陈卫卫-高等教育出版社     陆军工程大学811数据结构教材    第1-2章 参考答案 习题1.1 1.1-1      (1)名称.数量.特征.性质的   ...

  9. 数据结构与算法精选面试50题(附答案)

    1.数组编码面试问题 数组是最基本的数据结构,它将元素存储在一个连续的内存位置.这也是面试官们热衷的话题之一,在任何一次编程面试中,你都会听到很多关于数组的问题,比如将数组中元素位置颠倒,对数组进行排 ...

  10. mooc数据结构与算法python版期末考试_数据结构与算法Python版-中国大学mooc-试题题目及答案...

    数据结构与算法Python版-中国大学mooc-试题题目及答案 更多相关问题 婴儿出生一两天后就有笑的反应,这种笑的反应属于(). [判断题]填制原始凭证,汉字大写金额数字一律用正楷或草书书写,汉字大 ...

最新文章

  1. 【冷门实用小工具】轻量级流程图工具ClickCharts PRO绿色版,ClickCharts PRO下载【亲测有效】
  2. MySQL设计一个图书馆数据库_请设计一个图书馆数据库
  3. b站前端大佬_最强UP主:罗翔老师,你凭什么打败B站千万粉大佬老番茄?
  4. boost::python模块包装几个 C++ 函数 将二维数组操作为采用 NumPy 数组的 Python 函数作为参数
  5. CDQ 分治解决和点对有关的问题
  6. [gtest][001] A quick introduction to the Google C++ Testing Framework
  7. 前端市场现在是不是饱和了?
  8. Reflector for .NET 下载问题
  9. html颜色自定义器,可自定义颜色的jQuery颜色拾取器插件
  10. linux文件系统知识总结、SD卡挂载问题总结
  11. JAVA核心:I/O(输入/输出)
  12. Intellij IDEA导入外部项目javax.servlet.http不存在
  13. 来了老弟,表格的渲染
  14. 磁盘数据页的存储结构
  15. 『小众摄影』01 期 :网红重庆•千年古镇磁器口
  16. Cypress之管理配置信息
  17. c++实现二分查找法和求平方根
  18. XMLHttpRequest cannot load的解决方法
  19. BlockChange | 对话路印协议创始人王东:从谷歌工程师到区块链创业者,一个运营良好的区块链社区如何建设?
  20. Python爬虫之正则 BeautifulSoup4解析HTML

热门文章

  1. 蒙特卡罗方法C语言求定积分,蒙特卡罗方法计算定积分
  2. JQuery 学习总结及实例 !! (转载)
  3. 林期苏曼属性标签编辑_标签制作软件如何制作椭圆形药品标签
  4. 【原创】LabView制作实时读取Excel正态分布图
  5. single-precision operand implicitly
  6. “燕云十六将”之三弟王静
  7. 60万餐厅数据为你画出全国美食地图(附技术讲解)
  8. html5制作旋转正方体,html5—旋转立方体
  9. IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
  10. 使用outlook及office assitans实现邮件批量发送