java实现栈的反转
来源:互联网 发布:nginx书籍 编辑:程序博客网 时间:2024/05/19 11:45
class Node {
char data;
Node next;
public Node(char data) {
this.data = data;
next = null;
}
}
class LinkStack {
Node first;
public LinkStack() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public boolean push(Node node) {
node.next = first;
first = node;
return true;
}
public Node pop() {
if (isEmpty())
return null;
Node tmp;
tmp = first;
first = first.next;
return tmp;
}
}
public class ReverseStack {
public static void main(String[] args) {
LinkStack stackIn = new LinkStack();
LinkStack stackOut = new LinkStack();
String str = "hello world!";
char[] a = str.toCharArray();
Node node = new Node(' ');
stackIn.push(node);
for (char x : a) {
Node n = new Node(x);
stackIn.push(n);
}
while (stackIn.first != null) {
stackOut.push(stackIn.pop());
if (stackOut.first.data == ' ') {
while (stackOut.first != null)
System.out.print(stackOut.pop().data);
}
}
}
}
char data;
Node next;
public Node(char data) {
this.data = data;
next = null;
}
}
class LinkStack {
Node first;
public LinkStack() {
first = null;
}
public boolean isEmpty() {
return first == null;
}
public boolean push(Node node) {
node.next = first;
first = node;
return true;
}
public Node pop() {
if (isEmpty())
return null;
Node tmp;
tmp = first;
first = first.next;
return tmp;
}
}
public class ReverseStack {
public static void main(String[] args) {
LinkStack stackIn = new LinkStack();
LinkStack stackOut = new LinkStack();
String str = "hello world!";
char[] a = str.toCharArray();
Node node = new Node(' ');
stackIn.push(node);
for (char x : a) {
Node n = new Node(x);
stackIn.push(n);
}
while (stackIn.first != null) {
stackOut.push(stackIn.pop());
if (stackOut.first.data == ' ') {
while (stackOut.first != null)
System.out.print(stackOut.pop().data);
}
}
}
}
- java实现栈的反转
- 单链表的反转java实现
- java 实现单链表的反转
- Java实现字符串的反转
- java 实现单链表的反转
- 单链表的反转-Java实现
- java实现字符串的反转
- Java实现字符串的反转
- java实现单链表的反转
- 算法:java 实现单链表的反转
- 反转链表的Java实现
- 用Java实现单链表的反转
- java实现链表的反转
- java 如何实现一个字符串的反转
- Java实现数组反转翻转的方法
- 元音字母的反转----java实现
- java实现链表的反转
- 关于java字符串反转的实现
- addChild的使用 ——可视化对象
- hdu 1255
- 黑客编程教程(十五)HTTP代理
- 数字,字符串 和集合
- 黑客编程教程(十六)线程插入技术
- java实现栈的反转
- hdu 1542 Atlantis
- Android应用程序UI设计(1)_布局
- 线程间通信
- asp.net 验证正则表达式集合-1
- 数组生成的所有集合
- HaspMap(jiava)
- build-helper-maven-plugin 配置多 source resource 文件
- java识别文件编码格式代码(无引用jar包,测试可行)