Hi guys, I am needed some help with a snippet to order a list properly. I have actually turned my use case into a simple employee class for the sake of explaining my issue to everyone.
In the below example, we have four employees, but I want to be able to set a flag on each instance to be able to re-shuffle the order later - I'm not exactly sure how I can get the order to work after the adjustment?
The complexity is employee 1 should print after employee 3. This is easy to adjust, I can just insert into the linked list and remove the first index/employee 1.
But the challenge is that employee 3 should then print after employee 4 - again, easy to make this single adjustment to move 3 to run after 4, BUT then we just set 1 to run after 3 above - and I loose this bit of info. It's kinda like I need some way that when an adjust in the order is made, the code is smart enough to move chunks of employees around rather then swap elements.
Please note that I have kept the initial default order as 1,2,3,4 but the initial default order can be in any order e.g 4,1,3,2 etc - the point of the question is how to adjust the initial default order by flipping values around given the adjustment
I'm a novice at algorithms and data structures and was wondering if someone could help me in the right direction.
Thanks,
Alfa
public static void main(String[] args) { // default order Employee one = new Employee(1); Employee two = new Employee(2); Employee three = new Employee(3); Employee four = new Employee(4); // Add to linked list in the order defined above LinkedList<Employee> list = new LinkedList<>(); list.add(one); list.add(two); list.add(three); list.add(four); System.out.println(list); // prints [1, 2, 3, 4] // set flags to flip the order one.processAfterEmployeeId(3); three.processAfterEmployeeId(4); // this should print [2, 4, 3, 1] as 1 should be AFTER 4, and 3 should be AFTER 4 }
public class Employee { int employeeId; int proessAfterEmployeeId; public Employee(int employeeId) { this.employeeId = employeeId; } public void processAfterEmployeeId(int id) { this.proessAfterEmployeeId = id; } @Override public String toString() { return employeeId + ""; } }