java - 给定链表中的特定节点时如何使用 insertAfter()
问题描述
// insert a new data after the given one
public void insertAfter(int givenData, int newData){
// Your code here
Node previous = new Node(givenData);
if (previous == null) {
//System.out.println("The given previous node cannot be null.");
return;
}
Node newNode = new Node(newData);
newNode.next = previous.next;
previous.next = newNode;
}
// Removes the Node with the given data
public void remove(int current) {
// Your code here
Node previous = new Node(current);
int count = 1;
int position = 0;
while (count < position -1) {
previous = previous.next;
count++;
}
Node curNode = previous.next;
previous.next = curNode.next;
curNode.next = null;
}
此代码没有给出任何错误消息,但不会添加或删除节点。我相信我的困惑在于int givenData以及如果它不是节点如何访问它。第一次在这里发帖,希望我提供了足够的信息:)
public class LinkedListTemplate {
Node head;
// inserts data to the end of the list only using the head pointer
public void append(int data){
Node newNode = new Node(data);
if(head == null){
head = newNode;
} else {
Node currentNode = head;
while(currentNode.next != null){
currentNode = currentNode.next;
}
currentNode.next = newNode;
}
}
// inserts data to the beginning of the list
public void prepend(int data){
if(head == null){
Node newNode = new Node(data);
head = newNode;
return;
}
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
// print the linked list elements
public void print() {
Node currentNode = head;
System.out.printf("[");
while (currentNode.next != null) {
System.out.printf("%d, ", currentNode.data);
currentNode = currentNode.next;
}
System.out.printf("%d]%n", currentNode.data);
}
// counts the length of the list
public int length(){
int length = 0;
Node currentNode = head;
while(currentNode != null){
length++;
currentNode = currentNode.next;
}
return length;
}
// get an item from the list specified by the index
public int get(int index){
int len = length();
if(index > len){
return -1;
}
Node currentNode = head;
int currentIndex = 0;
while(currentNode != null){
if(index == currentIndex){
return currentNode.data;
}
else{
currentNode = currentNode.next;
currentIndex++;
}
}
return -1;
}
我包含了其余的链表代码,还有另一个类可以测试列表并允许它运行。
解决方案
我试了一下。
请注意,我实际上并不知道 remove 真正应该做什么。
您的代码正在寻找具有给定索引的节点,因此我坚持使用该节点(第一个删除版本)。
但评论建议,您需要删除具有给定数据的节点。所以我添加了第二个版本,就是这样做的(第二个删除版本)。
public void insertAfter(int givenData, int newData) {
// Your code here
Node previous = head;
while (null != previous && previous.data != givenData) {
previous = previous.next;
}
// this prevents insertion if given Data was not found
if (previous == null) {
System.out.println("the given data doesn't exist");
return;
}
Node newNode = new Node(newData);
newNode.next = previous.next;
previous.next = newNode;
}
删除具有给定索引的节点的版本:
// Removes the Node with the given index
public void remove(int current) {
Node previous = head;
for (int i = 0; i < current - 1; i++) {
if (null == previous) {
System.out.println("the given position doesn't exist");
return;
}
previous = previous.next;
}
if (current == 0) {
head = head.next;
} else if (previous.next == null) {
System.out.println("the given position is after the end - nothing to do!");
} else {
previous.next = previous.next.next;
}
}
删除具有给定数据的元素的版本
// Removes the Node with the given data
public void remove(int data) {
Node previous = head;
if(head == null){
System.out.println("Empty list - nothing to remove!");
return;
} if (head.data == data){
head = head.next;
return;
}
while(previous.next != null && previous.next.data != data){
previous = previous.next;
}
if (previous.next == null) {
System.out.println("the given element was not found - nothing to do!");
} else {
previous.next = previous.next.next;
}
}
推荐阅读
- scala - 使用scala删除长字符串中的重复单词
- gulp - 如何使用 gulp-file-include 突出显示菜单中的活动链接?
- c++ - c++ libCurl:如何使用 libCurl 接受过期的证书
- java - 单元测试 - 如果测试方法提前失败,要模拟什么?
- php - 比较实际日期与其他日期
- java - 这个冒泡排序算法有什么问题吗?
- php - 当网站在 laravel 5.6 中打开时如何自动打开一个弹出窗口?
- python - 使用海龟图形在屏幕上绘制它的最简单方法是什么?
- android - 如何在应用程序关闭时保存片段(Android)的状态并在应用程序再次运行时恢复它?
- ssas - 覆盖 DAX 度量中的 FILTER