C 删除链表中间的一个节点
我已经写了一段使用链表的代码。我让用户输入几个数字,然后让用户输入他希望删除的数字的索引。我做了一些研究,发现我需要检查下一个节点是否是我要删除的节点,然后有两个临时指针指向当前节点和下一个节点(我要删除的节点),然后将第一个临时指针的下一个节点分配给第二个临时指针的下一个节点,最后释放第二个临时指针。这是我的密码:C 删除链表中间的一个节点,c,pointers,linked-list,C,Pointers,Linked List,我已经写了一段使用链表的代码。我让用户输入几个数字,然后让用户输入他希望删除的数字的索引。我做了一些研究,发现我需要检查下一个节点是否是我要删除的节点,然后有两个临时指针指向当前节点和下一个节点(我要删除的节点),然后将第一个临时指针的下一个节点分配给第二个临时指针的下一个节点,最后释放第二个临时指针。这是我的密码: #include <stdio.h> #include <stdlib.h> typedef struct node { int item;
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int item;
struct node *next;
}ListNode;
void print(ListNode *head);
int deleteNode(ListNode **ptrHead, int index);
int main()
{
int n;
int remove;
ListNode *head = NULL;
ListNode *temp = NULL;
printf("Enter a value: ");
scanf("%d", &n);
while (n != -1)
{
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = n;
temp->next = NULL;
printf("Enter a value: ");
scanf("%d", &n);
}
printf("Enter index to remove: ");
scanf("%i", &remove);
while (remove != -1)
{
deleteNode(&head, remove);
print(head);
printf("Enter index to remove: ");
scanf("%i", &remove);
}
while (head != NULL)
{
temp = head;
head = head->next;
free(temp);
}
head = NULL;
return 0;
}
int deleteNode(ListNode **ptrHead, int index)
{
int count = 0;
ListNode* temp1 = NULL;
ListNode* temp2 = NULL;
while (count <= index)
{
if (index == 0)
{
temp2 = (*ptrHead)->next;
free((*ptrHead));
(*ptrHead) = temp2;
return 0;
break;
}
if (count+1 == index)//checking for the node ahead
{
temp1 = (*ptrHead);
temp2 = (*ptrHead)->next;//the one to free
temp1->next = temp2->next;
free(temp2);
return 0;
break;
}
(*ptrHead) = (*ptrHead)->next;
count++;
}
return -1;
}
void print(ListNode *head){
if (head == NULL)
{
return;
}
while (head != NULL)
{
printf("%i\n", head->item);
head = head->next;
}
}
#包括
#包括
类型定义结构节点
{
国际项目;
结构节点*下一步;
}列表节点;
作废打印(列表节点*头);
int deleteNode(列表节点**ptrHead,int索引);
int main()
{
int n;
int-remove;
ListNode*head=NULL;
ListNode*temp=NULL;
printf(“输入值:”);
scanf(“%d”和“&n”);
而(n!=-1)
{
if(head==NULL)
{
head=malloc(sizeof(ListNode));
温度=水头;
}
其他的
{
temp->next=malloc(sizeof(ListNode));
温度=温度->下一步;
}
临时->项目=n;
temp->next=NULL;
printf(“输入值:”);
scanf(“%d”和“&n”);
}
printf(“输入要删除的索引:”);
scanf(“%i”,删除(&R));
while(删除!=-1)
{
删除节点(&头,删除);
打印头;
printf(“输入要删除的索引:”);
scanf(“%i”,删除(&R));
}
while(head!=NULL)
{
温度=水头;
头部=头部->下一步;
免费(临时);
}
head=NULL;
返回0;
}
int deleteNode(列表节点**ptrHead,int索引)
{
整数计数=0;
ListNode*temp1=NULL;
ListNode*temp2=NULL;
而(数下),;
游离((*ptrHead));
(*ptrHead)=temp2;
返回0;
打破
}
if(count+1==index)//检查前面的节点
{
temp1=(*ptrHead);
temp2=(*ptrHead)->next;//要释放的那个
temp1->next=temp2->next;
免费(临时2);
返回0;
打破
}
(*ptrHead)=(*ptrHead)->下一步;
计数++;
}
返回-1;
}
无效打印(列表节点*头){
if(head==NULL)
{
返回;
}
while(head!=NULL)
{
printf(“%i\n”,标题->项目);
头部=头部->下一步;
}
}
例如,如果我输入12345-1,链表将包含12345。然后如果我输入0以删除索引,程序将输出23445。无论我输入0还是1,这都有效。但是,当我输入索引2及以上时,它会给我奇怪的输出,例如,如果我将链表设置为12345,然后输入索引2To remove,按理说它应该输出1 2 4 5,但是它输出2 4 5,我不知道发生了什么,请给我指出正确的方向。在deleteNode中,您使用实际的头指针遍历列表。在列表中循环时,您移动实际的头!您只需修改
(*ptrHead)
删除第一个元素时
我建议您将*ptrHead
复制到一个局部变量,并使用该局部变量遍历列表
我用以/***
int deleteNode(ListNode **ptrHead, int index)
{
int count = 0;
ListNode* temp1 = NULL;
ListNode* temp2 = NULL;
ListNode* traverse = *ptrHead; // *** make a copy that we can use to traverse the list
while (count <= index)
{
if (index == 0)
{
temp2 = (*ptrHead)->next;
free((*ptrHead));
(*ptrHead) = temp2; // *** Keep this line as it is to correctly handle deletion of index 0.
return 0;
break;
}
if (count+1 == index)//checking for the node ahead
{
temp1 = traverse; // *** Use local copy of pointer
temp2 = traverse->next;//the one to free // *** Use local copy of pointer
temp1->next = temp2->next;
free(temp2);
return 0;
break;
}
traverse = traverse->next; // *** Use local copy of pointer
count++;
}
return -1;
}
int-deleteNode(列表节点**ptrHead,int索引)
{
整数计数=0;
ListNode*temp1=NULL;
ListNode*temp2=NULL;
ListNode*traverse=*ptrHead;//***制作一个副本,我们可以使用它来遍历列表
而(数下),;
游离((*ptrHead));
(*ptrHead)=temp2;//***保持此行原样,以正确处理索引0的删除。
返回0;
打破
}
if(count+1==index)//检查前面的节点
{
temp1=遍历;//***使用指针的本地副本
temp2=遍历->下一步;//要释放的对象//***使用指针的本地副本
temp1->next=temp2->next;
免费(临时2);
返回0;
打破
}
遍历=遍历->下一步;//***使用指针的本地副本
计数++;
}
返回-1;
}
在deleteNode中,使用实际头指针遍历列表。在列表中循环时,移动实际头!删除第一个元素时,只应修改(*ptrHead)
我建议您将*ptrHead
复制到一个局部变量,并使用该局部变量遍历列表
我用以/***
int deleteNode(ListNode **ptrHead, int index)
{
int count = 0;
ListNode* temp1 = NULL;
ListNode* temp2 = NULL;
ListNode* traverse = *ptrHead; // *** make a copy that we can use to traverse the list
while (count <= index)
{
if (index == 0)
{
temp2 = (*ptrHead)->next;
free((*ptrHead));
(*ptrHead) = temp2; // *** Keep this line as it is to correctly handle deletion of index 0.
return 0;
break;
}
if (count+1 == index)//checking for the node ahead
{
temp1 = traverse; // *** Use local copy of pointer
temp2 = traverse->next;//the one to free // *** Use local copy of pointer
temp1->next = temp2->next;
free(temp2);
return 0;
break;
}
traverse = traverse->next; // *** Use local copy of pointer
count++;
}
return -1;
}
int-deleteNode(列表节点**ptrHead,int索引)
{
整数计数=0;
ListNode*temp1=NULL;
ListNode*temp2=NULL;
ListNode*traverse=*ptrHead;//***制作一个副本,我们可以使用它来遍历列表
而(数下),;
游离((*ptrHead));
(*ptrHead)=temp2;//***保持此行原样,以正确处理索引0的删除。
返回0;
打破
}
if(count+1==index)//检查前面的节点
{
temp1=遍历;//***使用指针的本地副本
temp2=遍历->下一步;//要释放的对象//***使用指针的本地副本
temp1->next=temp2->next;
免费(临时2);
返回0;
打破
}
遍历=遍历->下一步;//***使用指针的本地副本
计数++;
}
返回-1;
}
在deleteNode中,使用实际头指针遍历列表。在列表中循环时,移动实际头!删除第一个元素时,只应修改(*ptrHead)
我建议您将*ptrHead
复制到一个局部变量,并使用该局部变量遍历列表
<