版权声明:无意呢。 https://blog.csdn.net/qq_41900081/article/details/86553122
定义
栈是一个有序线性表,只能在表的一端(称为栈顶,top)执行插入和删除操作,最后插入的元素第一个被删除,是一种后进先出(FILO)的线性表
栈的操作
1. 主要操作
- void push(int data):入栈,将数据插入栈。
- int pop():出栈,删除并返回最后一个插入栈的元素
2.辅助操作
- int pop():返回最后一个插入栈的元素,但不删除。
- int size():返回存储在栈中元素的个数。
- int isEmpty():判断栈中是否有元素。
- int isStackFull():判断栈中是否存满元素。
3.三种实现方式
- 基于简单数组的实现
- 基于动态数组的实现
- 基于链表的实现
4.基于数组实现和基于链表实现的比较
a)基于数组的实现
- 各个操作都是常数时间开销
- 每隔一段时间倍增操作的开销较大
- n个操作的任意序列的平摊时间开销为O(n)
b)基于链表的实现
- 栈规模的增加和减小都很简洁
- 各个操作都是常数时间开销
- 每个操作都要使用额外的空间开销和时间开销来处理指针
5.简单数组实现栈
栈的大小是固定的,当栈满了之后无法继续执行入栈操作
package dataStructure;
public class ArrayStack {
private int top;
private int[] array;
private int capacity;
public ArrayStack(int num) {
top = -1;
capacity = num;
array = new int[capacity];
}
boolean isEmpty() {
return top == -1;
}
int size() {
if(isEmpty()) return 0;
return top+1;
}
int top() {
if(isEmpty()) {
System.out.println("栈空");
new Exception();
}
return array[top];
}
boolean isStackFull() {
return (top == capacity - 1);
}
void push(int d) {
if(isStackFull()) {
System.out.println(d + "插入失败,原因:栈满");
}else {
array[++top] = d;
}
}
int pop() {
if(isEmpty()) {
System.out.println("栈空");
return -1;
}
return array[top--];
}
void display() {
if(isEmpty()) {
System.out.println("栈空");
}else {
System.out.println("栈内元素:");
for(int i = 0; i < size(); i++) {
System.out.print(array[i] + " ");
}
}
}
void deleteStack(){
top = -1;
}
public static void main(String[] args) {
ArrayStack as = new ArrayStack(3);
as.push(2);
as.push(3);
as.push(1);
as.pop();
as.push(4);
as.push(5);
as.display();
System.out.println("\n栈顶元素:" + as.pop());
System.out.println("栈的大小:" + as.size());
}
}
6.动态数组实现栈
当预设数组大小满了之后,将新建数组为原数组的两倍大小,然后将原数组复制到新数组里面
package dataStructure;
public class DynArrayStack {
private int top;
private int[] array;
private int capacity;
public DynArrayStack(int num) {
top = -1;
capacity = num;
array = new int[capacity];
}
boolean isEmpty() {
return top == -1;
}
int size() {
if(isEmpty()) return 0;
return top+1;
}
int top() {
if(isEmpty()) {
System.out.println("栈空");
new Exception();
}
return array[top];
}
boolean isStackFull() {
return (top == capacity - 1);
}
void push(int d) {
if(isStackFull())
doubleArray();
array[++top] = d;
}
void doubleArray() {
int[] newArray = new int[capacity * 2];
System.arraycopy(array, 0, newArray, 0, capacity);
array = newArray;
capacity = 2 * capacity;
}
int pop() {
if(isEmpty()) {
System.out.println("栈空");
return -1;
}
return array[top--];
}
void display() {
if(isEmpty()) {
System.out.println("栈空");
}else {
System.out.print("栈内元素:");
for(int i = 0; i < size(); i++) {
System.out.print(array[i] + " ");
}
}
}
void deleteStack() {
top = -1;
}
public static void main(String[] args) {
DynArrayStack as = new DynArrayStack(3);
as.push(2);
as.push(3);
as.push(1);
as.pop();//移除1
as.push(4);
as.push(5);
as.push(6);
as.push(7);
as.display();
System.out.println("\n栈顶元素:" + as.top());
System.out.println("栈的大小:" + as.size());
}
}
7.链表的实现栈
表头为栈顶,在表头执行入栈和出栈操作
package dataStructure;
class LLNode{
private int data;
private LLNode next = null;
public LLNode(int d){
data = d;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public LLNode getNext() {
return next;
}
public void setNext(LLNode next) {
this.next = next;
}
}
public class LLStack {
private LLNode head;
boolean isEmpty() {
return head == null;
}
int size() {
int count = 1;
LLNode cur = head;
if(isEmpty()) return 0;
while(cur.getNext() !=null ) {
cur = cur.getNext();
count++;
}
return count;
}
int top() {
if(isEmpty())
new Exception("栈空");
return head.getData();
}
void push(int d) {
if(isEmpty()) {
head =new LLNode(d);
}else {
LLNode node = new LLNode(d);
node.setNext(head);
head = node;
}
}
int pop() {
if(isEmpty())
new Exception("栈空");
LLNode del = head;
head = head.getNext();
return del.getData();
}
void display() {
if(isEmpty()) {
System.out.println("栈空");
}else {
LLNode cur = head;
System.out.print("栈内元素:");
while(cur != null) {
System.out.print(cur.getData() + "->");
cur = cur.getNext();
}
System.out.println();
}
}
void deleteStack() {
head = null;
}
public static void main(String[] args) {
LLStack ls = new LLStack();
ls.push(2);
ls.push(3);
System.out.println("栈顶元素:" + ls.top());
ls.display();
System.out.println("移除栈顶元素:" + ls.pop());
ls.push(5);
System.out.println("删除整个栈");
ls.deleteStack();
ls.display();
}
}