题目5
1.使用单链表高效实现栈类,不用头节点和尾节点。
2.使用单链表高效实现队列类,不用头节点和尾节点。
3.使用循环数组高效实现队列类。
java代码:
1.public class SingleStack<AnyType> {
//实现栈就是模拟push和pop方法。
private Node<AnyType> head;
private class Node<AnyType>
{
AnyType data;
Node next;
Node(){this(null,null);}
Node(AnyType d){this(d,null);}
Node(AnyType d,Node p){data = d; next = p;}
}
//进栈
void push(AnyType x)
{
Node<AnyType> p = new Node<AnyType>(x,head);
head = p;
}
void top()
{
head = head.next;
}
//出栈
AnyType pop()
{
return head.data;
}
}