Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > New to Java

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-10-2008, 06:50 AM
Junior Member
 
Join Date: Oct 2008
Posts: 1
oaks12 is on a distinguished road
Question String concat, substring detection using linked list

Java Code:
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..........

Last edited by oaks12; 06-10-2008 at 04:36 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse shortcut keys ejb3 quartz java convert double to binary java forum java forums java jtextarea bold java jtextarea color java jtextarea font java jtextarea font size java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed javapf javaprogrammingforums jtextarea bold jtextarea font jtextarea font color jtextarea font size programing forums programming forums reset jcombobox saaj0008 saaj0008: bad response; bad request saaj0008: bad response; not found severe: saaj0008 severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soap java.security.privilegedactionexception soapexceptionimpl: bad endpoint type textpad java

All times are GMT. The time now is 12:16 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.