数据结构——用(单端)链表实现栈(java实现)
来源:互联网 发布:太阳帝国知乎 编辑:程序博客网 时间:2024/06/17 12:23
栈是一种先进后出的数据结构,即只有最后插入的数据项才运行被访问,今天用单端链表模拟了栈的实现,代码如下:
/* *You can just get the top item in the stack,what is first inserted is in the bottom of stack! * **/class Link{public long dData;public Link next;public Link(long dData){this.dData = dData;}public void displayLink(){System.out.print("{" + this.dData + "}");}}class LinkList{Link first;public LinkList(){this.first = null;}public boolean isEmpty(){return first == null;}public void insertFirst(long key)//this method will be used in push(){Link newLink = new Link(key);if(this.isEmpty())//if list is empty{first = newLink;newLink.next = null;}else{newLink.next = first;first = newLink;}}public long deleteFirst()//this method will be used in pop();{Link current = null;if(this.isEmpty()){System.out.println("Your stack is empty");return -1;}else{current = first;first = first.next;return current.dData;}}public void displayList(){Link current = first;System.out.print("stack (top-->buttom): ");if(this.isEmpty()){System.out.println("Your list is empty, nothing to show!");}else{while(current!=null){current.displayLink();current = current.next;}System.out.println("");}}}class LinkStack{LinkList list = new LinkList();public void push(long key){list.insertFirst(key);}public long pop(){return list.deleteFirst();}public void showStack(){list.displayList();}}class LinkStackApp{public static void main(String[] args){LinkStack linkStack = new LinkStack();linkStack.push(20);//push four elementlinkStack.push(40);linkStack.showStack();//look at what is in the stacklinkStack.push(60);linkStack.push(80);linkStack.showStack();//look at what is in the stacklinkStack.pop();linkStack.pop();linkStack.showStack();//look at what is in the stack}}
运行效果:
0 0
- 数据结构——用(单端)链表实现栈(java实现)
- 数据结构——建立单向链表(java实现)
- 数据结构——单向传统链表(java实现)
- 数据结构——双向链表(Java实现)
- java 实现双向链表(数据结构)
- 数据结构--双向链表实现(java)
- 数据结构——双端链表(java实现)
- 数据结构——使用双端链表实现队列(java实现)
- 数据结构单链队列——链式存储实现
- 数据结构(队列--单链表表实现--不含队头尾指针)
- 数据结构—顺序表(自己实现Java的ArrayList)
- java 实现单项链表(摘抄)
- 数据结构Java实现——①栈
- 数据结构——Java实现栈
- 数据结构——Java实现顺序表
- java 链表数据结构实现
- 数据结构-Java实现链表
- 数据结构之-栈(Java实现)
- perl解析XML的性能比较
- 关于android toast的简单使用
- 利用主线程与子线程间的消息通讯,实现任务处理队列.子线程中创建不会阻塞执行的窗口
- 第一章 Web MVC简介
- !!![YOU SHOULD NEVER EVER BE SO FOOL TO MAP <C-N> TO <C-X><C-O>]!!!
- 数据结构——用(单端)链表实现栈(java实现)
- Android APK反编译就这么简单 详解(附图)
- Source Insight设置配置
- HBase Thrift 接口函数
- 熊猫盼盼迎35岁生日 相当于人类100岁
- jquery ajax post方法获取json数据
- 拒绝服务
- leetcode 1 Two Sum
- IDEA Maven不见了