到目前为止,我已经成功删除了第i个索引中的所有内容,但是我需要能够在第i个索引之后将节点从第i个索引之后连接到节点,这是我到目前为止所做的。
LinklistNode* remove_node(LinklistNode* list_head, int index){
if(list_head != NULL){
LinklistNode* temp;
if(list_head-> next ==NULL){
temp = list_head;
list_head = NULL;
}
else{
LinklistNode* list_pointer = list_head;
LinklistNode* next_list_pointer = list_pointer->next;
while(next_list_pointer->next != NULL && index > 0){
index--;
list_pointer = next_list_pointer;
next_list_pointer = next_list_pointer->next;
}
temp = next_list_pointer;
list_pointer->next = NULL;
}
free(temp);
}
return list_head;
}
答案 0 :(得分:1)
要链接索引前后的节点,请将list_pointer->next
指向next_list_pointer->next
while(next_list_pointer->next != NULL){
index--;
if (index == 0) break;
list_pointer = next_list_pointer;
next_list_pointer = next_list_pointer->next;
}
temp = next_list_pointer;
list_pointer->next = temp->next;
答案 1 :(得分:1)
为了在第i个元素之前连接节点,请使用list_pointer。有类似的东西 ...
using System;
using System.Windows.Forms;
namespace Project6
{
class Student
{
//Initialize variables
private string[] studentInformationArray;
//Constructor that accepts the studentInformationArray as an argument
public Student(string[] studentInformationArray)
{
this.studentInformationArray = studentInformationArray;
}
public Student()
{
string className = studentInformationArray[1];
string semester = studentInformationArray[2];
string picture = studentInformationArray[3];
int project1 = Convert.ToInt32(studentInformationArray[4]);
int project2 = Convert.ToInt32(studentInformationArray[5]);
int project3 = Convert.ToInt32(studentInformationArray[6]);
int project4 = Convert.ToInt32(studentInformationArray[7]);
int project5 = Convert.ToInt32(studentInformationArray[8]);
int project6 = Convert.ToInt32(studentInformationArray[9]);
int midtermExam = Convert.ToInt32(studentInformationArray[10]);
int finalExam = Convert.ToInt32(studentInformationArray[11]);
}
public string GetName()
{
string studentName;
studentName = studentInformationArray[0];
return studentName;
}
}
}
... 这应该几乎涵盖删除部分,只需确保包含您的基本案例。
答案 2 :(得分:0)
使用指针指向节点的指针的替代方法:
LinklistNode* remove_node(LinklistNode* list_head, int index)
{
LinklistNode **ppNode = &list_head;
if(list_head == NULL)
return NULL;
while(index--){
ppNode = &((*ppNode)->next);
if(*ppNode == NULL) /* if index not in list */
return list_head; /* just return */
}
*ppNode = (*ppNode)->next; /* delete node */
return list_head;
}