Dear Experts,
I am new to the LinkedList concept and trying out coding a LinkedList problem which require me to do Insert newInteger based on the Index, Remove Index and Change oldInteger to newInteger based on Index.
Please kindly advice what i should code in my
public void insert(int index, int newInteger) {
// implementation
}
public void remove(int index) {
// implementation
}
public void change(int index, int newInteger) {
// implementation
}
------------------------------------------------------------------------------------
Sample input
4 8
1 5 2 3
4
I 1 2
R 2
C 1 10
I 4 2
Sample output
YES
NO
NO
NO
------------------------------------------------------------------------------------
Here is my full code:
import java.util.*;
//use ListNode to represent the integers.
class ListNode {
protected Object element;
protected ListNode next;
public ListNode(Object item){
element = item;
next = null;
}
//declare constructors
public ListNode(Object item, ListNode n){
element = item;
next = n;
}
//get the next list node
public ListNode getNext(){
return this.next;
}
public Object getElement(){
return this.element();
}
}
class LinkedList {
// declare the member field
protected ListNode head = null;
protected int num_nodes = 0;
public boolean isEmpty(){
return (num_nodes == 0);
}
/* add: add a listNode to the linklist
* PRE-Condition :
* POST-Condition :
*/
public void add(ListNode listNode) {
// implementation
head = new ListNode (item, head);
num_nodes ++;
}
/* insert: insert a newInteger at index
* PRE-Condition :
* POST-Condition :
*/
public void insert(int index, int newInteger) {
// implementation
}
/* remove: remove the element at index
* PRE-Condition :
* POST-Condition :
*/
public void remove(int index) {
// implementation
}
/* change: change the integer at index with newInteger
* PRE-Condition :
* POST-Condition :
*/
public void change(int index, int newInteger) {
// implementation
}
/* isBetter: to compare between this linkedList with prevLinkedList
* PRE-Condition :
* POST-Condition :
*/
public String isBetter(LinkedList prevLinkedList) {
// picking the integers in the newLinkedList and find out the difference between integers in the prevLinkedList
// and sum the difference up.
// if the sum of difference is more than K, return "YES"
// else return "NO"
int ans = 0;
for (int i=0; i<prevLinkedList.size(); i ++){
prevLinkedList.element - element = ans;
}
return "YES";
}
}
public class Main {
public static void main(String[] args) {
// declare the necessary variables
//int [] array;
LinkedList <Integer>list = new LinkedList<Integer>();
int N; //indicate the size of array
int K; //indicate the citeria
int Q; //indicate the no.of entry
String operator; //indicate if it is for insert, remove or change
int index; //indicate the first input of operator
int newInteger; //indicate the second input of operator
int sum;
//declare a Scanner object to read input
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
K = sc.nextInt();
list = new int [N];
for (int i=0; i<list.size(); i++){
list.add(sc.nextInt());
}
Q = sc.nextInt();
for (int i=0; i<Q; i++){
operator = sc.next();
if(operator = "I"){
index = sc.nexInt();
newInteger = sc.nextInt();
//int post = index -1;
}
else if (operator = "R"){
index = sc.nextInt();
}
else if (operator = "C"){
index = sc.nextInt();
newIntegter = sc.nexInt();
//int post = index -1;
}
}
System.out.println(list.isBetter);
}
}