Java用链表实现栈和队列
1、用链表实现栈
package stack;
/**
*
* @author denghb
*
*/
class Link {
public long dData;
public Link next;
public Link(long dd) {
dData = dd;
}
public void displayLink() {
System.out.print(dData + " ");
}
} class LinkList {
private Link first;
public LinkList() {
first = null;
}
public boolean isEmpty() {
return (first == null);
}
public void insertFirst(long dd) {
Link newLink = new Link(dd);
newLink.next = first;
first = newLink;
}
public long deleteFirst() {
//如果这是一个非空链表
Link temp = first;
first = first.next;
return temp.dData;
}
public void displayList() {
Link current = first;
while(current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
} class LinkStack {
private LinkList theList;
public LinkStack() {
theList = new LinkList();
}
public void push(long j) {
theList.insertFirst(j);
}
public long pop() {
return theList.deleteFirst();
}
public boolean isEmpty() {
return (theList.isEmpty());
}
public void displayStack() {
System.out.print("Stack (top-->bottom): ");
theList.displayList();
}
}
public class LinkStackApp {
public static void main(String[] args) {
LinkStack theStack = new LinkStack();
theStack.push(20);
theStack.push(40); theStack.displayStack(); theStack.push(60);
theStack.push(80); theStack.displayStack(); theStack.pop();
theStack.pop(); theStack.displayStack();
}
}
2、用链表实现队列
package queue;
/**
*
* @author denghb
*
*/
class Link {
public long dData;
public Link next;
public Link(long dd) {
dData = dd;
}
public void displayLink() {
System.out.print(dData + " ");
}
} class FirstLastList {
private Link first;
private Link last; public FirstLastList() {
first = null;
last = null;
}
public boolean isEmpty() {
return (first == null);
}
public void insertLast(long dd) {
Link newLink = new Link(dd);
if(isEmpty()) {
first = newLink;
} else {
last.next = newLink;
}
last = newLink;
}
public long deleteFirst() {
//如果这是一个非空链表
long temp = first.dData;
if(first.next == null) { //如果仅仅有一个链接点
last = null;
}
first = first.next;
return temp;
}
public void displayList() {
Link current = first;
while(current != null) {
current.displayLink();
current = current.next;
}
System.out.println("");
}
} class LinkQueue {
private FirstLastList theList;
public LinkQueue() {
theList = new FirstLastList();
}
public void insert(long j) {
theList.insertLast(j);
}
public long remove() {
return theList.deleteFirst();
}
public boolean isEmpty() {
return (theList.isEmpty());
}
public void displayStack() {
System.out.print("Stack (top-->bottom): ");
theList.displayList();
}
} public class LinkQueueApp {
public static void main(String[] args) {
LinkQueue theQueue = new LinkQueue();
theQueue.insert(20);
theQueue.insert(40); theQueue.displayStack(); theQueue.insert(60);
theQueue.insert(80); theQueue.displayStack(); theQueue.remove();
theQueue.remove();
theQueue.displayStack();
}
}
第一个源程序的输出结果:
Stack (top-->bottom): 40 20
Stack (top-->bottom): 80 60 40 20
Stack (top-->bottom): 40 20
第二个源程序的输出结果:
Stack (top-->bottom): 20 40
Stack (top-->bottom): 20 40 60 80
Stack (top-->bottom): 60 80
Java用链表实现栈和队列的更多相关文章
- Java 用链表实现栈和队列
栈 是一种基于后进先出(LIFO)策略的集合类型.当邮件在桌上放成一叠时,就能用栈来表示.新邮件会放在最上面,当你要看邮件时,会一封一封从上到下阅读.栈的顶部称为栈顶,所有操作都在栈顶完成. 前面提到 ...
- java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表 数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...
- 数据结构 1 线性表详解 链表、 栈 、 队列 结合JAVA 详解
前言 其实在学习数据结构之前,我也是从来都没了解过这门课,但是随着工作的慢慢深入,之前学习的东西实在是不够用,并且太皮毛了.太浅,只是懂得一些浅层的,我知道这个东西怎么用,但是要优化.或者是解析,就不 ...
- 链表、栈、队列、KMP相关知识点
链表.栈与队列.kmp; 数组模拟单链表: 用的最多的是邻接表--就是多个单链表: 作用:存储树与图 需要明确相关定义: 为什么需要使用数组模拟链表 比使用结构体 或者类来说 速度更快 代码简洁 算法 ...
- 图解堆算法、链表、栈与队列(Mark)
原文地址: 图解堆算法.链表.栈与队列(多图预警) 堆(heap),是一类特殊的数据结构的统称.它通常被看作一棵树的数组对象.在队列中,调度程序反复提取队列中的第一个作业并运行,因为实际情况中某些时间 ...
- [Python] 数据结构--实现顺序表、链表、栈和队列
说明: 本文主要展示Python实现的几种常用数据结构:顺序表.链表.栈和队列. 附有实现代码. 来源主要参考网络文章. 一.顺序表 1.顺序表的结构 一个顺序表的完整信息包括两部分,一部分是表中元素 ...
- 线性表 及Java实现 顺序表、链表、栈、队列
数据结构与算法是程序设计的两大基础,大型的IT企业面试时也会出数据结构和算法的题目, 它可以说明你是否有良好的逻辑思维,如果你具备良好的逻辑思维,即使技术存在某些缺陷,面试公司也会认为你很有培养价值, ...
- Java数据结构和算法 - 栈和队列
Q: 栈.队列与数组的区别? A: 本篇主要涉及三种数据存储类型:栈.队列和优先级队列,它与数组主要有如下三个区别: A: (一)程序员工具 数组和其他的结构(栈.队列.链表.树等等)都适用于数据库应 ...
- Java面试题:栈和队列的实现
面试的时候,栈和队列经常会成对出现来考察.本文包含栈和队列的如下考试内容: (1)栈的创建 (2)队列的创建 (3)两个栈实现一个队列 (4)两个队列实现一个栈 (5)设计含最小函数min()的栈,要 ...
随机推荐
- 将十进制的颜色制转换成ARGB
将一个十进制的颜色值转换成具体的ARGB 格式,起初,这看起来有些难,一直找不到方法,在网上也找不到具体的资料,最后在同事的指导下成功完成的转换,现分享出来,供大家参考,具体转换方法如下: /// & ...
- 版本控制工具git入门
版本控制工具的历史 不说了,放张图 两者的区别:集中式需要一个中心服务器放置最新的文件,需要联网操作.分布式可以再不联网的情况下操作,前提要拥有版本库 git安装 略 github注册 略 如何在g ...
- DM8168 debug continue... ...
1.boot VFS: Unable to mount root fs via NFS, trying floppy. VFS: Cannot open root device "n ...
- JavaScript学习代码整理(二)--函数
//JavaScript函数 //简单的求和函数 function sum(a,b) { return a + b; } //函数可以存储在变量中,也可以通过变量调用函数 x = sum(a,b); ...
- ACM俱乐部算法基础练习赛(1)
A: 水题 代码: #include<cstdio> #include<algorithm> using namespace std; ]; int n,m,c; int ma ...
- Spring 自动装配
1.自动装配有 bytype 和byName两种模式. 2.可以使用autowire属性指定自动装配的方式,byName根据bean的名称和当前bean的setter风格属性进行自动装配:byType ...
- C++实现一个限制对象实例个数的类
http://www.cnblogs.com/absolute8511/archive/2009/03/02/1649603.html
- 【Xamarin开发 Android 系列 4】 Android 基础知识
原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android? Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Li ...
- Eclipse can't install updates
trying to update eclipse but after downloading updates i always get an error dialog saying: An error ...
- YIi 使用 beginContent() 和 endContent() 设定 Yii 的 layouts
Yii 的 views/layouts 是用来放置 layouts 的目录,在默认的情况下会有 main.php 和 column1.php 和 column2.php. main.php 内容定义了 ...