//=================================================
// File Name : LinkStack_demo
//------------------------------------------------------------------------------
// Author : Common //类名:Link_long
//属性:
//方法:
class Link_long{ //链节点类
public long dData;
public Link_long next; //链表中下一个节点的引用 public Link_long(long dData) {
super();
this.dData = dData;
} public void displayLink(){ //显示当前节点的值
System.out.println("dData"+dData);
} } //类名:LinkList_long
//属性:
//方法:
class LinkList_long{
private Link_long first; //只需要第一个节点,从第一个节点出发即可定位所有节点 public LinkList_long() { //构造函数
this.first = null;
} public boolean isEmpty(){
return (first == null);
} public void insertFirst(long dd){ //插入元素是从链表的头开始插入
Link_long newLink = new Link_long(dd);
newLink.next = first;
first = newLink;
} public long deleteFirst(){ //删除temp.dData
Link_long temp = first; //暂存first
first = first.next; //把next设为first
return temp.dData; //返回原来的first
} public void displayList(){
System.out.println("List(first-->last):");
Link_long current = first; //用于不断改变位置实现遍历
while(current != null){
current.displayLink();
current = current.next;
}
} public Link_long find(int key){ //查找指定的关键字
Link_long current = first;
while(current.dData != key){
if(current.next == null)
return null;
else
current = current.next;
}
return current;
} public Link_long delete(int key){ //如果current的值匹配,则删除
Link_long current = first;
Link_long previous = first;
//没有匹配到值
while(current.dData != key){
if(current.next == null)
return null;
else{ //pre和cur向后移动
previous = current;
current = current.next;
}
}
//匹配到值
if(current == first) //只有一个first,并匹配,则把first设成first.next
first = first.next;
else //current的值匹配,则删除,并把cur的next赋给pre的next
previous.next = current.next;
return current;
}
} //类名:LinkList_long
//属性:
//方法:
class LinkStack{ //用链表实现栈,链表从First开始插入,新的元素将变成First,先删除后来元素 private LinkList_long theList; public LinkStack() { //构造函数
theList = new LinkList_long();
} public void push(long j){ //入栈,即在链表的First插入元素
theList.insertFirst(j);
} public long pop(){ //出栈,即删除链表的First元素
return theList.deleteFirst();
} public boolean isEmpty(){ //判断栈是否为空,即判断链表是否为空
return (theList.isEmpty());
} public void displayStack(){
System.out.println("Stack(top-->bottom):");
theList.displayList();
} } //主类
//Function : LinkStack_demo
public class LinkStack_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
LinkStack theStack = new LinkStack();
theStack.push(10);
theStack.push(20);
theStack.push(30);
theStack.displayStack(); theStack.pop();
theStack.pop();
theStack.displayStack();
} }

Java数据结构——用链表实现栈的更多相关文章

  1. java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表

    java实现 数据结构:链表. 栈. 队列.优先级队列.哈希表   数据结构javavector工作importlist 最近在准备找工作的事情,就复习了一下java.翻了一下书和网上的教材,发现虽然 ...

  2. JAVA数据结构之链表

    JAVA数据结构之链表 什么是链表呢? 链表作为最基本的数据结构之一,定义如下: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的. 简单来说呢,链 ...

  3. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

  4. java数据结构与算法之栈(Stack)设计与实现

    本篇是java数据结构与算法的第4篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是一种用于 ...

  5. Java数据结构和算法(一)--栈

    栈: 英文名stack,特点是只允许访问最后插入的那个元素,也就是LIFO(后进先出) jdk中的stack源码: public class Stack<E> extends Vector ...

  6. Java数据结构与算法(2):栈

    栈是一种线性表,特点在于它只能在一个位置上进行插入和删除,该位置是表的末端,叫做栈的顶(top).因此栈是后进先出的(FIFO).栈的基本操作有push.peek.pop. 栈的示意图 进栈和出栈都只 ...

  7. JAVA数据结构——单链表

    链表:一. 顺序存储结构虽然是一种很有用的存储结构,但是他有如下几点局限性:1. 因为创造线性表的时候已经固定了空间,所以当需要扩充空间时,就需要重新创建一个地址连续的更大的存储空间.并把原有的数据元 ...

  8. java实现单链表、栈、队列三种数据结构

    一.单链表 1.在我们数据结构中,单链表非常重要.它里面的数据元素是以结点为单位,每个结点是由数据元素的数据和下一个结点的地址组成,在java集合框架里面 LinkedList.HashMap(数组加 ...

  9. 数据结构之链表、栈和队列 java代码实现

    定义抽象节点类Node: package cn.wzbrilliant.datastructure; /** * 节点 * @author ice * */ public abstract class ...

随机推荐

  1. 1010每次备份我的MySQL数据库

    转自http://bbs.csdn.net/topics/320136534 在windows平台下面 /*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/* ...

  2. oracle从游标批量提取数据

    来源于:http://blog.csdn.net/ceclar123/article/details/7974973 传统的fetch into一次只能取得一条数据,使用fetch bulk coll ...

  3. 高手详解SQL性能优化十条经验

    1.查询的模糊匹配 尽量避免在一个复杂查询里面使用 LIKE '%parm1%'—— 红色标识位置的百分号会导致相关列的索引无法使用,最好不要用. 解决办法: 其实只需要对该脚本略做改进,查询速度便会 ...

  4. JavaScript写一个小乌龟推箱子游戏

    推箱子游戏是老游戏了, 网上有各种各样的版本, 说下推箱子游戏的简单实现,以及我找到的一些参考视频和实例: 推箱子游戏的在线DEMO : 打开 如下是效果图: 这个拖箱子游戏做了移动端的适配, 我使用 ...

  5. java-io-FileReader和FileWriter类

    实例:用FileWriter类向文件中写入一个串字符,然后用FileReader读出写入的内容. import java.io.*; public class FileStream2{ public ...

  6. lucene-查询query->QueryParser

    对于搜索引擎(比如Google和百度)来讲,很多情况下只需要用户在输入框内输入所需查询的内容,然后再单击“搜索”就可以了,其余的事情全部交给搜索引擎去处理,最后搜索引擎会把检索到的结果显示出来.那么搜 ...

  7. 纯代码自定义不等高cell

    数据模型.plist解析这里就不过多赘述. 错误思路之一: 通过在heightForRowAtIndexPath:方法中调用cellForRowAtIndexPath:拿到cell,再拿到cell的子 ...

  8. How to fix the sources list

    How to fix the sources list Sometimes the apt-get may not work, it is often caused by the misspelled ...

  9. Debugger 怎么用

    Debugger 是一个很有用的工具,尤其对于workflow开发人员来说.可以帮助我们check 从source 到target的数据流.我们可以看到什么数据从source中流出,在接下来的tran ...

  10. 【BZOJ-3786】星系探索 Splay + DFS序

    3786: 星系探索 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 647  Solved: 212[Submit][Status][Discuss] ...