栈和队列是两种重要的线性数据结构,都是在一个特定的范围的存储单元中的存储数据。与线性表相比,它们的插入和删除操作收到更多的约束和限定,又被称为限定性的线性表结构。栈是先进后出FILO,队列是先进先出FIFO,但是有的数据结构按照一定的条件排队数据的队列,这时候的队列属于特殊队列,不一定按照上面的原则。
链表方法:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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;
}
}
|
链表方法:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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;
}
}
|
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望通过本能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/qq442270636/article/details/72085631
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-24 44
-
2025-05-29 15
-
2025-05-25 86
-
2025-06-05 36
-
2025-05-27 25
热门评论