数据结构与算法:链表,队列,栈,递归,有序表
反转单链表,双链表
import java.util.ArrayList;
import java.util.List;public class ReverseList {public static class Node{public int value;public Node next;public Node(int data){value = data;}}public static class DoubleNode{public int value;public DoubleNode last;public DoubleNode next;public DoubleNode(int data){value = data;}}// 利用双指针,反转单链表// head a - > b - > c - > d - > e - > nullpublic static Node reverseLinkedList(Node head){Node pre = null;Node next = null;while(null != head){next = head.next;head.next = pre;pre = head;head = next;}return pre;}// 反转双向链表public static DoubleNode reverseDoubleList(DoubleNode head){DoubleNode pre = null;DoubleNode next = null;while(head != null){next = head.next;head.next = pre;head.last = next;pre = head;head = next;}return pre;}public static Node testReverseLinkedList(Node head){if(null == head){return null;}ArrayList<Node> list = new ArrayList<>();while(head != null){list.add(head);head = head.next;}list.get(0).next = null;int N = list.size();for (int i = 1; i < N; i++) {list.get(i).next = list.get(i-1);}return list.get(N-1);}public static Node generateRandomLinkedList(int len, int value){int size = (int)(Math.random() * (len+1));if(size == 0){return null;}size--;Node head = new Node((int)(Math.random())*(value+1));Node pre = head;while(size != 0){Node cur = new Node((int)(Math.random())*(value+1));pre.next = cur;pre = cur;size--;}return head;}public static List<Integer> getLinkedListOriginOrder(Node head){List<Integer> ans = new ArrayList<>();while(head != null){ans.add(head.value);head = head.next;}return ans;}public static boolean checkLinkedListReverse(List<Integer> origin, Node head){for (int i = origin.size()-1; i >= 0 ; i--) {if(!origin.get(i).equals(head.value)){return false;}head = head.next;}return true;}public static void main(String[] args) {int len = 50;int value = 100;int testTime = 100000;System.out.println("test Begin!!");for (int i = 0; i < testTime; i++) {Node node1 = generateRandomLinkedList(len,value);List<Integer> list1 = getLinkedListOriginOrder(node1);node1 = reverseLinkedList(node1);if(!checkLinkedListReverse(list1,node1)){System.out.println("Oops1!");}}System.out.println("test end!!");}// 删除链表中值等于num的节点public static Node removeValue(Node head, int num){while(null != head){// 来到第一个不需要删的位置if(head.value != num){break;}head = head.next;}Node pre = head;Node cur = head;while(cur != null){if(cur.value == num){pre.next = cur.next;}else{pre = cur;}cur = cur.next;}return head;}
}
队列和栈是逻辑上的概念
数组和链表都能实现队列和栈。
双端链表实现队列
public class DoubleEndsQueueToStackAndQueue {public static class Node<T>{public T value;public Node<T> last;public Node<T> next;public Node(T data){value = data;}}// 双端队列public static class DoubleEndsQueue<T>{public Node<T> head; // 头指针public Node<T> tail; // 尾指针// 从头部加public void addFromHead(T value){Node<T> cur = new Node(value);if(head == null){head = cur;tail = cur;}else{head.last = cur;cur.next = head;head = cur;}}// 从尾部加public void addFromBottom(T value){Node<T> cur = new Node(value);if(tail == null){head = cur;tail = cur;}else{tail.next = cur;cur.last = tail;tail = cur;}}// 从头部弹出来public T popFromHead(){if(head == null){return null;}Node<T> cur = head;if(head == tail){tail = null;head = null;}else{head = head.next;cur.next = null;head.last = null;}return cur.value;}// 从尾部弹出来public T popFromTail(){if(head == null){return null;}Node<T> cur = tail;if(head == tail){tail = null;head = null;}else{tail = tail.last;tail.next = null;cur.last = null;}return cur.value;}}public static void main(String[] args) {}
}
数组实现队列
public class RingArray {public static class MyQueue{private int[] arr;private int pushi;private int polli;private int size; // 已加入到队列中的个数private final int limit; // 队列最多能装多少个public MyQueue(int limit){arr = new int[limit];pushi = 0;polli = 0;size = 0;this.limit = limit;}public void push(int value){if(size == limit){throw new RuntimeException("栈满了,不能再加了");}size++;arr[pushi] = value;pushi = nextIndex(pushi);}public int pop(int value){if(size == 0){throw new RuntimeException("栈空了,不能再拿了");}size--;int cur = arr[polli];polli = nextIndex(polli);return cur;}public boolean isEmpty(){return size == 0 ? true : false;}// 如果现在的下标值是i,返回下一个位置public int nextIndex(int i){// 如果没到底,位置就加1。 如果位置到底,就返回0return i < limit - 1 ? i+1 : 0;}}
}
实现一个特殊的栈,在实现基本功能的基础上,再实现返回栈中最小元素的功能
- pop,push,getMin操作的时间复杂度都是O(1)
- 设计的栈类型可以使用现成的栈结构
题目: 如何用栈结构实现队列结构
import java.util.Stack;public class TwoStacksImplementQueue {public static class TwoStacksQueue{public Stack<Integer> stackPush;public Stack<Integer> stackPop;public TwoStacksQueue(){stackPush = new Stack<Integer>();stackPop = new Stack<Integer>();}// 从push栈往pop栈倒入数据private void pushToPop(){if(stackPop.empty()) { // 只有pop栈不为空while (!stackPush.isEmpty()) {stackPop.push(stackPush.pop());}}}public void add(int pushInt){stackPush.push(pushInt);pushToPop();}public int poll(){if(stackPush.empty() && stackPop.empty()){throw new RuntimeException("Queue is empty");}pushToPop();return stackPop.pop();}public int peek(){if(stackPush.isEmpty() && stackPop.isEmpty()){throw new RuntimeException("Queue is empty");}pushToPop();return stackPop.peek();}}public static void main(String[] args) {}
}
题目: 如何用队列结构实现栈结构
package datastructure.trainingcamp.Code03;import java.util.LinkedList;
import java.util.Queue;public class TwoQueueImplementStack {public static class TwoQueueStack<T>{public Queue<T> queue;public Queue<T> help;public TwoQueueStack(){queue = new LinkedList<>();help = new LinkedList<>();}public void push(T value){queue.offer(value);}public T poll(){while(queue.size() > 1){help.offer(queue.poll());}T ans = queue.poll();Queue<T> tmp = queue;queue = help;help = tmp;return ans;}public T peek(){while(queue.size() > 1){help.offer(queue.poll());}T ans = queue.poll();help.offer(ans);Queue<T> tmp = queue;queue = help;help = tmp;return ans;}}public static void main(String[] args) {}
}
任何递归都可以改成非递归
红黑树,avl,sb树,跳表都能实现有序表。
数据结构与算法:链表,队列,栈,递归,有序表相关推荐
- 数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构
数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构 目录 打印两个有序链表公共部分 判断一个链表是否具有回文结构 1. 打印两个有序链表公共部分 1.问题描述 思路:Node1和N ...
- c语言编写队列元素逆置,数据结构与算法实验—利用栈逆置队列元素.doc
数据结构与算法实验-利用栈逆置队列元素 利用栈逆置队列元素实验报告 通信1204班 谢崇赟 实验名称 利用堆栈将队列中的元素逆置 实验目的 会定义顺序栈和链栈的结点类型. 掌握栈的插入和删除结点在操作 ...
- 数据结构与算法--链表实现以及应用
数据结构与算法–链表实现以及应用 链表是面试时候使用最频繁的一种数据结构.链表的结构简单,他由指针将若干个节点链接成链状结构.链表的创建,插入,删除,查询操作都只有几行代码可以完成,代码量比较少,可以 ...
- 数据结构与算法-链表学习笔记
数据结构与算法-链表学习笔记 链表的概念 链表是有序的列表. 链表是以节点的方式来存储,是链式存储,它在内存中并不是一定是连续的. 每个节点包含 data 域:存储数据, next 域:指向下一个节点 ...
- 数据结构与算法_03队列
数据结构与算法_03队列 队列 0.章节重点整理 1.认识队列 1.1. 队列的工作运算 1.2.队列的数组实现 1.3.队列的链表实现 2.队列的应用 2.1.环形队列 2.2.双向队列 队列 0. ...
- php学数据结构,PHP 程序员学数据结构与算法之《栈》
介绍 "要成高手,必练此功". 要成为优秀的程序员,数据结构和算法是必修的内容.而现在的Web程序员使用传统算法和数据结构都比较少,因为很多算法都是包装好的,不用我们去操心具体的实 ...
- JavaScript数据结构与算法——链表详解(下)
在JavaScript数据结构与算法--链表详解(上)中,我们探讨了一下链表的定义.实现原理以及单链表的实现.接下来我们进一步了解一下链表的其他内容. 1.双向链表 双向链表实现原理图: 与单向链表不 ...
- JavaScript数据结构与算法——链表详解(上)
注:与之前JavaScript数据结构与算法系列博客不同的是,从这篇开始,此系列博客采用es6语法编写,这样在学数据结构的同时还能对ECMAScript6有进一步的认识,如需先了解es6语法请浏览ht ...
- C语言中用链表构建栈讲解,C语言数据结构之使用链表模拟栈的实例
C语言数据结构之使用链表模拟栈的实例 以下是"使用链表模拟栈"的简单示例: 1. 用C语言实现的版本 #include #include typedef char datatype ...
- php数据结构之线性表,php数据结构之顺序链表与链式线性表示例
本文实例讲述了php数据结构之顺序链表与链式线性表.分享给大家供大家参考,具体如下: 链表操作 1. InitList(L):初始化链表 2. DestroyList(L):删除连接 3. Clear ...
最新文章
- “百度智能云”下,群星璀璨,照亮百度世界2020
- Spring4:具有Java 8 Date-Time API的@DateTimeFormat
- 在传统Spring应用中使用spring-boot-actuator模块提供监控端点
- 项目建设做好服务器,我院数字化建设项目(一期)服务器系统切换工作顺利完成...
- ASP.NET MVC3 RenderPartial 传入空Model时出现字典类型错误
- 泰晤士2021计算机科学排名,重磅!2021泰晤士学科排名发布!
- onenote 无法正常使用的解决办法
- 搜索关键字拼音智能提示实现
- 微信公众号开发:Java后台如何处理公众号关注和取关事件
- Android Studio计算BMI小软件
- CodeForces 427C Checkposts (强连通分量Tarjan模板题)
- 【人工智能 AI 2.0】阿里VP贾扬清被曝将离职创业:建大模型基础设施 已火速锁定首轮融资
- Selenium工作原理_Sinno_Song_新浪博客
- python条件语句多条件_Python简介,第3章-条件语句
- ACM-ICPC 2018 沈阳赛区网络预赛 G. Spare Tire
- flash导入swf文件
- 《毛毛虫团队》第六次作业:团队项目系统设计改进与详细设计
- js实现连续播放音频文件
- 设计模式之禅--读后感
- jQuery学习第三天(插件库、引入页面、jQ中的ajax)