Java数据结构和算法--栈与队列

来源:互联网 发布:专业照片恢复软件 编辑:程序博客网 时间:2024/05/30 23:45
(1)栈 
Java代码  收藏代码
  1. package ChapterOne;  
  2.   
  3. public class Stack {  
  4.     //栈数组  
  5.     long stackArr[];  
  6.     //栈的大小  
  7.     int maxSize;  
  8.     //栈的顶部  
  9.     int top;  
  10.     //初始化一个大小为size的栈  
  11.     public Stack(int size){  
  12.         maxSize = size;   
  13.         stackArr = new long[size];  
  14.         top = -1;  
  15.     }  
  16.     //出栈操作  
  17.     public long pop(){  
  18.         return stackArr[top--];  
  19.     }  
  20.     //进栈操作  
  21.     public void push(long value){  
  22.         stackArr[++top] = value;  
  23.     }  
  24.     //判断栈是否为空  
  25.     public boolean isEmpty(){  
  26.         return top == -1;  
  27.     }  
  28.     //判断栈是否已满  
  29.     public boolean isFull(){  
  30.         return top == maxSize-1;  
  31.     }  
  32.     //取栈顶元素  
  33.     public long peek(){  
  34.         return stackArr[top];  
  35.     }  
  36.     public static void main(String[] args) {  
  37.         Stack stack = new Stack(10);  
  38.         while(!stack.isFull()){  
  39.             long v = (long) (Math.random()*100);  
  40.             stack.push(v);  
  41.             System.out.print(v+" ");  
  42.         }  
  43.         System.out.println();  
  44.         while(!stack.isEmpty()){  
  45.             long topValue = stack.pop();  
  46.             System.out.print(topValue+" ");  
  47.         }  
  48.         System.out.println();  
  49.     }  
  50. }  

(2)队列 
Java代码  收藏代码
  1. package ChapterOne;  
  2.   
  3. public class Queue {  
  4.     //队列数组  
  5.     private long queueArr[];  
  6.     //队列的前端下标  
  7.     private int front;  
  8.     //队列的尾端下标  
  9.     private int rear;  
  10.     //队列的大小  
  11.     private int maxSize;  
  12.     //队列中元素的个数  
  13.     private int nItems;  
  14.     //初始化一个大小为size的队列  
  15.     public Queue(int size){  
  16.         queueArr = new long[size];  
  17.         maxSize = size;  
  18.         front = 0;  
  19.         rear = -1;  
  20.         nItems = 0;  
  21.     }  
  22.     //插入操作  
  23.     public void insert(long value){  
  24.         //队列已满  
  25.         if(rear == maxSize-1)  
  26.             rear = -1;  
  27.         queueArr[++rear] = value;  
  28.         nItems++;  
  29.     }  
  30.     //删除操作  
  31.     public long remove(){  
  32.         long temp = queueArr[front++];  
  33.         if(front == maxSize)  
  34.             front = 0;  
  35.         nItems--;  
  36.         return temp;  
  37.     }  
  38.     //返回队列第一个元素  
  39.     public long peakFront(){  
  40.         return queueArr[front];  
  41.     }  
  42.     //判断是否为空  
  43.     public boolean isEmpty(){  
  44.         return nItems == 0;  
  45.     }  
  46.     //判断是否已满  
  47.     public boolean isFull(){  
  48.         return nItems == maxSize;  
  49.     }  
  50.     //返回队列中元素的个数  
  51.     public int size(){  
  52.         return nItems;  
  53.     }  
  54.       
  55.     public void print(){  
  56.         for(int i = front;i < front+nItems;i++){  
  57.             System.out.print(queueArr[i]+" ");  
  58.         }  
  59.         System.out.println();  
  60.     }  
  61.       
  62.     public static void main(String[] args) {  
  63.         Queue q = new Queue(10);  
  64.         while(!q.isFull()){  
  65.             long value = (long)(Math.random()*100);  
  66.             q.insert(value);  
  67.         }  
  68.         q.print();  
  69.         while(!q.isEmpty()){  
  70.             q.remove();  
  71.             q.print();  
  72.         }  
  73.         q.print();  
  74.         System.out.println(q.isEmpty());  
  75.     }  
  76. }  

(3)优先队列 
Java代码  收藏代码
  1. package ChapterOne;  
  2.   
  3. public class PriorityQueue {  
  4.   
  5.     private int nItems;  
  6.       
  7.     private long pqArr[];  
  8.       
  9.     private int maxSize;  
  10.       
  11.     public PriorityQueue(int size){  
  12.         maxSize = size;  
  13.         pqArr = new long[size];  
  14.         nItems = 0;  
  15.     }  
  16.       
  17.     public void insert(long value){  
  18.         int i;  
  19.         if(nItems == 0)  
  20.             pqArr[nItems++] = value;  
  21.         else{  
  22.             for(i = nItems-1;i >= 0;i--){  
  23.                 if(value < pqArr[i]){  
  24.                     pqArr[i+1] = pqArr[i];  
  25.                 }  
  26.                 else  
  27.                     break;  
  28.             }  
  29.             pqArr[i+1] = value;  
  30.             nItems++;  
  31.         }  
  32.     }  
  33.       
  34.     public long remove(){  
  35.         return pqArr[--nItems];  
  36.     }  
  37.       
  38.     public boolean isEmpty(){  
  39.         return nItems == 0;  
  40.     }  
  41.       
  42.     public boolean isFull(){  
  43.         return nItems == maxSize;  
  44.     }  
  45.       
  46.     public void print(){  
  47.         for(int i = 0;i < nItems;i++)  
  48.             System.out.print(pqArr[i]+" ");  
  49.         System.out.println();  
  50.     }  
  51.       
  52.     public static void main(String[] args) {  
  53.         PriorityQueue pq = new PriorityQueue(10);  
  54.         while(!pq.isFull()){  
  55.             long value = (long)(Math.random()*100);  
  56.             pq.insert(value);  
  57.         }  
  58.         pq.print();  
  59.     }  
  60. }