《数据结构与算法》学习笔记10 栈及其应用

本文介绍了一个简单的栈实现方法,并展示了如何使用栈进行字符串反转及括号匹配检查。通过具体示例,读者可以了解到栈的基本操作及其在实际问题解决中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//栈的实现
public class StackX {
  private long[] a;
  private int msize;
  private int top;
  public StackX(int max){
	  msize=max;
	  a=new long[msize];
	  top=-1;
  }
  
  public void push(long value){
	  top++;
	  a[top]=value;
  }
  
  public long pull(){
	  return a[top--];
  }
  
  public void dispaly(){
	  for(int i=top;i>-1;i--){
		  System.out.println(a[i]);
	  }
  }
  
  public boolean isEmpty(){
	  if(top!=-1) return false;
	  else return true;
  }
  
  public boolean isFull(){
	  if(top==msize-1) return true;
	  else return false;
  }
}

栈的反转:

public class StackX {
  private char[] a;
  private int size;
  private int top;
  public StackX(int num){
	  a=new char[num];
	  top=-1;
	  size=0;
  }
  
  public void push(char c){
	  top++;
	  size++;
	  a[top]=c;
  }
  
  public char  pull(){
	  size--;
	  return a[top--];
  }
  
  public char getTop(){
	  return a[top];
  }
  
  public boolean isEmpty(){
	  if(top==-1) return true;
	  else return false;  //return top==-1;
  }

  public int getSize(){
	return size;
  }
  
  public void display(){
	  for(int i=size-1;i>=0;i--)
		  System.out.println(a[i]);
  }
}

public class StackReverse {

   public static StackX Reverse(StackX p){
	   int num=p.getSize();
	   StackX p1=new StackX(num);
	   char[] x=new char[num];
	   for(int i=0;i<num;i++){
		   x[i]=p.pull();
	   }
	   for(int i=0;i<num;i++){
		   p1.push(x[i]);
	   }
	   return p1;
   }
}

public class StackApp {
  public static void main(String[] args) {
	  StackX arr=new StackX(10);
	  arr.push('y');
	  arr.push('a');
	  arr.push('n');
	  arr.push('g');
	  arr.push('c');
	  arr.pull();
	  StackX p1=StackReverse.Reverse(arr);
	  p1.display();
  }
}

栈的分隔符匹配:

  public class StackX {
	  private char[] a;
	  private int msize;
	  private int top;
	  public StackX(int max){
		  msize=max;
		  a=new char[msize];
		  top=-1;
	  }
	  
	  public void push(char value){
		  top++;
		  a[top]=value;
	  }
	  
	  public char pull(){
		  return a[top--];
	  }
	  
	  public void dispaly(){
		  for(int i=top;i>-1;i--){
			  System.out.println(a[i]);
		  }
	  }
	  
	  public boolean isEmpty(){
		  if(top!=-1) return false;
		  else return true;
	  }
	  
	  public boolean isFull(){
		  if(top==msize-1) return true;
		  else return false;
	  }
	}

public class StackBra {
  private String input;
  public StackBra(String in){
	  input=in;
  }
  
  public  void check(){
	  int StackSize=input.length();
	  StackX theStack=new StackX(StackSize);
	  //把{[()]}放到栈里
	  for(int i=0;i<StackSize;i++){
		  char ch=input.charAt(i);
		  switch(ch){
		  case '{':
		  case '[':
		  case '(':
			  theStack.push(ch);
		  break;
		  case '}':
		  case ']':
		  case ')':
			  if(!theStack.isEmpty()){
				  char p=theStack.pull();
				  if((ch=='}'&& p!='{') ||(ch==']'&& p!='[') || (ch==')'&& p!='(')) {
					  System.out.println("error");
				  }
			  }
			  else System.out.println("error");
		  default:
			  break;
		  }
	  }
	  if(!theStack.isEmpty()) System.out.println("error:loss right");
	  else System.out.println("success");
	}
}

import java.io.*;
public class StackBraApp {
   public static void main(String[] args) throws IOException{
	   System.out.println("Input String:");
	   String input=getString();
	   StackBra test=new StackBra(input);
	   test.check();
   }
   
   public static String getString() throws IOException{
	   InputStreamReader re=new InputStreamReader(System.in);
	   BufferedReader br=new BufferedReader(re);
	   String s=br.readLine();
	   return s;
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值