2 Replies - 189 Views - Last Post: 07 June 2014 - 03:49 PM Rate Topic: -----

#1 mochajava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-June 14

writing indexOf() method for more than one instance in a linked list

Posted 07 June 2014 - 02:35 PM

I'm trying to write an indexOf() method that will return every time a value occurs in a linked list. I need to use my user-created linked list not the built in Java linked list. For example in a linked list of characters: "i, p, z, z, n, d, p, z" when I search for "z" it should return position variables for 3, 4, and 8. Currently what I have is obviously only returning the first instance. Does anyone have any suggestions for how I can return more than one instance?
public int indexOf(char input) {
        LLNode currentNode = this.first;
        int position =1;
        boolean found = false;

        for (; ; position++) {
          if (currentNode == null) {
            break;}

          if (input==currentNode.getInfo()) {
        	found = true;
            break;
          }

          currentNode = currentNode.getLink();
        }

        if (!found) {
          position = -1;
        }
       
        return position;
        
      }



Is This A Good Question/Topic? 0
  • +

Replies To: writing indexOf() method for more than one instance in a linked list

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: writing indexOf() method for more than one instance in a linked list

Posted 07 June 2014 - 02:39 PM

Your method returns int, so only one value. To get more than one values returned, you will need to return a list of integers or so... You will also then have to change the loop not to break when it finds the node.
Although most of indexOf() implementation I know of returns only the first occurrence.

This post has been edited by smohd: 07 June 2014 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 jacobTheMechanic  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 89
  • Joined: 18-September 12

Re: writing indexOf() method for more than one instance in a linked list

Posted 07 June 2014 - 03:49 PM

How about a stack? And return an array of your index ints?

public int[] indexOf(char input) {
	int[] indexOf;
	Stack<Integer> stack = new Stack<>();
        LLNode currentNode = this.first;
        int position =1;
        boolean found = false;

        for (; ; position++) {
          if (currentNode == null) {
            break;}

          if (input==currentNode.getInfo()) {
        	found = true;
		stack.push(position);

            //break;
          }

          currentNode = currentNode.getLink();
        }
	indexOf = new int[stack.size()];
	for(int i=0; i<indexOf.length; i++){
		indexOf[i] = stack.pop();
	}

        if (!found) {
          position = -1;
        }
       
        return indexOf;
        
      }


This post has been edited by jacobTheMechanic: 07 June 2014 - 03:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1