Java自带的LinkedList提供泛型,底层是双向链表实现的
栈:是一个操作受限制的线性表,只能在一端进行删除或删除数据 FIFO 栈的实现:用链表或者数组实现一个集合类,这个集合类代表的数据结构是栈 如果一个集合类底层是数组,必定面临两个问题,数组初始化和长度
题外话: 分布式的原理:服务分布在不同的地方,这些分布在不同位置的服务又可以相互关联某些途径
实现栈
用链表实现栈stack
package com.cskaoyan.www.day4.stack;
public class MyLinkedStack<T> {
private Node top;
private int size;
public boolean push(T value){
top = new Node(value, top);
size++;
return true;
}
public T pop(){
if (isEmpty()) throw new RuntimeException("stack is empty");
T value = top.value;
top = top.next;
size--;
return value;
}
public T peek(){
if (isEmpty()) throw new RuntimeException("stack is empty");
return top.value;
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
class Node{
T value;
Node next;
public Node(T value, Node next) {
this.value = value;
this.next = next;
}
}
}
用数组实现stack
package com.cskaoyan.www.day4.stack;
import java.util.HashMap;
public class MyArrayStack<T> {
private final int MAX_CAPACITY = Integer.MAX_VALUE - 8;
private final int INIT_CAPACITY = 10;
private Object[] objs;
private int size;
public MyArrayStack(){
objs = new Object[INIT_CAPACITY];
}
public MyArrayStack(int initCapacity){
if (initCapacity < 1 || initCapacity > MAX_CAPACITY)throw new IllegalArgumentException("parame is Illegal");
objs = new Object[initCapacity];
}
public boolean push(T value){
if (size == objs.length){
int newLen = getLen();
grow(newLen);
}
objs[size] = value;
size++;
return true;
}
private void grow(int newLen) {
Object[] newObjs = new Object[newLen];
for (int i = 0; i < objs.length; i++) {
newObjs[i] = objs[i];
}
objs = newObjs;
}
private int getLen() {
int oldLen = objs.length;
int newLen = oldLen * 2;
if (newLen >= MAX_CAPACITY || newLen < 0){
newLen = MAX_CAPACITY;
}
if (newLen == oldLen)throw new RuntimeException("stack can not add");
return newLen;
}
public T pop(){
if (isEmpty()) throw new RuntimeException("stack is empty");
T value = (T)objs[size - 1];
size--;
return value;
}
public T peek(){
if (isEmpty()) throw new RuntimeException("stack is empty");
return (T) objs[size - 1];
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
}
队列
用链表实现queue
package com.cskaoyan.www.day4.queue;
public class MyLinkedQueue<T> {
private Node head;
private Node end;
private int size;
public boolean offer(T value){
if (isEmpty()){
head = new Node(value, null );
end = head;
size++;
return true;
}else {
Node newNode = new Node(value, null);
end.next = newNode;
end = newNode;
size++;
return true;
}
}
public T poll(){
if (isEmpty())throw new RuntimeException("queue is empty");
T value = head.value;
if (size == 1){
head = null;
end = null;
}else {
head = head.next;
}
size--;
return value;
}
public T peek() {
if (isEmpty()) throw new RuntimeException("queue is empty");
return head.value;
}
public boolean isEmpty(){
return size == 0;
}
public int size(){
return size;
}
class Node{
T value;
Node next;
public Node(T value, Node next) {
this.value = value;
this.next = next;
}
}
}
用数组实现queue
package com.cskaoyan.www.day4.queue;
public class MyArrayQueue<T> {
private final int MAX_CAPACITY = Integer.MAX_VALUE - 8;
private final int INIT_CAPACITY = 10;
private Object[] arr;
private int size;
private int head;
private int end;
public MyArrayQueue() {
this.arr = new Object[INIT_CAPACITY];
}
public MyArrayQueue(int initCapacity) {
if (initCapacity < 1 || initCapacity > MAX_CAPACITY)throw new IllegalArgumentException("initCapacity is Illegal");
this.arr = new Object[initCapacity];
}
public boolean offer(T value){
if (size == arr.length){
int newLen = getLen();
grow(newLen);
}
if (size == 0){
arr[head] = value;
end = head;
}else {
end = (end + 1) % arr.length;
arr[end] = value;
}
size++;
return true;
}
private void grow(int newLen) {
Object[] newArr = new Object[newLen];
for (int i = 0; i < arr.length; i++) {
int index = (head + i) % arr.length;
newArr[i] = arr[index];
}
arr = newArr;
head = 0;
end = size -1;
}
private int getLen() {
int oldLen = arr.length;
int newLen = oldLen * 2;
if (newLen >= MAX_CAPACITY || newLen < 0){
newLen = MAX_CAPACITY;
}
if (newLen == oldLen)throw new RuntimeException("stack can not add");
return newLen;
}
public T poll(){
if (isEmpty()) throw new RuntimeException("queue is empty");
T vlaue = (T)arr[head];
if (size == 1){
head = 0;
end = 0;
}else {
head = (head + 1) % arr.length;
}
size--;
return vlaue;
}
public T peek() {
if (isEmpty()) throw new RuntimeException("queue is empty");
return (T)arr[head];
}
public boolean isEmpty(){
return size == 0;
}
}
|