import java.util.Scanner;
public class faisal222{
public static void main(String[]args){
Scanner sca=new Scanner(System.in);
SLL letterList= new SLL();
DLL wordList=new DLL();
int selectout=0;
int selectin=0;
String input;
Scanner sc=new Scanner(System.in);
while(selectout!=6){
System.out.println("1-Print dictionary \n2-Search for word\n3-Insert word\n4-Delete word"+
"\n5-print report of the words"+
"\n6-exit");
System.out.print("\nYour select:");
selectout=sca.nextInt();
switch(selectout){
case 1:
System.out.println("Enter letter you want to print its words:");
char ins=sc.next().charAt(0);
SLLNode current=new SLLNode(ins,null);
letterList.insert(ins,null);
System.out.println("1-Print Forward List\n2-Print Backward List");
System.out.print("Choice:");
selectin=sc.nextInt();
if(selectin==1){
wordList.printForward();}
else wordList.printBackward();
break;
case 2:
System.out.println("Enter word that you search for:");
String search=sc.next();
wordList.searchWord(search);
System.out.println();
break;
case 3:
System.out.println("1-Enter letter that word start with it:");
System.out.println("Letter:");
char insletter=sc.next().charAt(0);
SLLNode current2=new SLLNode(insletter,null);
letterList.insert(insletter,null);
System.out.println("Word:");
input=sc.next();
DLLNode<String> insword=new DLLNode<String>(input);//create the node to be inserted
wordList.insertWord(insword,null);
break;
case 4:
System.out.println("Word to be deleted:");
input=sc.next();
DLLNode del=wordList.findWord(input);//node to delete
if(del==null)
System.out.println("Error:Word doesn't exist!");
else
wordList.deleteWord(del);
break;
case 5:
System.out.println("Total number of words:"+wordList.getSize());
letterList.printList();
break;
case 6:
System.exit(0);
break;
default: System.out.println("invalid input, try again");
}
}
}
}
/*******************************************************************/
class SLL{
SLLNode first;
public SLL(){
first= null;
}
public void printList(){
if(first==null) //letterList is empty
System.out.println("dictionary is empty!");
else{//print letts one by one
SLLNode current=first;
while(current!=null){
System.out.println(current.lett+":");
current=current.next;
}
System.out.println();
}
}
public void insert(char lett,SLLNode previous){
SLLNode ins = new SLLNode(lett,null);
if(previous==null){
ins.next=first;
first=ins;}
else{
ins.next=previous.next;
previous.next=ins;
}
}
}
/*******************************************************************/
class DLL{
DLLNode first;
DLLNode last;
public DLL(){
first=last=null;
}
public void printForward(){
if(first==null){
System.out.println("The wordList is empty");
}
else{
DLLNode current=first;
while(current!=null){
System.out.print(current.word+" ");
current=current.next;
}
System.out.println("\n");
}
}
public void printBackward(){
if(first==null){
System.out.println("The wordList is empty");
}
else{
DLLNode current=last;
while(current!=null){
System.out.print(current.word+" ");
current=current.previous;
}
System.out.println("\n");
}
}
public DLLNode searchWord(String word){
DLLNode current=first;
if(current!=null && (current.word.equals(word))){
current=current.next;
System.out.println("Search success: Word does exist in dictionary");
}
else
System.out.println("Search success: Word doesn't exist in dictionary");
return current;
}
public DLLNode findWord(String word){
DLLNode current=first;
while(current!=null && !(current.word.equals(word))){
current=current.next;
}
return current;
}
public void insertWord(DLLNode insword,DLLNode pred){
if(pred==null){ //user wants to insert at the beginning
//two cases: 1-empty wordList, 2-not empty
if(first==null)
first=last=insword;
else{
insword.next=first;
first.previous=insword;
first=insword;
}
}
}
public void deleteWord(DLLNode del){
if(del==first){//node to delet is the first one
first=first.next;
//two cases:1-wordList becomes empty, 2-still there are nodes
if(first==null)
last=null;
else
first.previous=null;
}
else if(del==last){//node to delete is the last one
last=del.previous;
last.next=null;
}
else{//delete a node in the middle
DLLNode pred=del.previous;
DLLNode succ=del.next;
pred.next=succ;
succ.previous=pred;
}
}
public int getSize(){
DLLNode current= first;
int size=0;
while(current!=null){
size++;
current=current.next;
}
return size;
}
}
/**************************************************************/
class SLLNode{
char lett;
SLLNode next;
SLLNode privous;
//SLLNode firstWord;
//SLLNode lastWord;
public SLLNode(char lett,SLLNode next){
this.lett=lett;
this.next=next;
//this.SLLNode lastWord=DLLNode previous
//this.SLLNode firstWord=DLLNode next
}
}
class DLLNode<T>{
T word;
DLLNode next; // you can call succ or successor
DLLNode previous; //you can also call pred or predessor
public DLLNode(T word){
next=previous=null;
this.word=word;
}
}