Java实现顺序栈
package sequence;
//java实现顺序栈
public class SeqStack<T> {
private int top = 0;
private int capacity;
private T[] elementData;
private static final int DEAFAULT_CAPACITY = 4;
public SeqStack(){
capacity = DEAFAULT_CAPACITY;
elementData = (T[])(new Object[capacity]);
}
public SeqStack(int capacity){
this.capacity = capacity;
elementData = (T[])new Object[capacity];
}
public SeqStack(T element){
this();
elementData[0] = element;
top++;
}
public SeqStack(T element,int capacity){
this.capacity = capacity;
elementData = (T[]) new Object[capacity];
elementData[0] = element;
top++;
}
public boolean isEmpty(){
return top == 0;
}
public boolean isFull(){
return top == capacity;
}
public boolean push(T element){
if(isFull()){
return false;
}
elementData[top++] = element;
return true;
}
public T pop(){
if(isEmpty()){
throw new IndexOutOfBoundsException("栈为空");
}
return elementData[--top];
}
public T peek(){
if(isEmpty()){
throw new IndexOutOfBoundsException("栈为空");
}
return elementData[top - 1];
}
public void traverse(){
for(int i=0;i<top;i++){
System.out.println(elementData[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SeqStack<String> ss = new SeqStack<>("aaa",3);
ss.push("bbb");
ss.push("ccc");
ss.traverse();
System.out.println("=======");
ss.push("ddd");
ss.traverse();
}
}
Java实现链式栈
package link;
public class LinkStack<T> {
class Node{
private T data;
private Node next;
public Node(){}
public Node(T data,Node next){
this.data = data;
this.next = next;
}
}
private Node top; //栈顶元素
private int size;
public LinkStack(){
top = null;
}
public LinkStack(T element){
top = new Node(element,null);
size++;
}
public int getSize(){
return size;
}
public void push(T element){
top = new Node(element,top);
// top = node;
size++;
}
public T pop(){
Node old = top;
top = top.next;
old.next = null;
size--;
return old.data;
}
public T peek(){
return top.data;
}
public boolean isEmpty(){
return size == 0;
}
public void clear(){
top = null;
size = 0;
}
public void traverse(){
for(Node current = top;current != null;current = current.next){
System.out.println(current.data);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkStack<String> ls = new LinkStack<>();
ls.push("aaa");
ls.push("bbb");
ls.traverse();
System.out.println("====");
// System.out.println(ls.peek());
ls.pop();
ls.push("ccc");
ls.traverse();
}
}
栈应用实例
进制转换
package sequence;
import java.util.Scanner;
public class Convert {
//将一个数转化为16进制
public static void main(String[] args) {
// TODO Auto-generated method stub
//转化为十六进制因为栈中存放的是小于16的余数,这里要定义数组使余数与下标对应
char num[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
SeqStack<Integer> sq = new SeqStack<>(10);
System.out.println("请输入要转化的十进制数");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int result = 0;
while(n != 0){
result = n%16;
sq.push(result);
n = n / 16;
}
while(!sq.isEmpty()){
int number = sq.pop();
System.out.print(num[number]);
}
System.out.println();
}
}
括号匹配
package sequence;
public class Match {
public static void main(String[] args) {
String str = "[({})]";
SeqStack<Character> test = new SeqStack<>(20);
int flag = 0;
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
switch(ch){
case '(':
test.push(ch);
break;
case '[':
test.push(ch);
break;
case '{':
test.push(ch);
break;
case ')':
if(test.isEmpty()){
flag = 1;
}else{
if(test.pop() == '('){
}else{
flag = 1;
}
}
break;
case ']':
if(test.isEmpty()){
flag = 1;
}else{
if(test.pop() == '['){
}else{
flag = 1;
}
}
break;
case '}':
if(test.isEmpty()){
flag = 1;
}else{
if(test.pop() == '{'){
}else{
flag = 1;
}
}
break;
}
}
if(test.isEmpty() && flag == 0){
System.out.println("字符串匹配正确");
}else{
System.out.println("匹配错误");
}
}
}