-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartF.java
More file actions
141 lines (101 loc) · 3.82 KB
/
PartF.java
File metadata and controls
141 lines (101 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.employee;
public class PartF {
SingleLinkedList empList = new SingleLinkedList();
public SingleLinkedList newEmployeeStructure(PermanentEmployee[] perm) {
// ************************************ TODO F2 ************************************
/*
* This method should form a linkedlist containing objects of type
* PermanentEmployee and should return the linkedlist
*
* You are required to implement LinkedList from scratch and add all the objects
* of perm array to your linked List
*
* Uncomment line "TestYourCode.testNewEmployeeStructure(tail)" to test your code
* Note : this method takes the tail of type Node of your linkedList as a parameter.
*
*/
// ************************************ SOLUTION F2 BEGIN ************************************
for(int i =0 ; i< perm.length ; i++) {
empList.addNode(perm[i]);
}
int lastEmployeeId = 0;
if(empList.tail.next == null) {
lastEmployeeId = empList.tail.employee.getId();
System.out.println(lastEmployeeId);
}
// Uncomment the line below to test your code
TestYourCode.testNewEmployeeStructure(lastEmployeeId);
return empList;
// ************************************ SOLUTION F2 END ************************************
}
public SingleLinkedList addNewEmployee(SingleLinkedList empList, PermanentEmployee perm) {
// ************************************ TODO F3 ************************************
/*
* This method should add an employee in the linkedlist
* and return the updated linkedlist
* Note : the linked list remains ordered based on the permanent Ids
*
* Uncomment line "TestYourCode.testAddNewEmployee(position)" to test your code
* Note : this method takes the position of type integer at which the object is added as a parameter
*
*/
// ************************************ SOLUTION F3 BEGIN ************************************
empList.addNode(perm);
Node temp = empList.head;
int position = 1;
if (perm.getId() < temp.employee.getId()) {
Node temp1 = empList.head;
empList.head = new Node(perm);
empList.head.next = temp1;
} else {
while (temp.next != null && temp.next.employee.getId() < perm.getId()) {
position++;
temp = temp.next;
}
Node temp2 = temp.next;
temp.next = new Node(perm);
temp.next.next = temp2;
if (temp.next.next == null) {
empList.tail = temp.next;
}
position++;
System.out.println(position);
}
// Uncomment the line below to test your code
TestYourCode.testAddNewEmployee(position);
return empList;
// ************************************ SOLUTION F3 END ************************************
}
public SingleLinkedList removeAbscondedEmployee(SingleLinkedList empList) {
// ************************************ TODO F4 ************************************
/*
* This method should remove an employee from the linkedlist
* Note : Your task is to print the id of the employee that gets removed.
* This method should return the updated linkedlist
*
* Uncomment the line "TestYourCode.testRemoveAbscondedEmployee(abscondedEmployee)" to test your code
* Note : this method takes abscondedEmployee of type Node as a parameter.
*/
int len = 0;
Node temp = empList.head;
Node temp1 = empList.head;
while(temp != null)
{
temp = temp.next;
len++;
}
int i=2, abscondedEmployee;
while(i < len-4 && temp1 != null)
{
temp1 = temp1.next;
i++;
}
abscondedEmployee = temp1.next.employee.getId();
System.out.println(abscondedEmployee);
// ************************************ SOLUTION F4 BEGIN ************************************
// Uncomment the line below to test your code
TestYourCode.testRemoveAbscondedEmployee(abscondedEmployee);
return empList;
// ************************************ SOLUTION F4 END ************************************
}
}