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
结果