日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java中的列表栈链表_Java数据结构(栈,队列,双链表)

發(fā)布時(shí)間:2025/3/12 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中的列表栈链表_Java数据结构(栈,队列,双链表) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

(1)棧package ChapterOne;

public class Stack {

//棧數(shù)組

long stackArr[];

//棧的大小

int maxSize;

//棧的頂部

int top;

//初始化一個(gè)大小為size的棧

public Stack(int size){

maxSize = size;

stackArr = new long[size];

top = -1;

}

//出棧操作

public long pop(){

return stackArr[top--];

}

//進(jìn)棧操作

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)隊(duì)列

package ChapterOne;

public class Queue {

//隊(duì)列數(shù)組

private long queueArr[];

//隊(duì)列的前端下標(biāo)

private int front;

//隊(duì)列的尾端下標(biāo)

private int rear;

//隊(duì)列的大小

private int maxSize;

//隊(duì)列中元素的個(gè)數(shù)

private int nItems;

//初始化一個(gè)大小為size的隊(duì)列

public Queue(int size){

queueArr = new long[size];

maxSize = size;

front = 0;

rear = -1;

nItems = 0;

}

//插入操作

public void insert(long value){

//隊(duì)列已滿

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;

}

//返回隊(duì)列第一個(gè)元素

public long peakFront(){

return queueArr[front];

}

//判斷是否為空

public boolean isEmpty(){

return nItems == 0;

}

//判斷是否已滿

public boolean isFull(){

return nItems == maxSize;

}

//返回隊(duì)列中元素的個(gè)數(shù)

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)優(yōu)先隊(duì)列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){ //在鏈表尾添加節(jié)點(diǎn)

Chain current=new Chain(); //創(chuàng)建鏈表頭

Chain temp=header;

while(temp.next!=null) //循環(huán)至鏈表尾

temp=temp.next;

temp.next=current;

current.pre=temp;

current.id=id;

current.name=name;

return current;

}

public Chain remove(int id){ //刪除指定id的節(jié)點(diǎn)

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的節(jié)點(diǎn)

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(){ //刪除最后一個(gè)節(jié)點(diǎn)

Chain temp=header;

while(temp.next.next!=null){

temp=temp.next;

}

temp.next=null;

return temp;

}

public void clear(){ //刪除所有節(jié)點(diǎn)

header.next=null;

}

public Chain insert(int id,String name,int pos){ //在指定位置插入節(jié)點(diǎn)

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();

}

}

總結(jié)

以上是生活随笔為你收集整理的java中的列表栈链表_Java数据结构(栈,队列,双链表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。