1.3.44

question:

Text editor buffer. Develop a data type for a buffer in a text editor in a text editor that implements the following API:

public class Buffer
             Buffer()        //create an empty buffer
        void insert(char c)  //insert at the cursor position
        char delete()        //delete and return the character at the cursor
        void left(int k)     //move the cursor k positons to the left
        void right(int k)    //move the cursor k positions to the right 
         int size()          //number of characters in the buffer

answer:

import edu.princeton.cs.algs4.*;

public class editorBuffer<Item>
{
    
    private class Stack<Item>
    {
         private class Node
         {
             Item item;
             Node next;
         }
         
         private Node top;
         private int N;
    
         public Stack()
         {
             top = null;
             N = 0;
         }
    
         public int size()
         {
             return N;
         }
         
         public boolean isEmpty()
         {
             return N == 0;
         }
        
         public void push(Item item)
         {
             Node node = new Node();
             node.item = item;
             node.next = null;
             if(N == 0)
             {
                 top = node;
                 N++;
                 return;
             }
             N++;
             node.next = top;
             top = node;
         }
    
         public Item pop()
         {
             if(N == 0)
             {
                 return null;
             }
             N--;
             Item item = top.item;
             top = top.next;
             return item;
         }
    }
    //以上是栈类,直接看下面
    private Stack<Item> stack1;//用于输入
    private Stack<Item> stack2;//用于调顺序
    
    public editorBuffer()
    {
        stack1 = new Stack<Item>();
        stack2 = new Stack<Item>();
    }
        
    public void insert(Item c)//输入到stack1里面
    {
        stack1.push(c);
    }
    
    public Item delete()//在stack1里面删除
    {
        Item item = stack1.pop();
        return item;
    }
    
    public void left(int k)//把stack1里面k个元素放到stack2里面
    {
        int c = stack1.size();
        c = c < k ? c : k;//防止k大于stack1的size
        for(int i = 0; i < c; i++)
        {
            stack2.push(stack1.pop());
        }
    }
    
    public void right(int k)//把stack2里面k个元素放到stack1里面
    {
        int c = stack2.size();
        c = c < k ? c : k;//防止k大于stack2的size
        for(int i = 0; i < c; i++)
        {
            stack1.push(stack2.pop());
        }
    }
    
    public int size()
    {
        return stack1.size() + stack2.size();
    }
    
    public void cleanBuffer()//便于检查结果,全放到stack2里面再全pop出来,保证顺序
    {
        while(!stack1.isEmpty())
        {
            stack2.push(stack1.pop());
        }
        while(!stack2.isEmpty())
        {
            StdOut.print(stack2.pop() + " ");
        }
        StdOut.print("\n");
    }
    
    public static void main(String[] args)
    {
        editorBuffer<Character> buffer = new editorBuffer<Character>();
        buffer.insert('c');
        buffer.left(4);
        buffer.insert('w');
        buffer.insert('j');
        buffer.right(1);
        buffer.insert('!');
        buffer.cleanBuffer();
    }
}

猜你喜欢

转载自www.cnblogs.com/w-j-c/p/9090864.html