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的更多相关文章

  1. 2018.9.5 Java中使用栈来模拟队列

    栈的规律是是先进后出 队列的规律是先进先出 栈模拟队列 首先我们定义两个栈,一个放数据,一个出数据,判断B栈是否有元素,有元素则直接pop:没有元素则需要我们将A里面的元素出栈然后放到B里面,再取出, ...

  2. JAVA:使用栈实现一个队列

    使用栈实现一个队列,需要弄清楚栈和队列的区别: 栈:先进后出: 队列:先进先出. 实现思路: 1)通过两个栈(pushStack / popStack)对倒,确保 popStack 栈的出栈顺序与队列 ...

  3. java两个栈实现一个队列&&两个队列实现一个栈

    栈:先进后出  队列:先进先出 两个栈实现一个队列: 思路:先将数据存到第一个栈里,再将第一个栈里的元素全部出栈到第二个栈,第二个栈出栈,即可达到先进先出 源码: class Queue<E&g ...

  4. Java数据类型Stack栈、Queue队列、数组队列和循环队列的比较

    判断括号是否匹配:调用java本身 import java.util.Stack; public class Solution { public boolean isValid(String s){ ...

  5. Java数据结构——用链表实现栈

    //================================================= // File Name : LinkStack_demo //---------------- ...

  6. 线性表 及Java实现 顺序表、链表、栈、队列

    数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...

  7. java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表

    java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表   数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...

  8. 数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解

    前言 其实在学习数据结构之前,我也是从来都没了解过这门课,但是随着工作的慢慢深入,之前学习的东西实在是不够用,并且太皮毛了.太浅,只是懂得一些浅层的,我知道这个东西怎么用,但是要优化.或者是解析,就不 ...

  9. Java用链表实现栈和队列

    1.用链表实现栈 package stack; /** * * @author denghb * */ class Link { public long dData; public Link next ...

随机推荐

  1. TaikrSpaceShooterStartKit.unitypackage包下载地址

    有好多教程里面没有资源包,现在加密分享给大家 unity4.*  链接: https://pan.baidu.com/s/1XMo2zVpV3ZhkNZKOb6H0yw 密码: tqnt unity5 ...

  2. 机器人自主移动的秘密,从SLAM技术说起(一)

    博客转载自:https://www.leiphone.com/news/201609/c35bn1M9kgVaCCef.html 雷锋网(公众号:雷锋网)按:本文作者SLAMTEC(思岚科技公号sla ...

  3. Linux bc命令

    一.简介 GNU bc是一款基于命令行的计算器程序,支持高精度数字和多种数值类型(例如二进制.十进制.十六进制)的输入输出. 二.实例 http://www.linuxidc.com/Linux/20 ...

  4. cakephp目录结构

  5. CF 432B :Football Kit

    hash做法: #include<stdio.h> #include<string.h> ; int home[Max],away[Max],hash[Max]; int ma ...

  6. WordCount编码测试

    Github项目地址:https://github.com/LantyrLYL/WordCount PSP表格: PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning 计 ...

  7. C语言结构体--位域

    有些数据在存储时并不需要占用一个完整的字节,只需要占用一个或几个二进制位即可.比如开关只有通电和断电两种状态,用 0 和 1 表示足以,也就是用一个二进位.正是基于这种考虑,C语言又提供了一种叫做位域 ...

  8. linux 进程间通信机制(IPC机制)一消息队列

    消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法.每个数据块都被认为含有一个类型,接收进程可以独立地接收含有不同类型的数据结构.我们可以通过发送消息来避免命名管道的同步和阻塞问题.但是消息 ...

  9. Delphi和C#数据类型对应表

    Delphi DataType C# datatype ansistring string boolean bool byte byte char char comp double currency ...

  10. docker网络模式----入门docker的难点

    众所周知,现在docker是轻量级虚拟化的典型代表!这段时间想要建立一个分布式系统,但是手头上主机没那么多,所以使用docker进行虚拟化,但是在使用的过程中对网络这一部分是一直不太理解,特别找了一篇 ...