Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: String substring, concatenation operation on linked list

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question String substring, concatenation operation on linked list

     
    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; October 5th, 2008 at 11:36 PM.


  2. #2
    Junior Member
    Join Date
    Dec 2008
    Location
    Country Moldova, Causeni town
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: String concat, substring detection using linked list

    Hi Oaks12 !

    You can just to see here : Java Platform SE 6
    --------------------------------------------------------------
    |...............|.Class..String................... ...............................|
    |...............|................................. ...................................|
    |...............|................................. ...................................|
    |java.lang.|...................................... ..............................|
    |-----------|................................................. ...................|
    |...............|...May be .................................................. ....|
    |...............|..- contains ()................................................ .|
    |String.....|..................................... ...............................|
    |...............|................................. ...................................|
    |...............|................................. ...................................|
    --------------------------------------------------------------

    Look at this methods !!!

    Good luck !
    Hi for each Programmer !
    I'm a new programmer in Java and at the moment I have started learning the Java from 0 to Expert !
    Who is with me ? Let's go in the Java world!

    P.S.: Per aspera ad Java ! (Translation from Latine: Through the difficulties to the Java)

Similar Threads

  1. Conversion of string into integer in Java
    By JavaPF in forum Java Programming Tutorials
    Replies: 17
    Last Post: January 23rd, 2010, 08:33 AM
  2. ClassCastException in Double Linked List toString
    By Rastabot in forum Collections and Generics
    Replies: 2
    Last Post: April 24th, 2009, 11:48 AM
  3. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM
  4. How to detect brightest spot among all spots?
    By BharatT in forum Java Theory & Questions
    Replies: 4
    Last Post: February 6th, 2009, 08:12 PM
  5. Replies: 5
    Last Post: January 30th, 2009, 08:31 PM