62,615
社区成员
发帖
与我相关
我的任务
分享
STACK-EMPTY(S)
1 if top[S] = 0
2 then return TRUE
3 else return FALSE
PUSH(S, x)
1 top[S] ← top[S] + 1
2 S[top[S]] ← x
POP(S)
1 if STACK-EMPTY(S)
2 then error "underflow"
3 else top[S] ← top[S] - 1
4 return S[top[S] + 1]
import java.util.Arrays;
import java.util.EmptyStackException;
public class Stack<E> {
private int count;
private Object[] objs;
private int capacityIncrement;
private int top() {
return count;
}
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = objs.length;
if (minCapacity > oldCapacity) {
int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement)
: (oldCapacity * 2);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
objs = Arrays.copyOf(objs, newCapacity);
}
}
public boolean isEmpty() {
return top() == 0;
}
public synchronized E push(E item) {
ensureCapacityHelper(count + 1);
objs[count++] = item;
return item;
}
@SuppressWarnings("unchecked")
public synchronized E pop() {
if (isEmpty())
throw new EmptyStackException();
else {
Object obj = (E) objs[count - 1];
count--;
objs[count] = null;
return (E) obj;
}
}
public Stack() {
this(10);
}
public Stack(int initialCapacity) {
this(initialCapacity, 0);
}
public Stack(int initialCapacity, int capacityIncrement) {
objs = new Object[initialCapacity];
}
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < 11; i++) {
stack.push(i);
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
}
ENQUEUE(Q, x)
1 Q[tail[Q]] ← x
2 if tail[Q] = length[Q]
3 then tail[Q] ← 1
4 else tail[Q] ← tail[Q] + 1
DEQUEUE(Q)
1 x ← Q[head[Q]]
2 if head[Q] = length[Q]
3 then head[Q] ← 1
4 else head[Q] ← head[Q] + 1
5 return x
import java.util.Arrays;
import java.util.NoSuchElementException;
public class Queue<E> {
private int head = 0;
private int tail = 0;
private Object[] objs;
private int capacityIncrement;
public boolean isEmpty() {
return head == tail;
}
public synchronized void enqueue(E item) {
objs[tail] = item;
if (tail == objs.length - 1)
tail = 0;
else
tail++;
if (tail == head) {
int oldLength = objs.length;
ensureCapacityHelper(objs.length + 1);
for (int i = tail; i < oldLength; i++) {
objs[i + objs.length - oldLength] = objs[i];
}
head += objs.length - oldLength;
}
}
@SuppressWarnings("unchecked")
public synchronized E dequeue() {
if (isEmpty())
throw new NoSuchElementException();
Object obj = objs[head];
if (head == objs.length - 1)
head = 0;
else
head++;
return (E) obj;
}
private void ensureCapacityHelper(int minCapacity) {
int oldCapacity = objs.length;
if (minCapacity > oldCapacity) {
int newCapacity = (capacityIncrement > 0) ? (oldCapacity + capacityIncrement)
: (oldCapacity * 2);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
objs = Arrays.copyOf(objs, newCapacity);
}
}
public Queue() {
this(10);
}
public Queue(int initialCapacity) {
this(initialCapacity, 0);
}
public Queue(int initialCapacity, int capacityIncrement) {
objs = new Object[initialCapacity];
}
public static void main(String[] args) {
Queue<Integer> queue = new Queue<Integer>();
for (int i = 0; i < 8; i++)
queue.enqueue(i);
for (int i = 0; i < 4; i++)
System.out.print(queue.dequeue() + " ");
for (int i = 0; i < 9; i++)
queue.enqueue(i + 10);
while (!queue.isEmpty())
System.out.print(queue.dequeue() + " ");
}
}
LIST-SEARCH(L, k)
1 x ← head[L]
2 while x ≠ NIL and key[x] ≠ k
3 do x ← next[x]
4 return x
LIST-INSERT(L, x)
1 next[x] ← head[L]
2 if head[L] ≠ NIL
3 then prev[head[L]] ← x
4 head[L] ← x
5 prev[x] ← NIL
LIST-DELETE(L, x)
1 if prev[x] ≠ NIL
2 then next[prev[x]] ← next[x]
3 else head[L] ← next[x]
4 if next[x] ≠ NIL
5 then prev[next[x]] ← prev[x]
public class List<E> {
private transient Entry<E> header = new Entry<E>(null);
public static class Entry<E> {
E element;
Entry<E> next;
Entry<E> previous;
Entry(E element) {
this.element = element;
}
}
public Entry<E> search(E x) {
if (x == null)
return null;
Entry<E> entry = header;
while (entry != null && !x.equals(entry.element))
entry = entry.next;
return entry;
}
public Entry<E> insert(E x) {
Entry<E> newEntry = new Entry<E>(x);
newEntry.next = header;
if (header != null)
header.previous = newEntry;
header = newEntry;
newEntry.previous = null;
return newEntry;
}
public void delete(E x) {
Entry<E> entry = search(x);
if (entry.previous != null)
entry.previous.next = entry.next;
else
header = entry.next;
if (entry.next != null)
entry.next.previous = entry.previous;
}
public static void main(String[] args) {
List<Integer> ll = new List<Integer>();
ll.insert(1);
ll.insert(2);
ll.insert(3);
ll.insert(4);
ll.delete(3);
Entry<Integer> e = ll.search(2);
System.out.println(e.next.element);
System.out.println(e.previous.element);
}
}
LIST-DELET′ (L, x)
1 next[prev[x]] ← next[x]
2 prev[next[x]] ← prev[x]
LIST-SEARC′(L, k)
1 x ← next[nil[L]]
2 while x ≠ nil[L] and key[x] ≠ k
3 do x ← next[x]
4 return x
LIST-INSER′ (L, x)
1 next[x] ← next[nil[L]]
2 prev[next[nil[L]]] ← x
3 next[nil[L]] ← x
4 prev[x] ← nil[L]