数据结构堆栈和队列

转自:http://blog.csdn.net/qq1712088151/article/details/7357476?locationNum=12&fps=1

堆栈和队列都是线性表结构。只不过堆栈的逻辑数据特点是先进后出(FILO),而队列的逻辑数据特点是先进先出(FIFO)。我们先用数组来存放这两种数据结构,也就是线性的存储结构。

请看下例:

[java]  view plain  copy
 print ?
  1. public class StackT {  
  2.     private int maxSize; // 堆栈的大小  
  3.     private int[] stackArray;  
  4.     private int top; // 堆栈顶部指针  
  5.   
  6.     public StackT(int s) {  
  7.         maxSize = s; // 初始化数组大小  
  8.         stackArray = new int[maxSize]; // 初始化一个数组  
  9.         top = -1;  
  10.     }  
  11.   
  12.     public void push(int j){  
  13.         if (!isFull())  
  14.             stackArray[++top] = j;  
  15.         else  
  16.             return;  
  17.     }  
  18.   
  19.     public int pop(){  
  20.         return stackArray[top--];  
  21.     }  
  22.   
  23.     public int peek(){// 得到栈顶的数据而不是出栈     
  24.         return stackArray[top];  
  25.     }  
  26.   
  27.     public boolean isEmpty(){  
  28.         return (top == -1);  
  29.     }  
  30.   
  31.     public boolean isFull(){  
  32.         return (top == maxSize - 1);  
  33.     }  
  34.   
  35.     public String toString(){  
  36.         StringBuffer sb = new StringBuffer();  
  37.         for (int i = top; i >= 0; i--)  
  38.             sb.append("" + stackArray[i] + "\n");  
  39.         return sb.toString();  
  40.     }  
  41.   
  42. // end class StackX  
  43.   
  44. class StackApp {  
  45.     public static void main(String[] args) {  
  46.         StackT theStack = new StackT(10); // 初始化一个堆栈  
  47.         theStack.push(20);  
  48.         theStack.push(40);  
  49.         theStack.push(60);  
  50.         theStack.push(80);  
  51.         System.out.println(theStack);  
  52.         System.out.println("");  
  53.     } // end main()  
  54. // end class StackApp  

运行的结果如图:

队列的实现(循环队列):

[java]  view plain  copy
 print ?
  1. public class Queue {  
  2.     private int maxSize; // 表示队列的大小  
  3.     private int[] queArr; // 用数组来存放有队列的数据  
  4.     private int front; // 取数据的下标  
  5.     private int rear; // 存数据的下标  
  6.     private int nItems; // 记录存放的数据个数  
  7.   
  8.     public Queue(int s) {  
  9.         maxSize = s;  
  10.         queArr = new int[maxSize];  
  11.         front = 0;  
  12.         rear = -1;  
  13.         nItems = 0;  
  14.     }  
  15.   
  16.     public void insert(int j) { // 增加数据的方法  
  17.         if (isFull())  
  18.             return;  
  19.         // 如果下标到达数组顶部的话,让rear指向数组的第一个位置之前  
  20.         if (rear == maxSize - 1)  
  21.             rear = -1;  
  22.         queArr[++rear] = j; // increment rear and insert  
  23.         nItems++; // one more item  
  24.     }  
  25.   
  26.     public int remove(){  
  27.         int temp = queArr[front++];  
  28.         // 如果下标到达数组顶部的话,让front指向数组的第一个位置  
  29.         if (front == maxSize)  
  30.             front = 0;  
  31.         nItems--;  
  32.         return temp;  
  33.     }  
  34.   
  35.     public int peekFront(){// 只是返回最前面那个元素的值,并不删除      
  36.         return queArr[front];  
  37.     }  
  38.   
  39.     public boolean isEmpty() {  
  40.         return (nItems == 0);  
  41.     }  
  42.   
  43.     public boolean isFull() {  
  44.         return (nItems == maxSize);  
  45.     }  
  46.   
  47.     public int size() {  
  48.         return nItems;  
  49.     }  
  50.   
  51. // end class Queue  
  52.   
  53. class QueueApp {  
  54.     public static void main(String[] args) {  
  55.         Queue theQueue = new Queue(5);  
  56.         theQueue.insert(10); // 插入4个数据  
  57.         theQueue.insert(20);  
  58.         theQueue.insert(30);  
  59.         theQueue.insert(40);  
  60.         theQueue.remove(); // 删除(10, 20, 30)  
  61.         theQueue.remove();  
  62.         theQueue.remove();  
  63.         theQueue.insert(50); // 再插入4个数据  
  64.         theQueue.insert(60);  
  65.         theQueue.insert(70);  
  66.         theQueue.insert(80);  
  67.         while (!theQueue.isEmpty())  
  68.             // 取出所有数据  
  69.             System.out.println(theQueue.remove());  
  70.     }   
  71. }   

运行的结果如图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值