第三章 栈
3.1 栈概念
栈 (stack),有些地方称为堆栈,是一种容器,可存入数据元素、访问元素、删除元素,它的特点在于只能允许在容器的一端(称为栈顶端指标,英语:top)进行加入数据(英语:push)和输出数据(英语:pop)的运算。没有了位置概念,保证任何时候可以访问、删除的元素都是此前最后存入的那个元素,确定了一种默认的访问顺序。
由于栈数据结构只允许在一端进行操作,因而按照后进先出的原理运作。
栈可以用顺序表实现,也可以用链表实现。
3.2 栈的操作
Stack() 创建一个新的空栈 push(element) 添加一个新的元素element到栈顶 pop() 取出栈顶元素 peek() 返回栈顶元素 is_empty() 判断栈是否为空 size() 返回栈的元素个数
实现类 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package mystack;
public
class
MyStack {
//栈的底层使用数组来存储数据
//private int[] elements;
int[] elements;
//测试时使用
public
MyStack() {
elements =
new
int[0];
}
//添加元素
public
void push(int element) {
//创建一个新的数组
int[] newArr =
new
int[elements.length + 1];
//把原数组中的元素复制到新数组中
for
(int i = 0; i < elements.length; i++) {
newArr[i] = elements[i];
}
//把添加的元素放入新数组中
newArr[elements.length] = element;
//使用新数组替换旧数组
elements = newArr;
}
//取出栈顶元素
public
int pop() {
//当栈中没有元素
if
(is_empty()) {
throw
new
RuntimeException(
"栈空"
);
}
//取出数组的最后一个元素
int element = elements[elements.length - 1];
//创建一个新数组
int[] newArr =
new
int[elements.length - 1];
//原数组中除了最后一个元素其他元素放入新数组
for
(int i = 0; i < elements.length - 1; i++) {
newArr[i] = elements[i];
}
elements = newArr;
return
element;
}
//查看栈顶元素
public
int peek() {
return
elements[elements.length - 1];
}
//判断栈是否为空
public
boolean is_empty() {
return
elements.length == 0;
}
//查看栈的元素个数
public
int size() {
return
elements.length;
}}
测试类 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package mystack;
public
class
Demo {
public
static
void main(String[] args) {
MyStack ms =
new
MyStack();
//添加元素
ms.push(9);
ms.push(8);
ms.push(7);
//取出栈顶元素// System.out.println(ms.pop()); //7// System.out.println(ms.pop()); //8// System.out.println(ms.pop()); //9
//查看栈顶元素
System.out.println(ms.peek());
//7
System.out.println(ms.peek());
//7
//查看栈中元素个数
System.out.println(ms.size());
//3
}}
第4章 队列
4.1 队列概念
队列 (Queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。
队列是一种先进先出的(First In First Out)的线性表,简称FIFO。允许插入的一端为队尾,允许删除的一端为队头。队列不允许在中间部位进行操作!假设队列是q=(a1,a2,……,an),那么a1就是队头元素,而an是队尾元素。这样我们就可以删除时,总是从a1开始,而插入时,总是在队列最后。这也比较符合我们通常生活中的习惯,排在第一个的优先出列,最后来的当然排在队伍最后。
同栈一样,队列也可以用顺序表或者链表实现。
4.2 队列的操作
Queue() 创建一个空的队列 enqueue(element) 往队列中添加一个element元素 dequeue() 从队列头部删除一个元素 is_empty() 判断一个队列是否为空 size() 返回队列的大小
实现类 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public
class
MyQueue {
int[] elements;
public
MyQueue() {
elements =
new
int[0];
}
//入队
public
void enqueue(int element) {
//创建一个新的数组
int[] newArr =
new
int[elements.length + 1];
//把原数组中的元素复制到新数组中
for
(int i = 0; i < elements.length; i++) {
newArr[i] = elements[i];
}
//把添加的元素放入新数组中
newArr[elements.length] = element;
//使用新数组替换旧数组
elements = newArr;
}
//出队
public
int dequeue() {
if
(isEmpty()) {
throw
new
RuntimeException(
"队空,无数据"
);
}
//把数组中第1个元素取出来
int element = elements[0];
//创建一个新数组
int[] newArr =
new
int[elements.length - 1];
//把原数组除了第一个数据,其他存入新数组
for
(int i = 0; i < elements.length; i++) {
newArr[i] = elements[i + 1];
}
//新数组替换旧数组
elements = newArr;
return
element;
}
//判断是否队空
public
boolean isEmpty() {
return
elements.length==0;
}
//获取队列长度
public
int size() {
return
elements.length;
}}
测试类 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public
class
Demo {
public
static
void main(String[] args) {
MyQueue mq =
new
MyQueue();
//入队
mq.enqueue(1);
mq.enqueue(2);
mq.enqueue(3);
//出队
System.out.println(mq.dequeue());
//1
System.out.println(mq.dequeue());
//2
System.out.println(mq.dequeue());
//3
}}