[英]Linked list deletion is removing the wrong node
Please I need someone help. 请我帮忙。 I'm having trouble with my homework. 我在做作业时遇到麻烦。
This homework is very simple. 这个作业很简单。 Create a list(1,2,3) and delete the middle number by creating a function delnode
. 创建一个list(1,2,3)并通过创建一个函数delnode
删除中间数字。 But it must use the function free()
. 但是它必须使用free()
函数。
Right now,I have created the list (1,2,3)
by using linked list method. 现在,我已经使用链接列表方法创建了列表(1,2,3)
。 I want to delete the number 2 but it doesn't work. 我想删除数字2,但不起作用。 It should be comes out with (1,3)
but it comes out with (2,3)
. 它应该与(1,3)
但它与(2,3)
。
#include <stdio.h>
#include <stdlib.h>
struct node{
int number;
struct node* next;
};
typedef struct node node;
//prototype function
node* allocateMemory(void);
node* insertNode(node*);
void delnode(node*);
int main(){
int i,num;
node* entr = allocateMemory();
node* p = NULL;
entr->number = 1;
entr->next = NULL;
num = 3;
for(i=1;i<num;i++){
if(!p){
p=insertNode(entr);
}else{
p=insertNode(p);
}
p->number = i+1;
}
while(entr){
if(entr->number == 2){
entr->number == NULL;
break;
}
entr=entr->next;
}
while(entr){
printf("%d\n",entr->number);
entr=entr->next;
}
return 0;
}
node* insertNode(node* current){
node* newNode = allocateMemory();
current->next = newNode;
newNode->next = NULL;
return newNode;
}
void delnode(node* current){
node* temp = allocateMemory();
temp = current->next->next;
free(current->next);
current = temp;
free(temp);
return ;
}
node* allocateMemory(void){
return (node*)malloc(sizeof(node));
}
Some issues: 一些问题:
(1) The original list you are creating is [2,3], and not [1,2,3] - your loop iterates for i=1,i=2 - and in each you insert i+1 - resulting in [2,3] (1)您要创建的原始列表是[2,3],而不是[1,2,3]-循环迭代i = 1,i = 2-并在每个循环中插入i + 1-导致[ 2,3]
(2) Your deletion is not doing anything: (2)您的删除操作无效:
while(entr){
if(entr->number == 2){
entr->number == NULL;
break;
}
entr=entr->next;
}
Note that entr->number == NULL
is only a boolean evaluation and not an assignment - since you use operator==
请注意, entr->number == NULL
只是布尔值评估,而不是赋值-因为您使用operator==
(Note it would fail for operator=
as well, because you do not want to assign NULL
to the value - what you really want to do is assign the previous node next
field.) (请注意,对于operator=
也会失败,因为您不想将NULL
分配给该值-您真正想要做的就是将前一个节点分配给next
字段。)
PS 聚苯乙烯
The terminology for this data structure is a Linked List , not a "Linear List" 此数据结构的术语是链接列表 ,而不是“线性列表”
Ok, this function delnode
is all kinds of messed up. 好的,该函数delnode
各种各样。 You don't need to allocate more memory when you're trying to delete a node. 尝试删除节点时,不需要分配更多的内存。 You need to link the previous to the next, and delete the current . 您需要将前一个链接到下一个,然后删除当前的 。
| prev | | current | | next |
|------| |---------| |------|
/\ X /\
|| X ||
|================================|
It should look more like this. 它看起来应该更像这样。
void delnode(node* current)
{
node* next = current->next;
// Now we need to find the node previous to this.
node* prev = entr; // From the start.
while(prev->next != current) { prev = prev->next; };
free(current); // delete the current node.
prev->next = next; // Link the previous node to the next in the list.
}
If you're trying to add three nodes, you need to change your loop. 如果要添加三个节点,则需要更改循环。
node* current = entr;
for(i=0;i<3;i++)
{
int newCount = current->number++; // Increment count.
current = insertNode(current); // Returns the new node.
current->number = newCount; // Assign new count.
}
If you want to delete the node with number
of 2
: 如果要删除number
为2
的节点:
node* idx = entr;
while(idx)
{
if(idx->number == 2) { delnode(idx); break; } // Delete node and break.
idx = idx->next; // Else, go to next node.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.