队列和栈【基础算法题】

题目一

用数组结构实现大小固定的对列和栈。

1 队列

设置start和end变量,初始都指向0位置。size变量约束start和end的行为。用户要设置队列的长度initialSize。

size是随着数的加入和弹出动态增减的~初始是0。只要不超过数组的initialSize(不小于0)就可以不断增(减)队列中的数,否则报错。

end:如果新加一个数,填到的位置。只要size小于initialSize,就可以将新加的数放到end所在位置。end=initialSize-1时,会跳回0位置。否则end++,size+1。

start:如果要取一个数,从哪个位置取。只要size大于0,就可以将start所在位置的数返回给用户。start=initialSize-1时,会跳回0位置。否则start++,size-1。

2 栈

size为栈的大小。设置一个index表示当前所在位置。

压栈数存到index当前位置且index++。

出栈取出index当前位置的数且index--。

如果index=size表示栈满,再压栈会报错。

如果index=0表示栈空,再出栈会报错。

代码实现

 1 package class_03;
 2 
 3 public class Code_01_Array_To_Stack_Queue {
 4 
 5     public static class ArrayStack {
 6         private Integer[] arr;
 7         private Integer size;
 8 
 9         public ArrayStack(int initSize) {
10             if (initSize < 0) {
11                 throw new IllegalArgumentException("The init size is less than 0");
12             }
13             arr = new Integer[initSize];
14             size = 0;
15         }
16 
17         public Integer peek() {
18             if (size == 0) {
19                 return null;
20             }
21             return arr[size - 1];
22         }
23 
24         public void push(int obj) {
25             if (size == arr.length) {
26                 throw new ArrayIndexOutOfBoundsException("The queue is full");
27             }
28             arr[size++] = obj;
29         }
30 
31         public Integer pop() {
32             if (size == 0) {
33                 throw new ArrayIndexOutOfBoundsException("The queue is empty");
34             }
35             return arr[--size];
36         }
37     }
38 
39     public static class ArrayQueue {
40         private Integer[] arr;
41         private Integer size;
42         private Integer first;
43         private Integer last;
44 
45         public ArrayQueue(int initSize) {
46             if (initSize < 0) {
47                 throw new IllegalArgumentException("The init size is less than 0");
48             }
49             arr = new Integer[initSize];
50             size = 0;
51             first = 0;
52             last = 0;
53         }
54 
55         public Integer peek() {
56             if (size == 0) {
57                 return null;
58             }
59             return arr[first];
60         }
61 
62         public void push(int obj) {
63             if (size == arr.length) {
64                 throw new ArrayIndexOutOfBoundsException("The queue is full");
65             }
66             size++;
67             arr[last] = obj;
68             last = last == arr.length - 1 ? 0 : last + 1;
69         }
70 
71         public Integer poll() {
72             if (size == 0) {
73                 throw new ArrayIndexOutOfBoundsException("The queue is empty");
74             }
75             size--;
76             int tmp = first;
77             first = first == arr.length - 1 ? 0 : first + 1;
78             return arr[tmp];
79         }
80     }
81 
82     public static void main(String[] args) {
83 
84     }
85 
86 }

题目二

思路

准备两个栈,一个Data栈,一个min栈。

第一个数开始进Data栈时,同时压入min栈。

第二个数进Data栈时,将Data栈顶元素d与min栈顶元素m比较,如果Data栈顶元素d小,则在min栈中压入d,如果min栈顶元素m小,则在min栈中压入m,···以此类推,min栈栈顶元素就是当前Data栈中最小元素。

Data栈中的元素要出栈时,min栈中对应位置的元素也出栈。

    👉   

 代码实现

  1 package class_03;
  2 
  3 import java.util.Stack;
  4 
  5 public class Code_02_GetMinStack {
  6     public static class MyStack1 {
  7         private Stack<Integer> stackData;
  8         private Stack<Integer> stackMin;
  9 
 10         public MyStack1() {
 11             this.stackData = new Stack<Integer>();
 12             this.stackMin = new Stack<Integer>();
 13         }
 14 
 15         public void push(int newNum) {
 16             if (this.stackMin.isEmpty()) {
 17                 this.stackMin.push(newNum);
 18             } else if (newNum <= this.getmin()) {
 19                 this.stackMin.push(newNum);
 20             }
 21             this.stackData.push(newNum);
 22         }
 23 
 24         public int pop() {
 25             if (this.stackData.isEmpty()) {
 26                 throw new RuntimeException("Your stack is empty.");
 27             }
 28             int value = this.stackData.pop();
 29             if (value == this.getmin()) {
 30                 this.stackMin.pop();
 31             }
 32             return value;
 33         }
 34 
 35         public int getmin() {
 36             if (this.stackMin.isEmpty()) {
 37                 throw new RuntimeException("Your stack is empty.");
 38             }
 39             return this.stackMin.peek();
 40         }
 41     }
 42 
 43     public static class MyStack2 {
 44         private Stack<Integer> stackData;
 45         private Stack<Integer> stackMin;
 46 
 47         public MyStack2() {
 48             this.stackData = new Stack<Integer>();
 49             this.stackMin = new Stack<Integer>();
 50         }
 51 
 52         public void push(int newNum) {
 53             if (this.stackMin.isEmpty()) {
 54                 this.stackMin.push(newNum);
 55             } else if (newNum < this.getmin()) {
 56                 this.stackMin.push(newNum);
 57             } else {
 58                 int newMin = this.stackMin.peek();
 59                 this.stackMin.push(newMin);
 60             }
 61             this.stackData.push(newNum);
 62         }
 63 
 64         public int pop() {
 65             if (this.stackData.isEmpty()) {
 66                 throw new RuntimeException("Your stack is empty.");
 67             }
 68             this.stackMin.pop();
 69             return this.stackData.pop();
 70         }
 71 
 72         public int getmin() {
 73             if (this.stackMin.isEmpty()) {
 74                 throw new RuntimeException("Your stack is empty.");
 75             }
 76             return this.stackMin.peek();
 77         }
 78     }
 79 
 80     public static void main(String[] args) {
 81         MyStack1 stack1 = new MyStack1();
 82         stack1.push(3);
 83         System.out.println(stack1.getmin());
 84         stack1.push(4);
 85         System.out.println(stack1.getmin());
 86         stack1.push(1);
 87         System.out.println(stack1.getmin());
 88         System.out.println(stack1.pop());
 89         System.out.println(stack1.getmin());
 90 
 91         System.out.println("=============");
 92 
 93         MyStack1 stack2 = new MyStack1();
 94         stack2.push(3);
 95         System.out.println(stack2.getmin());
 96         stack2.push(4);
 97         System.out.println(stack2.getmin());
 98         stack2.push(1);
 99         System.out.println(stack2.getmin());
100         System.out.println(stack2.pop());
101         System.out.println(stack2.getmin());
102     }
103 
104 }

题目三

工程上不会这么写,但考试会考。

1 如何仅用队列结构实现栈结构?

准备两个队列,来回倒。

进队的时候都进入第一个队列。

想出队时,两个队列来回倒,出几个就倒几次,每次倒完两个队列要换一下引用,

第一个队列中先进的前n-1个数,放入第二的队列中,第一个队列中剩下的一个数返回给用户,

第二个队列中先进的前n-1个数,放入第一的队列中,第二个队列中剩下的一个数返回给用户,

·····以此类推

2 如何仅用栈结构实现队列结构?

准备两个栈结构,一个push栈,一个pop栈。

用户新给的数永远进push栈,用户想取一个数永远从pop栈中拿。

将push栈中的数依次压入pop栈,从pop栈中拿数。

向pop栈压栈的时候有两个限制:

①如果push决定要向pop中倒数,一次要全部倒完!

②如果pop栈里有东西,push栈不能向pop栈里倒数!

     👉    

代码实现

 1 package class_03;
 2 
 3 import java.util.LinkedList;
 4 import java.util.Queue;
 5 import java.util.Stack;
 6 
 7 public class Code_03_StackAndQueueConvert {
 8 
 9     public static class TwoStacksQueue {
10         private Stack<Integer> stackPush;
11         private Stack<Integer> stackPop;
12 
13         public TwoStacksQueue() {
14             stackPush = new Stack<Integer>();
15             stackPop = new Stack<Integer>();
16         }
17 
18         public void push(int pushInt) {
19             stackPush.push(pushInt);
20         }
21 
22         public int poll() {
23             if (stackPop.empty() && stackPush.empty()) {
24                 throw new RuntimeException("Queue is empty!");
25             } else if (stackPop.empty()) {
26                 while (!stackPush.empty()) {
27                     stackPop.push(stackPush.pop());
28                 }
29             }
30             return stackPop.pop();
31         }
32 
33         public int peek() {
34             if (stackPop.empty() && stackPush.empty()) {
35                 throw new RuntimeException("Queue is empty!");
36             } else if (stackPop.empty()) {
37                 while (!stackPush.empty()) {
38                     stackPop.push(stackPush.pop());
39                 }
40             }
41             return stackPop.peek();
42         }
43     }
44 
45     public static class TwoQueuesStack {
46         private Queue<Integer> queue;
47         private Queue<Integer> help;
48 
49         public TwoQueuesStack() {
50             queue = new LinkedList<Integer>();
51             help = new LinkedList<Integer>();
52         }
53 
54         public void push(int pushInt) {
55             queue.add(pushInt);
56         }
57 
58         public int peek() {
59             if (queue.isEmpty()) {
60                 throw new RuntimeException("Stack is empty!");
61             }
62             while (queue.size() != 1) {
63                 help.add(queue.poll());
64             }
65             int res = queue.poll();
66             help.add(res);
67             swap();
68             return res;
69         }
70 
71         public int pop() {
72             if (queue.isEmpty()) {
73                 throw new RuntimeException("Stack is empty!");
74             }
75             while (queue.size() > 1) {
76                 help.add(queue.poll());
77             }
78             int res = queue.poll();
79             swap();
80             return res;
81         }
82 
83         private void swap() {
84             Queue<Integer> tmp = help;
85             help = queue;
86             queue = tmp;
87         }
88 
89     }
90 
91 }

题目四

 思路:

准备两个队列,狗队列和猫队列,一只宠物要进队,然后先判断是猫是狗,是猫就进入猫队列,是狗就进入狗队列,打一个“时间戳”。

代码实现:

  1 package class_03;
  2 
  3 import java.util.LinkedList;
  4 import java.util.Queue;
  5 
  6 public class Code_04_DogCatQueue {
  7 
  8     public static class Pet {
  9         private String type;
 10 
 11         public Pet(String type) {
 12             this.type = type;
 13         }
 14 
 15         public String getPetType() {
 16             return this.type;
 17         }
 18     }
 19 
 20     public static class Dog extends Pet {
 21         public Dog() {
 22             super("dog");
 23         }
 24     }
 25 
 26     public static class Cat extends Pet {
 27         public Cat() {
 28             super("cat");
 29         }
 30     }
 31 
 32     public static class PetEnterQueue {
 33         private Pet pet;
 34         private long count;
 35 
 36         public PetEnterQueue(Pet pet, long count) {
 37             this.pet = pet;
 38             this.count = count;
 39         }
 40 
 41         public Pet getPet() {
 42             return this.pet;
 43         }
 44 
 45         public long getCount() {
 46             return this.count;
 47         }
 48 
 49         public String getEnterPetType() {
 50             return this.pet.getPetType();
 51         }
 52     }
 53 
 54     public static class DogCatQueue {
 55         private Queue<PetEnterQueue> dogQ;
 56         private Queue<PetEnterQueue> catQ;
 57         private long count;
 58 
 59         public DogCatQueue() {
 60             this.dogQ = new LinkedList<PetEnterQueue>();
 61             this.catQ = new LinkedList<PetEnterQueue>();
 62             this.count = 0;
 63         }
 64 
 65         public void add(Pet pet) {
 66             if (pet.getPetType().equals("dog")) {
 67                 this.dogQ.add(new PetEnterQueue(pet, this.count++));
 68             } else if (pet.getPetType().equals("cat")) {
 69                 this.catQ.add(new PetEnterQueue(pet, this.count++));
 70             } else {
 71                 throw new RuntimeException("err, not dog or cat");
 72             }
 73         }
 74 
 75         public Pet pollAll() {
 76             if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) {
 77                 if (this.dogQ.peek().getCount() < this.catQ.peek().getCount()) {
 78                     return this.dogQ.poll().getPet();
 79                 } else {
 80                     return this.catQ.poll().getPet();
 81                 }
 82             } else if (!this.dogQ.isEmpty()) {
 83                 return this.dogQ.poll().getPet();
 84             } else if (!this.catQ.isEmpty()) {
 85                 return this.catQ.poll().getPet();
 86             } else {
 87                 throw new RuntimeException("err, queue is empty!");
 88             }
 89         }
 90 
 91         public Dog pollDog() {
 92             if (!this.isDogQueueEmpty()) {
 93                 return (Dog) this.dogQ.poll().getPet();
 94             } else {
 95                 throw new RuntimeException("Dog queue is empty!");
 96             }
 97         }
 98 
 99         public Cat pollCat() {
100             if (!this.isCatQueueEmpty()) {
101                 return (Cat) this.catQ.poll().getPet();
102             } else
103                 throw new RuntimeException("Cat queue is empty!");
104         }
105 
106         public boolean isEmpty() {
107             return this.dogQ.isEmpty() && this.catQ.isEmpty();
108         }
109 
110         public boolean isDogQueueEmpty() {
111             return this.dogQ.isEmpty();
112         }
113 
114         public boolean isCatQueueEmpty() {
115             return this.catQ.isEmpty();
116         }
117 
118     }
119 
120     public static void main(String[] args) {
121         DogCatQueue test = new DogCatQueue();
122 
123         Pet dog1 = new Dog();
124         Pet cat1 = new Cat();
125         Pet dog2 = new Dog();
126         Pet cat2 = new Cat();
127         Pet dog3 = new Dog();
128         Pet cat3 = new Cat();
129 
130         test.add(dog1);
131         test.add(cat1);
132         test.add(dog2);
133         test.add(cat2);
134         test.add(dog3);
135         test.add(cat3);
136 
137         test.add(dog1);
138         test.add(cat1);
139         test.add(dog2);
140         test.add(cat2);
141         test.add(dog3);
142         test.add(cat3);
143 
144         test.add(dog1);
145         test.add(cat1);
146         test.add(dog2);
147         test.add(cat2);
148         test.add(dog3);
149         test.add(cat3);
150         while (!test.isDogQueueEmpty()) {
151             System.out.println(test.pollDog().getPetType());
152         }
153         while (!test.isEmpty()) {
154             System.out.println(test.pollAll().getPetType());
155         }
156     }
157 
158 }

 

posted @ 2020-02-11 15:04  阿江是个程序猿  阅读(367)  评论(0编辑  收藏  举报