package com.cl.content01;
/*
* 使用链表来实现栈
*/
public class Stack<E> {
Node<E> top=null;
public boolean isEmpty(){
return top==null;
}
/*
* 出栈
*/
public void push(E data){
Node<E> nextNode=new Node<E>(data);
nextNode.next=top;
top=nextNode;
}
/*
* 出栈
*/
public E pop(){
if(this.isEmpty()){
return null;
}
E data =top.datas;
top=top.next;
return data;
}
}
/*
* 链表
*/
class Node<E>{
Node<E> next=null;
E datas;
public Node(E datas){
this.datas=datas;
}
}
package com.cl.content01;
public class MyQueue<E> {
private Node<E> head=null;
private Node<E> tail=null;
public boolean isEmpty(){
return head==null;
}
public void put(E data){
Node<E> newNode=new Node<E>(data);
if(head==null&&tail==null)
head=tail=newNode;
else
tail.next=newNode;
tail=newNode;
}
public E pop(){
if(this.isEmpty())
return null;
E data=head.data;
head=head.next;
return data;
}
public int size(){
int n=0;
Node<E> t=head;
while(t!=null){
n++;
t=t.next;
}
return n;
}
public static void main(String[] args) {
MyQueue<Integer> q=new MyQueue<Integer>();
q.put(1);q.put(3);q.put(2);
System.out.println(q.pop());
System.out.println(q.size());
System.out.println(q.pop());
}
}
class Node<E>{
Node<E> next=null;
E data;
public Node(E data){
this.data=data;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有