下面的代码是栈的使用的简单例子,以及栈的两个应用:字符串倒置,判断括号是否匹配。
import java.util.Stack;
public class StackTest
{
private static Stack<String> stack = new Stack<String>();
public static void main(String[] args)
{
String[] str = new String[]{"ab", "cd", "ef", "gh"};
Reverse(str);
String word = "abcdefg";
Reverse(word);
String match = "a{B[cf(ds)kf]s}sd";
isMatch(match);
}
public static void Reverse(String[] str)
{
System.out.print("Before reverse: ");
for(int i=0; i<str.length; i++)
System.out.print(str[i] + " ");
for(int i=0; i<str.length; i++)
stack.push(str[i]);
System.out.println();
System.out.print("After reverse: ");
while(!stack.isEmpty())
System.out.print(stack.pop() + " ");
System.out.println();
}
public static void Reverse(String str)
{
System.out.print("Before reverse: ");
System.out.print(str);
for(int i=0; i<str.length(); i++)
stack.push(String.valueOf(str.charAt(i)));
System.out.println();
System.out.print("After reverse: ");
while(!stack.isEmpty())
System.out.print(stack.pop());
System.out.println();
}
public static void isMatch(String str)
{
char ch;
String temp;
String top;
boolean flag = true;
for(int i=0; i<str.length(); i++)
{
ch = str.charAt(i);
temp = String.valueOf(ch);
if(temp.equals("{") || temp.equals("[") || temp.equals("("))
stack.push(String.valueOf(ch));
else if(temp.equals("}") || temp.equals("]") || temp.equals(")"))
{
if(stack.isEmpty())
{
flag = false;
System.out.println("Not match !");
break;
}
else
{
top = stack.pop();
if((top.equals("{") && !temp.equals("}")) || (top.equals("[") && !temp.equals("]")) || (top.equals("(") && !temp.equals(")")))
{
stack.push(top);
System.out.println("Not match !");
break;
}
}
}
}
if(stack.isEmpty() && flag)
System.out.println("Match!");
}
}
/**
* 输出如下:
* Before reverse: ab cd ef gh
After reverse: gh ef cd ab
Before reverse: abcdefg
After reverse: gfedcba
Match!
*/
下面的代码是队列的简单使用。
public class QueueTest
{
public static void main(String[] args)
{
String[] str = new String[]{"ab", "cd", "ef", "gh"};
Queue<String> queue = new LinkedList<String>();
Display(str, queue);
}
public static void Display(String[] str, Queue<String> queue)
{
for(int i=0; i<str.length; i++)
queue.add(str[i]);
while(!queue.isEmpty())
System.out.print(queue.poll() + " ");
System.out.println();
}
}
/**
* 输出如下:
* ab cd ef gh
*/
分享到:
相关推荐
C语言-数据结构-栈队列实现
delphi栈队列操作 delphi栈队列操作 delphi栈队列操作
栈队列串数组
数据结构实验:堆栈与队列; 包括3个代码和实验报告: 括号匹配完成、 利用栈队列逆置 、栈的操作
1、5个男生3个女生排成一排,3个女生要排在一起,有多少种不同的排法?
数据结构与算法:栈队列的题库
用C实现的栈与队列,可以加载使用。详见博文http://blog.csdn.net/pirateleo/article/details/7574598 共包含5个文件
简单介绍了线性表栈队列串数组广义表树二叉树图查找内部排序外部排序
任务描述栈和队列都提供 Push/Pop 两种操作,其中 Push:加入一个元素。Pop:弹出一个元素。给出一个线性结构的进出顺序,判定这个结构是栈还是队列。(40’) 输入描述第一行输入一个整数s,代表有s组测试数据。第一...
c/c++ 数据结构 回文 栈 队列 的应用 课程设计
栈队列堆试题.pdf
数据结构课件 栈 队列 排序 冒泡法 直接插入法 代码
c语言数据结构-栈队列数组完美版资料.ppt
栈和队列的基本操作实验报告 一、实验目的 1、熟练掌握栈和队列的基本操作在两种存储结构上的实现。 2、会用栈和队列解决简单的实际问题。 二、实验内容 题目:试写一个算法,判断依次读入的一个以@为结束符的...
数据结构包括 栈 队列 字符串 树等程序,完全可以实现,下载就可以任意使用,绝对无错。
c++ 数据结构 教程 线性表 栈 队列 串 的实现,共初学者学习参考
超级数组和链表及栈队列
考研必备 数据结构 第二章 栈队列和数组 请使用IE浏览器观看! 欢迎下载!
栈队列树二叉树课件PDF.rar
数栈队列PPT学习教案.pptx