知识点

背包:只进不出,迭代顺序不确定(即无先后顺序)

队列:先进先出

栈   :后进先出

两种基础数据结构:数组和链表

数据结构 优点 缺点
数组 通过索引可以访问任意元素 在初始化时就需要知道元素的数量
链表 使用的空间大小和元素数量成正比 需要通过引用访问任意元素

 

练习

1.3.1 为FixedCapacityStackOfStrings添加一个方法isFull().       /*其中注意N的含义,别和数组下标完全混淆*/

public class FixedCapacityStackOfStrings
{
    private String[] a;
    private int N;
    public FixedCapacityStackOfStrings(int cap)
    {
        a=new String[cap];
    }
    public boolean isEmpty()
    {
        return N==0;
    }
    public boolean isFull()    //检查是否已满
    {
        return N==a.length;
    }
    public int size()
    {
        return N;
    }
    public void push(String item)
    {
        a[N++]=item;
    }
    public String pop()
    {
        return a[--N];
    }
    
    public static void main(String[] args) throws IOException
    {
        FixedCapacityStackOfStrings s=new FixedCapacityStackOfStrings(3);
        while(!s.isFull())
            s.push("first");
        while(!s.isEmpty())
            StdOut.println(s.pop());
    }
}

 

 

1.3.3 假设某个用例程序会进行一系列入栈和出栈的混合操作.入栈操作会将整数0到9按顺序压入栈;出栈操作会打印出返回值.下面哪种序列是不可能产生的.

(a)  4 3 2 1 0 9 8 7 6 5   

(b)  4 6 8 7 5 3 2 9 0 1

(c)  2 5 6 7 4 8 9 3 1 0

(d)  4 3 2 1 0 5 6 7 8 9

(e)  1 2 3 4 5 6 9 8 7 0

(f)  0 4 6 5 3 8 1 7 2 9

(g)  1 4 7 9 8 6 5 3 0 2

(h)  2 1 4 3 6 5 8 7 9 0

其中  b---(0,1出错)  f---(1,7,2出错)  g---(0,2出错)

/*是否可以构造一个函数来进行判断?---找出其中遵守的规律?*/

1.3.4 编写一个Stack的用例Parentheses,从标准输入中读取一个文本流并使用栈判定其中的括号是否配对完整.例如[()]{}{[()]}为true,对于[(])程序则打印false.

个人编写的版本(较麻烦,且需要文本中括号要用空格分隔)

public class H1_3_04
{
    public static void main(String[] args)
    {
        In in=new In();
        @SuppressWarnings("deprecation")
        String[] arr=In.readStrings("h1_3_4.txt"); //注:其中文本中用空格分隔每个字符串,如[ ( ) ]
        for(String s:arr)
            StdOut.printf("%s",s);
        StdOut.println(check(arr));
        in.close();
    }
    
    public static boolean check(String[] arr)   //循环判断每个字符串
    {
        Stack<String> stack=new Stack<String>();
        for(int i=0;i<arr.length;i++)
        {
            if(check(arr[i],stack)==false)
                return false;
        }
        return true;
    }
    
    public static boolean check(String a,Stack<String> stack)  //判断每个右括号是否有匹配的左括号
    {
        switch (a)
        {
        case "]":
            while(true)
            {
                if(stack.isEmpty())
                    return false;
                if(stack.pop().equals("["))
                    return true;
            }
        case ")":
            while(true)
            {
                if(stack.isEmpty())
                    return false;
                if(stack.pop().equals("("))
                    return true;
            }
        case "}":
            while(true)
            {
                if(stack.isEmpty())
                    return false;
                if(stack.pop().equals("{"))
                    return true;
            }
        default:
            stack.push(a);
            return true;
        }
    }
}
View Code

相关文章:

  • 2021-08-23
  • 2021-11-23
  • 2021-05-03
猜你喜欢
  • 2021-10-01
  • 2021-10-22
  • 2022-12-23
  • 2022-12-23
  • 2021-05-29
  • 2022-03-03
  • 2021-07-18
相关资源
相似解决方案