java中的列表栈链表_Java数据结构(栈,队列,双链表)
(1)栈package ChapterOne;
public class Stack {
//栈数组
long stackArr[];
//栈的大小
int maxSize;
//栈的顶部
int top;
//初始化一个大小为size的栈
public Stack(int size){
maxSize = size;
stackArr = new long[size];
top = -1;
}
//出栈操作
public long pop(){
return stackArr[top--];
}
//进栈操作
public void push(long value){
stackArr[++top] = value;
}
//判断栈是否为空
public boolean isEmpty(){
return top == -1;
}
//判断栈是否已满
public boolean isFull(){
return top == maxSize-1;
}
//取栈顶元素
public long peek(){
return stackArr[top];
}
public static void main(String[] args) {
Stack stack = new Stack(10);
while(!stack.isFull()){
long v = (long) (Math.random()*100);
stack.push(v);
System.out.print(v+" ");
}
System.out.println();
while(!stack.isEmpty()){
long topValue = stack.pop();
System.out.print(topValue+" ");
}
System.out.println();
}
}
(2)队列
package ChapterOne;
public class Queue {
//队列数组
private long queueArr[];
//队列的前端下标
private int front;
//队列的尾端下标
private int rear;
//队列的大小
private int maxSize;
//队列中元素的个数
private int nItems;
//初始化一个大小为size的队列
public Queue(int size){
queueArr = new long[size];
maxSize = size;
front = 0;
rear = -1;
nItems = 0;
}
//插入操作
public void insert(long value){
//队列已满
if(rear == maxSize-1)
rear = -1;
queueArr[++rear] = value;
nItems++;
}
//删除操作
public long remove(){
long temp = queueArr[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
//返回队列第一个元素
public long peakFront(){
return queueArr[front];
}
//判断是否为空
public boolean isEmpty(){
return nItems == 0;
}
//判断是否已满
public boolean isFull(){
return nItems == maxSize;
}
//返回队列中元素的个数
public int size(){
return nItems;
}
public void print(){
for(int i = front;i < front+nItems;i++){
System.out.print(queueArr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Queue q = new Queue(10);
while(!q.isFull()){
long value = (long)(Math.random()*100);
q.insert(value);
}
q.print();
while(!q.isEmpty()){
q.remove();
q.print();
}
q.print();
System.out.println(q.isEmpty());
}
}(3)优先队列package ChapterOne; public class PriorityQueue { private int nItems; private long pqArr[]; private int maxSize; public PriorityQueue(int size){ maxSize = size; pqArr = new long[size]; nItems = 0; } public void insert(long value){ int i; if(nItems == 0) pqArr[nItems++] = value; else{ for(i = nItems-1;i >= 0;i--){ if(value < pqArr[i]){ pqArr[i+1] = pqArr[i]; } else break; } pqArr[i+1] = value; nItems++; } } public long remove(){ return pqArr[--nItems]; } public boolean isEmpty(){ return nItems == 0; } public boolean isFull(){ return nItems == maxSize; } public void print(){ for(int i = 0;i < nItems;i++) System.out.print(pqArr[i]+" "); System.out.println(); } public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(10); while(!pq.isFull()){ long value = (long)(Math.random()*100); pq.insert(value); } pq.print(); } }(4)双链表class Chain{
Chain pre=null,next=null;
int id;
String name;
}
class List{
private Chain header=new Chain();
public Chain add(int id,String name){ //在链表尾添加节点
Chain current=new Chain(); //创建链表头
Chain temp=header;
while(temp.next!=null) //循环至链表尾
temp=temp.next;
temp.next=current;
current.pre=temp;
current.id=id;
current.name=name;
return current;
}
public Chain remove(int id){ //删除指定id的节点
Chain temp=header;
Chain current=null;
while(temp.next!=null){
temp=temp.next;
if(temp.id==id){
current=temp;
break;
}
}
if(current==null)
return null;
current.pre.next=current.next;
if(current.next!=null)
current.next.pre=current.pre;
return current;
}
public Chain remove(String name){ //删除指定name的节点
Chain temp=header;
Chain current=null;
while(temp.next!=null){
temp=temp.next;
if(temp.name==name){
current=temp;
break;
}
}
if(current==null)
return null;
current.pre.next=current.next;
if(current.next!=null)
current.next.pre=current.pre;
return current;
}
public Chain remove(){ //删除最后一个节点
Chain temp=header;
while(temp.next.next!=null){
temp=temp.next;
}
temp.next=null;
return temp;
}
public void clear(){ //删除所有节点
header.next=null;
}
public Chain insert(int id,String name,int pos){ //在指定位置插入节点
Chain temp=header;
Chain current=new Chain();
int i=0;
for(i=0;i<=pos;i++){
if(temp.next!=null){
temp=temp.next;
}
else{
return null;
}
}
current.id=id;
current.name=name;
if(temp.next!=null){
temp.next.pre=current;
current.next=temp.next;
}
temp.next=current;
current.pre=temp;
return current;
}
public void print_all(){
Chain temp=header;
System.out.println("--------------------------------------");
while(temp.next!=null){
temp=temp.next;
System.out.println("ID: "+temp.id);
System.out.println("Name: "+temp.name);
}
System.out.println("--------------------------------------");
}
}
public class ChainList{
public static void main(String[] args){
List a=new List();
a.add(1,"谭孟泷1") ;
a.add(2,"谭孟泷2");
a.add(3,"谭孟泷3");
a.add(4,"谭孟泷4");
a.insert(12,"大胖",2);
a.print_all();
}
}
java中的列表栈链表_Java数据结构(栈,队列,双链表)相关推荐
- java数据接口之链表_Java数据结构和算法之链表
三.链表 链结点 在链表中,每个数据项都被包含在'点"中,一个点是某个类的对象,这个类可认叫做LINK.因为一个链表中有许多类似的链结点,所以有必要用一个不同于链表的类来表达链结点.每个LI ...
- java中怎样克隆,如何在Java中克隆列表?
要克隆Java中的列表,最简单的方法是使用ArrayList.clone()方法- 示例import java.util.ArrayList; public class Demo { public s ...
- 遍历Java中的列表的方法
本文翻译自:Ways to iterate over a list in Java Being somewhat new to the Java language I'm trying to fami ...
- java对列表数据排序_如何在Java中对列表进行排序
java对列表数据排序 Sometimes we have to sort a list in Java before processing its elements. In this tutoria ...
- 遍历Java中的列表
遍历Java中的列表 这篇文章将讨论在 Java 中迭代列表的各种方法. 1.使用 List.toString() 方法 如果我们只是想显示列表的内容,我们可以通过使用 toString() 方法,然 ...
- java中的递归函数调用函数_Java中函数的递归调用
说到递归,java中的递归和C语言中也是很相似的,在Java中,递归其实就是利用了栈的先进后出的机制来描述的. public class HelloWorld { public static void ...
- [AcWing]827. 双链表(C++实现)双链表模板题
[AcWing]827. 双链表(C++实现)双链表模板题 1. 题目 2. 读题(需要重点注意的东西) 3. 解法 4. 可能有帮助的前置习题 5. 所用到的数据结构与算法思想 6. 总结 1. 题 ...
- java中建立单链表_Java数据结构,单链表的建立
import java.util.*; //学生类 class Stu { private int math;//学生的学号 private String name;//学生的名字 public St ...
- java 二维链表_Java数据结构与算法----数组与链表
数据类型 1 数据类型介绍 数据类型的分类(按照结构划分):线性结构和非线性结构 线性结构:线性结构作为最常用的数据结构,其特点是数据元素之间存在一对一的线性关系 线性结构有两种不同的存储结构,即顺序 ...
最新文章
- 超详干货!Linux 环境变量配置全攻略
- 高斯混合模型(GaussianMixture Model, GMM)聚类、可视化最优协方差形式、通过TSNE进行结果可视化分析、抽取核心特征因子
- 2017年深度学习优化算法最新进展:如何改进SGD和Adam方法?
- 提示找不到include/common.h 提示No package 'minigui' found
- 【问链-EOS公开课】第十课 EOS 错误码整理
- go语言快速开发入门示例
- php 根据输入值读取MySQL_MySQL输入PHP给出的另一个值
- 最简单的6种防止数据重复提交的方法!(干货)
- 纱窗.20190512
- 数据库工作笔记013---如果存在表则删除表然后创建Mysql_drop table
- JWT实现Token认证
- 【转】移动客户端测试总结
- java数组整组处理_java – 使它漂亮:同时处理数组
- Linux文本编辑器-vi/vim
- numpy系列之拷贝和视图
- HUAWEI CE6870 IPv6 OSPFv3 配置
- linux 添加声卡驱动,操作手册:Linux系统安装声卡驱动
- 使用DuckDuckGo在命令行中搜索
- 零基础转行到IT,怎么选择适合的职业?
- 如何生成一个APP_ID
热门文章
- qc35 说明书_使用Bose QC35 2年的心得 | 迟而不迟的深度体验 | 文附佩戴效果照片...
- Java即时类| hashCode()方法与示例
- kotlin 字符串_Kotlin程序确定字符串是否具有所有唯一字符
- 经典面试题|ConcurrentHashMap 读操作为什么不需要加锁?
- 《JavaScript权威指南》——JavaScript核心
- 关于CentOS-6的默认带的mysql启动和安装问题
- AirFlow官方入门DAG示例
- vs连接oracle数据库报错,用VS连接oracle数据库时ORA-12504错误
- 编译后没有taget文件夹_maven资源文件的相关配置才会在编译后的target里面有
- 人脸检测算法_腾讯已开源高精度人脸检测算法DSFD