如何用数组实现队列和栈?

用数组结构实现大小固定的栈和队列,这是一个面试的常考题目,也是一个比较简单的题目。

1.实现栈结构:栈结构是先进后出的,只需要一个数组和一个记录位置的变量size,当进来一个元素,size就++,出去一个元素size就–。

2.实现队列结构:相对栈结构要难搞一些,队列的先进先出的,需要一个数组和三个变量,size记录已经进来了多少个元素,in记录刚进来的元素应该放在哪个位置,out表示用户要求弹出的元素所在的位置。size的作用不止于此,它还是in与out 的操作的关键信息,使得in与out解耦,避免的很多的麻烦,好像书本讲的是没有size这个变量的。当in或者out达到底部的时候就跳回0处。


    
    
  1. /**
  2. * 固定数组实现一个队列
  3. * 队列先进先出,方法有push,pull,peek
  4. */
  5. public class C02_ArrayToQueue {
  6. public static class MyQueue{
  7. private int out; //新进来的数 放这
  8. private int in; //用户要求弹出的数
  9. private int size; //已经进队列的个数
  10. private int arr[];
  11. public MyQueue(int iniSize){
  12. arr = new int[iniSize];
  13. size = 0;
  14. in = 0;
  15. out = 0;
  16. }
  17. public void push(int num){
  18. if(size==arr.length){
  19. throw new RuntimeException( “the queue is full!”);
  20. }
  21. size++; //大小扩展一个
  22. arr[in] = num; //赋值
  23. in = in==arr.length- 1 ? 0 : in+ 1; //如果已经到达数组末尾就重新等于0
  24. }
  25. public int pull(){
  26. if(size== 0){
  27. throw new RuntimeException( “the queue is empty!”);
  28. }
  29. size–;
  30. int t = out; //记录
  31. out = out==arr.length- 1 ? 0 : out+ 1; //如果已经到达数组末尾就重新等于0
  32. return arr[t];
  33. }
  34. public int peek(){
  35. if(size== 0){
  36. throw new RuntimeException( “the queue is empty!”);
  37. }
  38. return arr[out];
  39. }
  40. }
  41. public static void main(String[] args) {
  42. int iniSize = 3;
  43. MyQueue myQueue = new MyQueue(iniSize);
  44. myQueue.push( 12);
  45. myQueue.push( 13);
  46. myQueue.push( 15);
  47. System.out.println(myQueue.pull());
  48. System.out.println(myQueue.pull());
  49. System.out.println(myQueue.pull());
  50. myQueue.push( 23);
  51. myQueue.push( 24);
  52. System.out.println(myQueue.pull());
  53. System.out.println(myQueue.peek());
  54. }
  55. }


    
    
  1. /**
  2. * 固定数组实现栈结构
  3. * 实现方法push,pop,peek
  4. * 当越界的时候抛出一个运行时异常
  5. * 简单面试题
  6. */
  7. public class C01_ArrayToStack {
  8. public static class MyStack{
  9. private int size; //指针位置,也表示栈已经压了多少
  10. private int[]arr;
  11. MyStack( int iniSize){ //构造方法初始化数组
  12. arr = new int[iniSize];
  13. size = 0;
  14. }
  15. public void push(int num){
  16. if(size == arr.length){
  17. throw new RuntimeException( "栈下标越界!");
  18. }
  19. arr[size++] = num;
  20. }
  21. public int pop(){
  22. if(size == 0){
  23. throw new RuntimeException( "栈中已经没有元素可以弹出!");
  24. }
  25. return arr[--size];
  26. }
  27. public int peek(){
  28. if(size == 0){
  29. throw new RuntimeException( "栈中已经没有元素可以弹出!");
  30. }
  31. return arr[size];
  32. }
  33. }
  34. public static void main(String[] args) {
  35. int len = 13;
  36. MyStack myStack = new MyStack(len);
  37. for ( int i = 0; i < len; i++) {
  38. myStack.push(i);
  39. }
  40. for ( int i = 0; i < len; i++) {
  41. System.out.println(myStack.pop());
  42. }
  43. }
  44. }

用数组结构实现大小固定的栈和队列,这是一个面试的常考题目,也是一个比较简单的题目。

1.实现栈结构:栈结构是先进后出的,只需要一个数组和一个记录位置的变量size,当进来一个元素,size就++,出去一个元素size就–。

2.实现队列结构:相对栈结构要难搞一些,队列的先进先出的,需要一个数组和三个变量,size记录已经进来了多少个元素,in记录刚进来的元素应该放在哪个位置,out表示用户要求弹出的元素所在的位置。size的作用不止于此,它还是in与out 的操作的关键信息,使得in与out解耦,避免的很多的麻烦,好像书本讲的是没有size这个变量的。当in或者out达到底部的时候就跳回0处。


  
  
  1. /**
  2. * 固定数组实现一个队列
  3. * 队列先进先出,方法有push,pull,peek
  4. */
  5. public class C02_ArrayToQueue {
  6. public static class MyQueue{
  7. private int out; //新进来的数 放这
  8. private int in; //用户要求弹出的数
  9. private int size; //已经进队列的个数
  10. private int arr[];
  11. public MyQueue(int iniSize){
  12. arr = new int[iniSize];
  13. size = 0;
  14. in = 0;
  15. out = 0;
  16. }
  17. public void push(int num){
  18. if(size==arr.length){
  19. throw new RuntimeException( “the queue is full!”);
  20. }
  21. size++; //大小扩展一个
  22. arr[in] = num; //赋值
  23. in = in==arr.length- 1 ? 0 : in+ 1; //如果已经到达数组末尾就重新等于0
  24. }
  25. public int pull(){
  26. if(size== 0){
  27. throw new RuntimeException( “the queue is empty!”);
  28. }
  29. size–;
  30. int t = out; //记录
  31. out = out==arr.length- 1 ? 0 : out+ 1; //如果已经到达数组末尾就重新等于0
  32. return arr[t];
  33. }
  34. public int peek(){
  35. if(size== 0){
  36. throw new RuntimeException( “the queue is empty!”);
  37. }
  38. return arr[out];
  39. }
  40. }
  41. public static void main(String[] args) {
  42. int iniSize = 3;
  43. MyQueue myQueue = new MyQueue(iniSize);
  44. myQueue.push( 12);
  45. myQueue.push( 13);
  46. myQueue.push( 15);
  47. System.out.println(myQueue.pull());
  48. System.out.println(myQueue.pull());
  49. System.out.println(myQueue.pull());
  50. myQueue.push( 23);
  51. myQueue.push( 24);
  52. System.out.println(myQueue.pull());
  53. System.out.println(myQueue.peek());
  54. }
  55. }


  
  
  1. /**
  2. * 固定数组实现栈结构
  3. * 实现方法push,pop,peek
  4. * 当越界的时候抛出一个运行时异常
  5. * 简单面试题
  6. */
  7. public class C01_ArrayToStack {
  8. public static class MyStack{
  9. private int size; //指针位置,也表示栈已经压了多少
  10. private int[]arr;
  11. MyStack( int iniSize){ //构造方法初始化数组
  12. arr = new int[iniSize];
  13. size = 0;
  14. }
  15. public void push(int num){
  16. if(size == arr.length){
  17. throw new RuntimeException( "栈下标越界!");
  18. }
  19. arr[size++] = num;
  20. }
  21. public int pop(){
  22. if(size == 0){
  23. throw new RuntimeException( "栈中已经没有元素可以弹出!");
  24. }
  25. return arr[--size];
  26. }
  27. public int peek(){
  28. if(size == 0){
  29. throw new RuntimeException( "栈中已经没有元素可以弹出!");
  30. }
  31. return arr[size];
  32. }
  33. }
  34. public static void main(String[] args) {
  35. int len = 13;
  36. MyStack myStack = new MyStack(len);
  37. for ( int i = 0; i < len; i++) {
  38. myStack.push(i);
  39. }
  40. for ( int i = 0; i < len; i++) {
  41. System.out.println(myStack.pop());
  42. }
  43. }
  44. }

猜你喜欢

转载自blog.csdn.net/houguofei123/article/details/81011154