How to Ensure Order in LRStruct with addInOrder Method?

  • Thread starter Thread starter MFlood7356
  • Start date Start date
  • Tags Tags
    Test
Click For Summary
SUMMARY

The discussion focuses on implementing the addInOrder method for the LRStruct class to ensure that strings are added in the same order as they appear in the input list. The provided test case confirms that if the input is "a", "b", and "c", the output must also be "(a b c)". The initial implementation fails to add all elements due to a static counter and lacks a loop structure, which is essential for iterating through the input list.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with data structures, specifically linked lists
  • Knowledge of Java collections, particularly List interface
  • Experience with unit testing in Java using JUnit
NEXT STEPS
  • Implement a loop structure in the addInOrder method to iterate through the input list
  • Explore Java's for and while loop constructs for effective iteration
  • Review the LRStruct class documentation for additional methods and functionalities
  • Learn about JUnit assertions to enhance testing practices
USEFUL FOR

Java developers, software engineers, and students learning data structures who are looking to implement ordered insertion in custom data structures.

MFlood7356
Messages
37
Reaction score
0
1. Define this method so that it adds the Strings in inputto an initially empty LRStruct<String> so that they appearin the same order. Example: if input contains "a", "b" and "c", in that order, the returned list must contain "a", "b" and "c", in that order.

LRStruct Class: http://www.cs.rice.edu/~mgricken/research/a4obj1st/tchjava/Rice_MBS/RiceMBS.student/docs/lrs/LRStruct.html

Test:
Code:
@Test
	public void testABC() {
		_input.add("a");
		_input.add("b");
		_input.add("c");
		LRStruct<String> answer = _fwl.addInOrder(_input);
		String expected = "(a b c)";
		String actual = answer.toString();
		assertTrue("I thought answer would be "+expected+" but it was "+actual, expected.equals(actual));
	}
2. Here's my attempt at the method addInOrder. All I'm getting is the string c to add to the LRStruct. Not sure why my if statement isn't working correctly.

Code:
public LRStruct<String> addInOrder(List<String> input) {
	
		LRStruct<String> lrstruct = new LRStruct<String>();
		int counter = 2;
		
		if(counter>=0){
			lrstruct.insertFront(input.get(counter));
			counter--;
		}
		
		return lrstruct;
	}
 
Physics news on Phys.org
I think what you're looking for is a loop. I.e.:
Code:
int counter = 2;

if(counter>=0){
			lrstruct.insertFront(input.get(counter));
			counter--;
		}
will do the exact same thing as
Code:
lrstruct.insertFront(input.get(2));
 
Okay I understand that but that's the exact same thing I wrote. That's not helping me.
 
You need a loop

Doesn't Java have while loops, or for loops? Or for-each loops?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
6K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 16 ·
Replies
16
Views
5K
Replies
58
Views
13K