栈接口的定义
public interface Stack { void push(Object obj); Object pop(); Object peek(); boolean isEmpty(); void clear(); }
栈的顺序存储结构操作实现
public class SequenceStack implements Stack { final int minSize=10; private Object[] stackArray; private int top; public SequenceStack() { top=-1; stackArray=new Object[minSize]; } public SequenceStack(int n) { if(n<minSize) n=minSize; top=-1; stackArray=new Object[n]; } public void push(Object obj) { if(top==stackArray.length-1){ Object[] p=new Object[top*2]; for(int i=0;i<=top;i++) p[i]=stackArray[i]; stackArray=p; } top++; stackArray[top]=obj; } public Object pop() { if(top==-1) return null; top--; return stackArray[top+1]; } public Object peek() { if(top==-1) return null; return stackArray[top]; } public boolean isEmpty() { return top==-1; } public void clear() { top=-1; } }
客户端实现代码
public class Example { public static void main(String[] args) { Stack sck=new SequenceStack(); int []a={3,8,5,17,9,30,15,22}; for(int i=0;i<a.length;i++) sck.push(a[i]); System.out.println(sck.pop()+","+sck.pop()+","+sck.pop()); sck.push(68); System.out.println(sck.peek()+","+sck.pop()+","+sck.pop()); while(!sck.isEmpty()) System.out.print(sck.pop()+" "); System.out.println(); sck.clear(); } }
栈的链接存储结构
链接栈的操作方法十分简单,具体如下:
public class LinkStack implements Stack { private Node top; public LinkStack() { top=null; } public void push(Object obj) { top=new Node(obj,top); } public Object pop() { if(top==null) return null; Node p=top; top=top.next; return p.element; } public Object peek() { if(top==null) return null; return top.element; } public boolean isEmpty() { return top==null; } public void clear() { top=null; } }
客户端实现代码只需要将上一个调试程序的SequenceStack()替换为LinkStack()就可以。
队列
public interface Queue { void enter(Object obj); Object leave(); Object peek(); boolean isEmpty(); void clear(); }
队列的顺序存储结构的实现
public class SequenceQueue implements Queue { final int minSize=10; private Object queueArray[]; private int front,rear; public SequenceQueue() { front=rear=0; queueArray=new Object[minSize]; } public SequenceQueue(int n) { front=rear=0; if(n<=minSize) n=minSize; queueArray=new Object[n]; } public void enter(Object obj) { if((rear+1)%queueArray.length==front) { Object[] p=new Object[queueArray.length*2]; if(rear==queueArray.length-1) { for(int i=1;i<=rear;i++) p[i]=queueArray[i]; } else{ int i,j=1; for(i=front+1;i<queueArray.length;i++,j++) p[j]=queueArray[i]; for(i=0;i<=rear;i++,j++) p[j]=queueArray[i]; front=0;rear=queueArray.length-1; } queueArray=p; } rear=(rear+1)%queueArray.length; queueArray[rear]=obj; } public Object leave() { if(front==rear) return null; front=(front+1)%queueArray.length; return queueArray[front]; } public Object peek() { if(front==rear) return null; return queueArray[(front+1)%queueArray.length]; } public boolean isEmpty() { return front==rear; } public void clear() { front=rear=0; } }
客户端实现代码:
public void Example { public static void main(String[] args) { Queue que=new SequenceQueue(); int []a={3,8,5,17,9,30,15,22,20,13,35,26}; int i; for(i=0;i<a.length;i++) que.enter(a[i]); System.out.print(que.leave()+" "); System.out.print(que.leave()+" "); que.enter(68); System.out.print(que.peek()+" "); System.out.print(que.leave()); while(!que.isEmpty()) System.out.print(que.leave()+" "); System.out.println(); que.clear(); } }
队列的链接存储结构的实现
public class LinkQueue implements Queue { private Node front,rear; public LinkQueue() { front=rear=null; } public void enter(Object obj) { if(rear==null) front=rear=new Node(obj,null); else rear=rear.next=new Node(obj,null); } public Object leave() { if(front==null) return null; Node x=front; front=front.next; if(front==null) rear=null; return x.element; } public Object peek() { if(front==null) return null; return front.element; } public boolean isEmpty() { return front==null; } public void clear() { front=rear=null; } }
客户端实现代码只需要将上一个调试程序的主函数的第一句改为Queue que=new LinkQueue();就可以了。
时间: 2024-10-07 04:42:43