java-栈
来源:互联网 发布:请假流程数据库设计 编辑:程序博客网 时间:2024/06/03 21:32
栈是一种只允许在一端(top)进行插入和删除的线性表,特点是先进后出。
我们接下来要介绍栈的几个基本操作,判断栈空、判断栈满。出栈。进栈、给栈扩容等等。
1.栈的顺序存储结构
//顺序链表class SqStack{ private int[] stack; private int top; //开辟10个元素的空间 public SqStack(){// this.stack = new int[10];// this.top =0 ; this(10); } public SqStack(int size){ this.stack = new int[size]; this.top = 0; } //入栈 public void push(int val){ if(full()) resize(); //如果栈满则扩容 this.stack[this.top++] = val; } //出栈 public void pop(){ if(empty()) return; this.top--; } //返回栈顶元素 public int top(){ return this.stack[this.top-1]; } //判断是否栈空 public boolean empty(){ return this.top == 0; } //判断是否栈满 public boolean full(){ return this.top == this.stack.length; } //栈的扩容,一次扩容两倍 public void resize(){ this.stack = Arrays.copyOf(stack, top*2); } //用Arrays.copyOf函数进行扩容,利用拷贝的特性}
2.栈的链式存储结构
//定一个节点并初始化节点class Node{ int value; Node next; Node(){ this(0); } Node(int value){ this.next = null; this.value = value; }}class LinkList{ Node head;//定义一个链表 public LinkList(){ head = new Node(0); }//判断是否为空 public boolean empty(){ return head.next == null; }//头插法插入节点 public void insertHead(int val){ Node n = new Node(val); n.next = head.next; head.next = n; }//尾插法插入节点 public void insertTail(int val){ Node ptail = head; while(ptail.next != null){ ptail = ptail.next; } ptail.next = new Node(val); } //删除节点 public void delete(int val){ Node pcur = head.next; Node ppre = head; while(pcur != null){ if(pcur.value == val){ ppre.next = pcur.next; break; } ppre = pcur; pcur = pcur.next; } } //自己实现toString方法 public String toString(){ StringBuilder builder = new StringBuilder(); Node n = head.next; while(n != null){ builder.append(n.value + " "); n = n.next; } return builder.toString(); }}
顺序表与链表比较,顺序表用数组存储数据,更容易实现且操作简单,但做插入删除操作时需大量移动数据,效率较低。
链表采用栈的形式存取数据,与顺序表的优缺点刚好相反。
阅读全文
0 0
- java 栈
- java 栈
- Java栈
- java栈
- java栈
- java 栈
- Java 栈
- java 栈
- Java栈
- java栈
- 栈(JAVA)
- java-栈
- [Java]Java栈与堆
- java 栈----java.util.Stack
- Java栈和Java堆
- Java栈和Java堆
- 【Java】Java栈的使用
- java内存栈java虚拟机
- 解决 javaweb下载文件时报 您的主机中的软件中止了一个已建立的连接
- 【JZOJ5428】【NOIP2017提高A组集训10.27】查询
- 线程笔记1——多线程概述
- 简单医疗系统设计(一)登录界面的制作
- 机器学习概念理解
- java-栈
- linux/ubuntu下安装java开发环境
- 算法储备之Floyd Warshall算法
- JavaScript节流函数
- 武孝城际小站客运淡季营销结硕果
- SPOJ
- 线程间通信
- 最小树形图
- 关于final的一点思考