英文:
How do i Modify the append and insert operations in a linked list?
问题
以下是翻译后的内容:
class Node {
Node next;
int num;
public Node(int val) {
num = val;
next = null;
}
}
public class LinkedList {
Node head;
public LinkedList(int val) {
head = new Node(val);
}
public void append(int val) {
Node tmpNode = new Node(val);
tmpNode.num = val;
tmpNode.next = head;
head = tmpNode;
}
public void insert(int val) {
Node currentNode = head;
Node nextNode = head.next;
if (currentNode.num < val) {
Node tmpNode = head;
head = new Node(val);
head.next = tmpNode;
return;
}
if (nextNode != null && nextNode.num < val) {
currentNode.next = new Node(val);
currentNode.next.next = nextNode;
return;
}
while (nextNode != null && nextNode.num < val) {
currentNode = nextNode;
nextNode = nextNode.next;
}
currentNode.next = new Node(val);
currentNode.next.next = nextNode;
}
public void delete(int val) {
Node prevNode = null;
Node currNode = head;
if (head.num == val) {
head = head.next;
return;
}
while (currNode != null && currNode.num != val) {
prevNode = currNode;
currNode = currNode.next;
}
if (currNode == null) {
System.out.println("A node with that value does not exist.");
} else {
prevNode.next = currNode.next;
}
}
public void print() {
Node tmpNode = head;
while (tmpNode != null) {
System.out.print(tmpNode.num + " -> ");
tmpNode = tmpNode.next;
}
System.out.print("null" + "\n");
}
public static void main(String[] args) {
LinkedList myList = new LinkedList(2);
myList.append(5);
myList.insert(10);
myList.insert(4);
myList.print();
}
}
请注意,我只是按照你的要求翻译了代码部分,没有包含其他的内容。如果你有任何问题或需要进一步的帮助,请随时提问。
英文:
Hello i have an Assignment to modify a java program :
1- Makeing the Append operation be always in the front of the List :
LinkedList myList = new LinkedList(5);
myList.append(7);
myList.print();
it should be like this :
7 -> 5 -> null
instead of :
5 -> 7 -> null
2- Insert operation will be always as follows: any new added element using insert operation
should be after the current maximum element in the List
LinkedList myList = new LinkedList(2);
myList.append(5);
myList.insert(1);
myList.insert(4);
myList.print();
it should be :
5 -> 4 -> 1 -> 2 -> null
instead of :
5 -> 4 -> 2 -> 1 -> null
here is the code
class Node {
Node next;
int num ;
public Node(int val) {
num = val;
next = null;
}
}
public class LinkedList {
Node head;
public LinkedList(int val) {
head = new Node(val);
}
public void append(int val) {
Node tmpNode = new Node(val);
tmpNode.num = val;
tmpNode.next = head;
head = tmpNode;
}
public void insert(int val) {
Node currentNode = head;
Node nextNode = head.next;
if (currentNode.num < val) {
Node tmpNode = head;
head = new Node(val);
head.next = tmpNode;
return;
}
if (nextNode != null && nextNode.num < val) {
currentNode.next = new Node(val);
currentNode.next.next = nextNode;
return;
}
while (nextNode != null && nextNode.num < val) {
currentNode = nextNode;
nextNode = nextNode.next;
}
currentNode.next = new Node(val);
currentNode.next.next = nextNode;
}
public void delete(int val) {
Node prevNode = null;
Node currNode = head;
if (head.num == val) {
head = head.next;
return;
}
while (currNode != null && currNode.num != val) {
prevNode = currNode;
currNode = currNode.next;
}
if (currNode == null) {
System.out.println("A node with that value does not exist.");
} else {
prevNode.next = currNode.next;
}
}
public void print() {
Node tmpNode = head;
while (tmpNode != null) {
System.out.print(tmpNode.num + " -> ");
tmpNode = tmpNode.next;
}
System.out.print("null" + "\n");
}
public static void main(String[] args) {
LinkedList myList = new LinkedList(2);
myList.append(5);
myList.insert(10);
myList.insert(4);
myList.print();
}
}
I have tried to do it but i am not sure i did it right
Thanks for your time !
专注分享java语言的经验与见解,让所有开发者获益!
评论