import java.util.*; public class Main { private static Node linkedlist1_FNode; private static Node linkedlist1_LNode = null; private static Node linkedlist2_FNode; private static Node linkedlist2_LNode = null; public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); System.out.printf("Enter first string:"); String str1 = input.next(); System.out.printf("\nEnter second string:"); String str2 = input.next(); char[] ch1 = str1.toCharArray(); char[] ch2 = str2.toCharArray(); for(int i = 0; i < str1.length(); i++){ create(ch1[i], 1); } for(int j = 0; j < str2.length(); j++){ create(ch2[j], 2); } display(1); display(2); append(); //subString(); } public static void create(char ch, int linkedlist){ //inserts node into list if(linkedlist == 1){ Node n1 = new Node(ch, null); //create node if(linkedlist1_LNode != null){ //if it's not the first node linkedlist1_LNode.setNext(n1); linkedlist1_LNode = n1; } else{ //if n is the first node linkedlist1_FNode = n1; linkedlist1_LNode = n1; } } if(linkedlist == 2){ Node n2 = new Node(ch, null); //create node if(linkedlist2_LNode != null){ //if it's not the first node linkedlist2_LNode.setNext(n2); linkedlist2_LNode = n2; } else{ //if n is the first node linkedlist2_FNode = n2; linkedlist2_LNode = n2; } } } public static void display(int linkedlist){ //display all the data in the nodes if(linkedlist == 1){ System.out.print("Linked list for first string: \n"); Node n1 = linkedlist1_FNode; while(n1!=null){ //loops forward displaying nodes data System.out.print(n1.getWord()+ " --> "); n1 = n1.getNext(); //move to next node in the list } System.out.print("null"); } if(linkedlist == 2){ System.out.print("\nLinked list for second string: \n"); Node n2 = linkedlist2_FNode; while(n2!=null){ //loops forward displaying nodes data System.out.print(n2.getWord()+ " --> "); n2 = n2.getNext(); //move to next node in the list } System.out.print("null"); } } public static void append(){ Node n2 = linkedlist2_FNode; char x; while(n2!=null){ x = n2.getWord(); //nodes data placed in a variable Node n1 = new Node(x, null); if(linkedlist1_LNode != null){ //if it's not the first node linkedlist1_LNode.setNext(n1); linkedlist1_LNode = n1; } n2 = n2.getNext(); //move to next node in the list } System.out.print("\nAppend : \n"); display(1); //linkedlist2_FNode=null; //display(2); } public class Node{ private char letter; private Node next; //reference to the next node public Node(char letter, Node next){ //constructor this.letter = letter; this.next = next; } public void setWord(char letter){ this.letter = letter; } public char getWord(){ return letter; } public void setNext(Node next){ this.next = next; } public Node getNext(){ return next; } }
I've gone as far as this..........I need some help with the logic for finding whether string2 is sub-string of string1..........