wnameless 2021-10-31 00:23 采纳率: 0%
浏览 25

数据结构问题?依据ListADT实现List的数据结构(输入数据是char类型)读入testCase.txt,输出result.txt使用StreamTokenizer辅助完成。


import java.io.*;

public class LList {
    static class Link{
        private char element;
        private Link next;
        Link(char it,Link nexvavl){
            element=it;
            next=nexvavl;
        }
        Link(Link nexvavl){
            next=nexvavl;
        }
        Link next(){
            return next;
        }
        Link setNext(Link nextval){
            return next=nextval;
        }
        char element(){return element;}
        char setelement(char it){return element=it;}
    }

    static class CharList  implements ListADT.List{
        private Link head;
        private Link tail;
        protected Link curr;

        CharList() {
          head=new Link(null);
          curr=tail=head;
        }

        @Override
        public void insert(Object newElement) {
            curr.setNext(new Link((char) newElement,curr.next));
            if(tail==curr)tail=curr.next;
        }

        @Override
        public void replace(Object newElement) {
            curr.element=(char) newElement;
        }

        @Override
        public void clear() {
            head=new Link(null);
            curr=head;
            tail=head;
            System.out.print("Empty list -1\n");
        }

        @Override
        public boolean isEmpty() {
            return head==tail;
        }

        @Override
        public boolean isFull() {return false;}

        @Override
        public boolean gotoBeginning() {
            if (!isEmpty())curr=head.next;
            return !isEmpty();
        }

        @Override
        public boolean gotoEnd() {
            if (!isEmpty())curr=tail;
            return !isEmpty();
        }

        @Override
        public boolean gotoNext() {
            if(curr.next!=null)curr=curr.next;
            return curr.next!=null;
        }

        @Override
        public boolean gotoPrev() {
            if (head!=curr){
                Link temp=head;
                while (temp.next!=curr)temp=temp.next;
                this.curr=temp;
                return true;
            }
            return false;
        }

        @Override
        public Link getCursor() {
            if (!isEmpty()){
                return curr;
            }
            return null;
        }

        @Override
        public void showStructure() {
            int diction=0;
            Link temp=head.next;
            if(temp==null){
                clear();
                return;
            }
            while (temp.next!=null){
                if(temp!=curr)diction++;
                else System.out.print(diction);
                System.out.print(temp.element()+" ");
                temp=temp.next;
            }
            System.out.print(temp.element()+" ");
            this.clear();
        }

        @Override
        public void remove() {
            if (!isEmpty()){
                if (head!=curr){
                    Link temp=head;
                    while (temp.next!=curr)temp=temp.next;
                    this.curr=temp;
                    curr.next=curr.next.next;
                }
            }
        }
    }


    public static void main(String[] args) throws IOException {
        //读入文件
        String filePath="D:\\projects\\Java\\homework\\homework3\\src\\testCase.txt";
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new FileReader(filePath)));

       
        CharList myList=new CharList();
        in.eolIsSignificant(true);//将行尾视为标记
        in.quoteChar(' ');//按照空格分割


        int token=in.nextToken();
        while (token != StreamTokenizer.TT_EOF){//到达文档尾
            if (token==StreamTokenizer.TT_EOL)myList.showStructure();
            char content=(char) (in.ttype);
                switch (content) {
                    case '+' -> {in.nextToken();myList.insert(in.sval.charAt(0));}
                    case '-' -> myList.remove();
                    case '=' -> {in.nextToken();myList.replace(in.sval.charAt(0));}
                    case '#' -> myList.gotoBeginning();
                    case '*' -> myList.gotoEnd();
                    case '>' -> myList.gotoNext();
                    case '<' -> myList.gotoPrev();
                    case '~' -> myList.clear();
                }
            token=in.nextToken();
        }
    }
}

public class ListADT {
    //List ADT
    public interface List<T>{
        void insert(T newElement);
        void replace(T newElement);
        void clear();
        boolean isEmpty();
        boolean isFull();
        boolean gotoBeginning();
        boolean gotoEnd();
        boolean gotoNext();
        boolean gotoPrev();
        T getCursor();
        void showStructure();
        void remove();
    }
    }


//testCase.txt
< < +s +w =P =R +i +e +d - +n < < +j +z =J +a +n +j +c > - +t +f * ~ +h +k =K +i =A > +i +s =F * =Y < +w * - * > > > +z +m +b > < # +u +q +p +r # +m +d +d # +f +u # - - +b +b +b > > - # < > +i +x

img

img

img

img

img


结果

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-sinJack 2021-10-31 00:25
    关注

    截图不要放在代码块中,这样看不了。

    评论

报告相同问题?

问题事件

  • 修改了问题 10月31日
  • 创建了问题 10月31日

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding