package com.ttpfx.fundamentals.linked;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class LinkedStack<Item> {
private Node first = null;
private int size;
private class Node {
Item item;
Node next;
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return size;
}
public void push(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
size++;
}
public Item pop() {
Item item = first.item;
first = first.next;
size--;
return item;
}
// 测试输入:to be or not to - be - - that - - - is
public static void main(String[] args) {
LinkedStack<String> stack = new LinkedStack<>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) {
stack.push(item);
} else if (!stack.isEmpty()) {
StdOut.print(stack.pop() + " ");
}
}
StdOut.println("(" + stack.size() + " left on stack)");
}
}
package com.ttpfx.fundamentals.linked;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class LinkedQueue<Item> {
private Node first;
private Node last;
private int size;
private class Node {
Item item;
Node next;
}
/**
* 添加一个元素
*/
public void enqueue(Item item) {
Node oldLast = last;
last = new Node();
last.item = item;
if (oldLast == null) {
first = last;
} else {
oldLast.next = last;
}
size++;
}
/**
* 删除最早添加的元素
*/
public Item dequeue() {
if (isEmpty()) {
return null;
}
Item item = first.item;
if (first == last) {
first = null;
last = null;
} else {
first = first.next;
}
size--;
return item;
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return size;
}
// 测试:to be or not to - be - - that - - - is
public static void main(String[] args) {
LinkedQueue<String> queue = new LinkedQueue<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) {
queue.enqueue(item);
} else if (!queue.isEmpty()) {
StdOut.print(queue.dequeue() + " ");
}
}
StdOut.println("(" + queue.size() + " left on queue)");
}
}
package com.ttpfx.fundamentals.linked;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.util.Iterator;
public class LinkedBag<Item> implements Iterable<Item> {
private Node first = null;
private int size;
private class Node {
Item item;
Node next;
}
public boolean isEmpty() {
return first == null;
}
public int size() {
return size;
}
public void add(Item item) {
Node oldFirst = first;
first = new Node();
first.item = item;
first.next = oldFirst;
size++;
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private Node node = first;
@Override
public boolean hasNext() {
return node != null;
}
@Override
public Item next() {
Item item = node.item;
node = node.next;
return item;
}
}
// 测试输入:to be or not to be that is a question
public static void main(String[] args) {
LinkedBag<String> bag = new LinkedBag<>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
bag.add(item);
}
for (String s : bag) {
StdOut.println(s);
}
}
}
原文:https://www.cnblogs.com/ttpfx/p/13925087.html