一、栈
- 定义:只允许在一端插入与删除的线性表
- 特点:先进后出
- 栈的抽象数据类型
package 栈与队列;
public interface IStack {
public boolean isEmpty();
public void clear();
public int length();
//压栈
public void push(Object o) throws Exception;
//删除栈顶元素并且返回
public Object pop();
//获取栈顶元素
public Object peek();
}
- 顺序栈及其基本操作的实现
package 栈与队列;
public class SQStack implements IStack{
private Object[] stack; //栈的储存空间
private int top; //指向下一个储存位置
@Override
public boolean isEmpty() {
return top == 0;
}
@Override
public void clear() {
top = 0;
}
@Override
public int length() {
return top;
}
@Override
public void push(Object o) throws Exception {
if(top == stack.length) {
throw new Exception("栈已满");
}else {
stack[top] = o;
top++;
}
}
@Override
public Object pop() {
if(isEmpty()) {
return null;
}else {
Object o = new Object();
o = stack[top-1];
top = top-1;
return o;
}
}
@Override
public Object peek() {
if(isEmpty()) {
return null;
}else {
return stack[top-1];
}
}
public void print() {
for(int i = top - 1; i >= 0 ; i-- ) {
System.out.print(stack[i].toString()+" ");
}
}
}
- 链式栈类
package 栈与队列;
public class LinkStack implements IStack {
private SingleNode node; //栈顶元素的引用
@Override
public boolean isEmpty() {
return node == null;
}
@Override
public void clear() {
node = null;
}
@Override
public int length() {
SingleNode p = node; //将指针指向栈顶元素
int length = 0 ;
if(p != null) {
p = p.getNext();
length ++;
}
return length;
}
@Override
public void push(Object o) throws Exception {
SingleNode p = new SingleNode(o);
p.setNext(node);
node = p;
}
@Override
public Object pop() {
if(isEmpty()) {
return null;
}else {
SingleNode p = node;
node.setNext(node.getNext());
return p.getO();
}
}
@Override
public Object peek() {
if(isEmpty()) {
return null;
}else {
return node.getO();
}
}
public void print() {
SingleNode p = node;
while(p != null) {
System.out.print(p.getO().toString()+" ");
}
System.out.println();
}
}
二、 队列
1、定义:删除操作只能在一端(队首)进行,而插入操作只能在另一端(队尾)进行的线性表。
2、特点:先进先出
3、队列的抽象数据类型描述
package 栈与队列;
public interface IQueue {
public void clear();
public boolean isEmpty();
public int length();
public Object peek();
public void offer(Object o) throws Exception;
public Object poll();
}
4、顺序队列及其基本操作的实现
package 栈与队列;
public class SQQueue implements IQueue{
private Object[] queue;
private int next; // 指向队列的下一个,即队尾
@Override
public void clear() {
next = 0;
}
@Override
public boolean isEmpty() {
return next==0;
}
@Override
public int length() {
return next;
}
@Override
public Object peek() {
if(isEmpty()) {
return null;
}else {
return queue[0];
}
}
@Override
public void offer(Object o) throws Exception {
if(next == queue.length) {
throw new Exception("队列已满");
}else {
queue[next] = o;
next = next + 1;
}
}
@Override
public Object poll() {
if(isEmpty()) {
return null;
}else {
Object o = new Object();
o = queue[0];
for(int i = 0 ; i < next; i++) {
queue[i] = queue[i+1];
}
next = next - 1;
return o;
}
}
}
5、链式队列及其基本操作的实现
package 栈与队列;
public class LinkQueue implements IQueue{
private SingleNode front;
private SingleNode rear;
@Override
public void clear() {
front = rear = null;
}
@Override
public boolean isEmpty() {
return front == null;
}
@Override
public int length() {
SingleNode p = front;
int length = 0;
while(p != null) {
p = p.getNext();
length ++;
}
return length;
}
@Override
public Object peek() {
if(isEmpty()) {
return null;
}else {
return front.getO();
}
}
@Override
public void offer(Object o) throws Exception {
SingleNode p = new SingleNode(o);
if(front != null) {
rear.setNext(p);
rear = p;
}
}
@Override
public Object poll() {
if(front != null) {
SingleNode p = front;
front = front.getNext();
return p.getO();
}else {
return null;
}
}
}
6、循环队列及其基本操作的实现
package 栈与队列;
public class CircleQueue implements IQueue{
private Object[] queue; //储存空间
private int front;
private int rear;
public CircleQueue(int maxsize) {
front = rear = 0;
queue = new Object[maxsize];
}
@Override
public void clear() {
front = rear = 0;
}
@Override
public boolean isEmpty() {
return front == rear;
}
@Override
public int length() {
return (rear - front + queue.length)%queue.length;
}
@Override
public Object peek() {
if(isEmpty()) {
return null;
}else {
return queue[front];
}
}
@Override
public void offer(Object o) throws Exception {
if((rear+1)%queue.length == front) {
throw new Exception("队列已满");
}else {
queue[rear] = o ;
rear = (rear+1)%queue.length; //修改队尾指针
}
}
@Override
public Object poll() {
if(isEmpty()) {
return null;
}else {
Object o = queue[front];
front = (front+1)%queue.length;
return o;
}
}
}
注意:
队列初始化:front = rear = 0;
队空条件:front == rear;
队满条件:(rear+1) % maxSize == front;
队首指针进1: front = (front+1) % maxSize;
队尾指针进1: rear = (rear+1) % maxSize;
参考教材:数据结构——Java语言描述(清华大学出版社)