java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue
LikedList:
package Date_pacage;
public class LinkedList<E> {
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
for(int i = 0 ; i < 5 ; i ++) {
linkedList.addFirst(i);
System.out.println(linkedList);
}
linkedList.add(2, 666);
System.out.println(linkedList);
linkedList.remove(2);
System.out.println(linkedList);
}
private class Node{
public E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e, null);
}
public Node() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private Node dummyHead;
private int size;
public LinkedList() {
dummyHead = new Node(null, null);
size = 0;
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
public void add(int index, E e) {
if(index < 0 || index > size) {
throw new IllegalArgumentException("Add failed. Illegal index.");
}
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++) {
prev = prev.next;
}
// Node node = new Node(e);
// node.next = prev.next;
// prev.next = node;
prev.next = new Node(e, prev.next);
size ++;
}
public void addFirst(E e) {
// Node node = new Node(e);
// node.next = head;
// head = node;
add(0, e);
}
public void addLast(E e) {
add(size, e);
}
public E get(int index) {
if(index < 0 || index > size) {
throw new IllegalArgumentException("Get failed. Illegal index.");
}
Node cur = dummyHead.next;
for(int i = 0 ; i < index ; i ++) {
cur = cur.next;
}
return cur.e;
}
public E getFirst() {
return get(0);
}
public E getLast() {
return get(size - 1);
}
//修改链表的第index位置的元素为e
public void set(int index, E e) {
if(index < 0 || index > size) {
throw new IllegalArgumentException("Get failed. Illegal index.");
}
Node cur = dummyHead.next;
for(int i = 0; i < index ; i ++) {
cur = cur.next;
}
cur.e = e;
}
public boolean contains(E e) {
Node cur = dummyHead.next;
while(cur != null){
if(cur.e.equals(e)){
return true;
}
cur = cur.next;
}
return false;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
// Node cur = dummyHead.next;
// while(cur != null) {
// res.append(cur + "->");
// cur = cur.next;
// }
for(Node cur = dummyHead ; cur != null ; cur = cur.next) {
res.append(cur.e + "->");
}
res.append("NULL");
return res.toString();
}
public E remove(int index) {
if(index < 0 || index > size) {
throw new IllegalArgumentException("Get failed. Illegal index.");
}
Node prev = dummyHead;
for(int i = 0 ; i < index ; i ++) {
prev = prev.next;
}
Node retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size --;
return retNode.e;
}
public E removeFirst() {
return remove(0);
}
public E removrLast() {
return remove(size - 1);
}
}
链表栈 LinkedListStack:
package Date_pacage;
public class LinkedListStack<E> implements Stack<E> {
public static void main(String[] args) {
LinkedListStack<Integer> stack = new LinkedListStack<>();
for(int i = 0 ; i < 5 ; i ++) {
stack.push(i);
System.out.println(stack);
}
stack.pop();
System.out.println(stack);
}
private LinkedList<E> list;
public LinkedListStack() {
list = new LinkedList<>();
}
@Override
public int getSize() {
return list.getSize();
}
@Override
public boolean isEmpty() {
return list.isEmpty();
}
@Override
public void push(E e) {
list.addFirst(e);
}
@Override
public E pop() {
return list.removeFirst();
}
@Override
public E peek() {
return list.getFirst();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Stack: top");
res.append(list);
return res.toString();
}
}
链表队列 LinkedListQueue:
package Date_pacage; //使用带尾节点的链表
public class LinkedListQueue<E> implements Queue<E> {
public static void main(String[] args) {
LinkedListQueue<Integer> linkedList = new LinkedListQueue<>();
for(int i = 0 ; i < 5 ; i ++) {
linkedList.enqueue(i);
System.out.println(linkedList);
}
linkedList.enqueue(666);
System.out.println(linkedList);
linkedList.dequeue();
System.out.println(linkedList);
}
private class Node{
public E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e, null);
}
public Node() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
} private Node head, tail;
private int size;
public LinkedListQueue() {
head = null;
tail = null;
size = 0;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public E getFront() {
if(isEmpty()) {
throw new IllegalArgumentException("Cannot getFront from an empty queue.");
}
return head.e;
}
@Override
public void enqueue(E e) {
if(tail == null) {
tail = new Node(e);
head = tail;
}else {
tail.next = new Node(e);
tail = tail.next;
}
size++;
}
@Override
public E dequeue() {
if(isEmpty()) {
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
}
Node retNode = head;
head = head.next;
retNode.next = null;
if(head == null) {
tail = null;
}
size--;
return retNode.e;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue: front");
Node cur =head;
while(cur != null) {
res.append(cur + "->");
cur = cur.next;
}
res.append("NULL tail");
return res.toString();
}
}
java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue的更多相关文章
- 2018.9.5 Java中使用栈来模拟队列
栈的规律是是先进后出 队列的规律是先进先出 栈模拟队列 首先我们定义两个栈,一个放数据,一个出数据,判断B栈是否有元素,有元素则直接pop:没有元素则需要我们将A里面的元素出栈然后放到B里面,再取出, ...
- JAVA:使用栈实现一个队列
使用栈实现一个队列,需要弄清楚栈和队列的区别: 栈:先进后出: 队列:先进先出. 实现思路: 1)通过两个栈(pushStack / popStack)对倒,确保 popStack 栈的出栈顺序与队列 ...
- java两个栈实现一个队列&&两个队列实现一个栈
栈:先进后出 队列:先进先出 两个栈实现一个队列: 思路:先将数据存到第一个栈里,再将第一个栈里的元素全部出栈到第二个栈,第二个栈出栈,即可达到先进先出 源码: class Queue<E&g ...
- Java数据类型Stack栈、Queue队列、数组队列和循环队列的比较
判断括号是否匹配:调用java本身 import java.util.Stack; public class Solution { public boolean isValid(String s){ ...
- Java数据结构——用链表实现栈
//================================================= // File Name : LinkStack_demo //---------------- ...
- 线性表 及Java实现 顺序表、链表、栈、队列
数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...
- java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表 数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...
- 数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解
前言 其实在学习数据结构之前,我也是从来都没了解过这门课,但是随着工作的慢慢深入,之前学习的东西实在是不够用,并且太皮毛了.太浅,只是懂得一些浅层的,我知道这个东西怎么用,但是要优化.或者是解析,就不 ...
- Java用链表实现栈和队列
1.用链表实现栈 package stack; /** * * @author denghb * */ class Link { public long dData; public Link next ...
随机推荐
- STM32数据类型定义
#ifndef __STM32F10x_TYPE_H #define __STM32F10x_TYPE_H typedef signed long s32; typedef signed short ...
- 算法Sedgewick第四版-第1章基础-010一检查括号是否成对出现
/****************************************************************************** * Compilation: javac ...
- hadoop运行故障问题解决1——datanode节点启动后自动关闭
ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: java.io.IOException: Incompatible namespaceID ...
- 使用IDEA编译spark 1.5并运行example的代码
操作系统:windows 10 IDEA : IDEA 14.1.4 1:使用IDEA导入spark 1.5的源码,注意maven配置为自动导入 2:在maven窗口下的profiles中勾选hado ...
- 使用swiper.animate时,给一个对象添加两个动画且动画循环的方法
swiper官网上给对象加一个动画的方法是 <div class="swiper-slide"> <p class="ani" swiper- ...
- 双击获取GridView控件行信息
有网友要求在GridView控件上,不管是单击(onclick)还是双击(ondblclick),想获取所击行的信息.技术难度是为GridView的行注册单击或是双击事件.看例子吧:在数据库中创建数据 ...
- boostrap 进入条显示百分比
<div class="progress"> <div class="progress-bar progress-bar-success" ...
- jest+vue-test-utils初步实践
一.起步 1. jest Jest是 Facebook 的一套开源的 JavaScript 测试框架, 它自动集成了断言.JSDom.覆盖率报告等开发者所需要的所有测试工具,配置较少,对vue框架友好 ...
- [SinGuLaRiTy] 分治题目复习
[SInGuLaRiTy-1025] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 1905] 棍的膨胀 (Expanding ...
- 前端开发快速定位bug的一些小技巧
1,根据报错信息定位: (1) Uncaught TypeError: Cannot read property 'attr' of undefined; 此类型为变量或者对象属性未定义类型. (2) ...