mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Update the structure of the chapter
of binary tree.
This commit is contained in:
@ -64,13 +64,6 @@ public:
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
int get(int index) {
|
||||
if (index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
return nums[(front + index) % capacity()];
|
||||
}
|
||||
|
||||
/* 将数组转化为 Vector 并返回 */
|
||||
vector<int> toVector() {
|
||||
int siz = size();
|
||||
@ -103,11 +96,7 @@ int main() {
|
||||
/* 访问队首元素 */
|
||||
int peek = queue->peek();
|
||||
cout << "队首元素 peek = " << peek << endl;
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
int num = queue->get(2);
|
||||
cout << "队列第 3 个元素为 num = " << num << endl;
|
||||
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue->poll();
|
||||
cout << "出队元素 poll = " << poll << ",出队后 queue = ";
|
||||
|
||||
@ -41,13 +41,6 @@ public:
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int get(int index) {
|
||||
if(index >= size())
|
||||
throw out_of_range("索引越界");
|
||||
return stack[index];
|
||||
}
|
||||
|
||||
/* 返回 Vector */
|
||||
vector<int> toVector() {
|
||||
return stack;
|
||||
@ -73,10 +66,6 @@ int main() {
|
||||
int top = stack->top();
|
||||
cout << "栈顶元素 top = " << top << endl;
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int num = stack->get(3);
|
||||
cout << "栈索引 3 处的元素为 num = " << num << endl;
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack->pop();
|
||||
cout << "出栈元素 pop = " << pop << ",出栈后 stack = ";
|
||||
|
||||
@ -63,13 +63,6 @@ class ArrayQueue {
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int get(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return nums[(front + index) % capacity()];
|
||||
}
|
||||
|
||||
/* 返回数组 */
|
||||
public int[] toArray() {
|
||||
int size = size();
|
||||
|
||||
@ -45,13 +45,6 @@ class ArrayStack {
|
||||
return stack.get(size() - 1);
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
public int get(int index) {
|
||||
if (index >= size())
|
||||
throw new IndexOutOfBoundsException();
|
||||
return stack.get(index);
|
||||
}
|
||||
|
||||
/* 将 List 转化为 Array 并返回 */
|
||||
public Object[] toArray() {
|
||||
return stack.toArray();
|
||||
@ -75,10 +68,6 @@ public class array_stack {
|
||||
int peek = stack.peek();
|
||||
System.out.println("栈顶元素 peek = " + peek);
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
int num = stack.get(3);
|
||||
System.out.println("栈索引 3 处的元素为 num = " + num);
|
||||
|
||||
/* 元素出栈 */
|
||||
int pop = stack.pop();
|
||||
System.out.println("出栈元素 pop = " + pop + ",出栈后 stack = " + Arrays.toString(stack.toArray()));
|
||||
|
||||
@ -56,13 +56,6 @@ class ArrayQueue {
|
||||
return this.#queue[this.#front];
|
||||
}
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
get(index) {
|
||||
if (index >= this.size)
|
||||
throw new Error("索引越界");
|
||||
return this.#queue[(this.#front + index) % this.capacity];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
const siz = this.size;
|
||||
@ -95,10 +88,6 @@ console.log(queue.toArray());
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
const num = queue.get(2);
|
||||
console.log("队列第 3 个元素为 num = " + num);
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.poll();
|
||||
console.log("出队元素 poll = " + poll + ",出队后 queue = ");
|
||||
|
||||
@ -41,13 +41,6 @@ class ArrayStack {
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index) {
|
||||
if (index >= this.size)
|
||||
throw new Error("索引越界");
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
@ -73,10 +66,6 @@ console.log(stack.toArray());
|
||||
const top = stack.top();
|
||||
console.log("栈顶元素 top = " + top);
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
const num = stack.get(3);
|
||||
console.log("栈索引 3 处的元素为 num = " + num);
|
||||
|
||||
/* 元素出栈 */
|
||||
const pop = stack.pop();
|
||||
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
|
||||
|
||||
@ -54,13 +54,6 @@ class ArrayQueue:
|
||||
return False
|
||||
return self.__nums[self.__front]
|
||||
|
||||
""" 访问指定位置元素 """
|
||||
def get(self, index):
|
||||
if index >= self.size():
|
||||
print("索引越界")
|
||||
return False
|
||||
return self.__nums[(self.__front + index) % self.capacity()]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
res = [0] * self.size()
|
||||
@ -88,10 +81,6 @@ if __name__ == "__main__":
|
||||
peek = queue.peek()
|
||||
print("队首元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = queue.get(3)
|
||||
print("队列索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出队 """
|
||||
poll = queue.poll()
|
||||
print("出队元素 poll =", poll)
|
||||
|
||||
@ -34,11 +34,6 @@ class ArrayStack:
|
||||
def peek(self):
|
||||
assert not self.is_empty(), "栈为空"
|
||||
return self.__stack[-1]
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
def get(self, index):
|
||||
assert index < self.size(), "索引越界"
|
||||
return self.__stack[index]
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
@ -62,10 +57,6 @@ if __name__ == "__main__":
|
||||
peek = stack.peek()
|
||||
print("栈顶元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = stack.get(3)
|
||||
print("栈索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出栈 """
|
||||
pop = stack.pop()
|
||||
print("出栈元素 pop =", pop)
|
||||
|
||||
@ -57,13 +57,6 @@ class ArrayQueue {
|
||||
return this.queue[this.front];
|
||||
}
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
get(index: number): number {
|
||||
if (index >= this.size)
|
||||
throw new Error("索引越界");
|
||||
return this.queue[(this.front + index) % this.capacity];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray(): number[] {
|
||||
const siz = this.size;
|
||||
@ -94,10 +87,6 @@ console.log(queue.toArray());
|
||||
const peek = queue.peek();
|
||||
console.log("队首元素 peek = " + peek);
|
||||
|
||||
/* 访问指定索引元素 */
|
||||
const num = queue.get(2);
|
||||
console.log("队列第 3 个元素为 num = " + num);
|
||||
|
||||
/* 元素出队 */
|
||||
const poll = queue.poll();
|
||||
console.log("出队元素 poll = " + poll + ",出队后 queue = ");
|
||||
|
||||
@ -41,13 +41,6 @@ class ArrayStack {
|
||||
return this.stack[this.stack.length - 1];
|
||||
}
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
get(index: number): number | undefined {
|
||||
if (index >= this.size)
|
||||
throw new Error('索引越界');
|
||||
return this.stack[index];
|
||||
}
|
||||
|
||||
/* 返回 Array */
|
||||
toArray() {
|
||||
return this.stack;
|
||||
@ -73,10 +66,6 @@ console.log(stack.toArray());
|
||||
const top = stack.top();
|
||||
console.log("栈顶元素 top = " + top);
|
||||
|
||||
/* 访问索引 index 处元素 */
|
||||
const num = stack.get(3);
|
||||
console.log("栈索引 3 处的元素为 num = " + num);
|
||||
|
||||
/* 元素出栈 */
|
||||
const pop = stack.pop();
|
||||
console.log("出栈元素 pop = " + pop + ",出栈后 stack = ");
|
||||
|
||||
Reference in New Issue
Block a user