Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linked Lists?

Status
Not open for further replies.

jisoo22

Programmer
Apr 30, 2001
277
US
Hello all =)

I'm working on a rather "simple" programming project this time. I believe I have a firm idea how I want to do it but not sure how to program it exactly. Here's what I'm trying to do:

My small program will take in a string argument, "yabba dabba dabba do" for instance. The output after running the program will eliminate duplicate words and add parantheses to each end, i.e. "(yabba dabba do)" will be the result. I know you can probably do this using the Vector class easily but I'm not allowed to use it, rather I have to create my own methods called "insert" and "toString".

Here is my code thus far (not much):

Code:
// A mutable list of Strings without duplicates

class UniqList {

    class Item {
        String s;
        Item next;
        public Item( String _s,  Item _n ) {
            s = _s;
            next = _n;
        }
    }


    Item head;

    public void insert( String s ) {
        // Start of my code for this method
        String temp = "";
        for (int i = 0; i < s.length(); i++;)
        	if Character.isDigit(s.charAt(i)))
        		temp = temp + s.charAt(i);
        	else
        		if ( s.substring



    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(&quot;(&quot;);
        // Start of my code for this method


        sb.append(&quot;)&quot;);
        return sb.toString();
    }

    public static void main( String args[] ) {
        UniqList l = new UniqList();
        for (int i=0; i < args.length; ++i ) {
            l.insert(args[i]);
        }
        System.out.println(l.toString());
    }
}

What I was thinking for the &quot;insert&quot; method would be like this (pseudo-code of course)...

Code:
for (int i = 0; i < s.length(); i++)
  string temp = &quot;&quot;;
  if a character at position i is a digit
    concatenate it to temp.
  else
    since it's not a digit (rather a space)
    compare the substring of the word that's
    now been copied into temp and compare with
    substrings of equal length in the full s string.
      if there is no match
        leave it in the temp string
      else there is a match
        rid it from the temp string

Now I could be going about this the wrong way, I've been racking my brains because I can't seem to figure out how to create a substring in the temp string of just the recently recorded set of consequtive characters or word. Can anyone give me an idea of how to do that? Or am I making this way too hard and there is a better way to go about it?

Thanks,
Jisoo22
 
You could possibly use a StringTokenizer to split the input string into tokens, and then insert them into your list as long as they are unique. Just a thought.
 
To all students monitoring...

This a better way to get help for school work. NOT asking for code, but ask for ideas on how to code.

jisoo22,
As far as your question, what, egobbitz suggested I would think would be a good place to start. Mike Wills
IBM iSeries (AS/400) Programmer
[pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top