我是C的新手,请你帮忙。 我已经发布了我已经拥有的两个大链接列表问题的部分,因为我不想用大代码轰炸你们所以我在部分内容这样做。这是一个新问题,如果你能解释我,我会一如既往地欣赏它。
我的双链表上有一个函数,它应该删除我列表中的字符串,但我似乎有一个问题,它没有删除任何东西。事实上它让我卡住了,我无法输入任何东西。我想粘贴我的代码,以便更好地了解我正在处理的内容。爱你的帮助!
这是我的结构节点:
struct node
{
char data[100];
struct node *previous; // Points to the previous node
struct node *next; // Points out to the next node
}*head, *last;
这是我的函数:delete_from_middle
char delete_from_middle(char words[99])
{
struct node *temp,*var,*temp1;
temp=head;
strcpy(temp->data, words);
while (temp!=NULL)
{
if (temp->data == words)
{
if (temp->previous==NULL)
{
free(temp);
head=NULL;
last=NULL;
return 0;
}
else
{
var->next=temp1;
temp1->previous=var;
free(temp);
return 0;
}
}
else
{
var=temp;
temp=temp->next;
temp1=temp->next;
}
}
printf(" Data deleted from list is %s \n", words);
return 0;
}
这就是我在主要
上分配的地方 int main()
{
char loc[99];
char words[99];
int i, dat;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) Insert At Begning\n2.) Insert At End\n3.) Insert At Middle");
printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.)Exit");
while(1)
{
printf("\n\n Enter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("Enter a word you want to insert in the 1st node ");
scanf(" %s",words);
insert_beginning(words);
display();
break;
}
case 2:
{
printf("Enter a word you want to insert in the last node ");
scanf(" %s",words);
insert_end(words);
display();
break;
}
case 3:
{
printf("After which data you want to insert your new data ");
scanf(" %s",words);
printf("Enter the data you want to insert in list ");
scanf(" %s",loc);
insert_after(words, loc);
display();
break;
}
case 4:
{
delete_from_end();
display();
break;
}
case 5:
{
printf("Enter the value you want to delete");
scanf(" %s",words);
delete_from_middle(words);
display();
break;
}
真的很抱歉,如果代码看起来很长,但我真的想知道如何做到这一点。 有帮助吗? 如果我遗失了某些内容或者我的问题没有被正确询问,请告诉我。
答案 0 :(得分:1)
嗯,行
if (temp->data == words) {
肯定没有按照你的期望去做:你在比较指针,而不是指针背后的字符串!请使用strcmp()
。
准确地说:编写==
运算符来比较两个数组,但这些数组衰减指向它们的第一个元素的指针,代码相当于
if (&temp->data[0] == &words[0]) {
但这可能是你后来应该学到的一课,它让经验丰富的C程序员感到困惑......
答案 1 :(得分:0)
通过将temp设置为指向列表的头部来启动该功能。然后用搜索字符串替换head->数据。显然现在head-> data == words和previous == null所以head被释放,函数返回零
答案 2 :(得分:0)
您的代码非常复杂,似乎包含多个问题。您应该将功能切割成较小的部分,以便更容易发现错误。
例如:
struct node *find(char words[99])
{
struct node *temp;
temp = head;
while (temp != NULL)
{
if (strcmp(temp, words) == 0)
return temp;
}
return NULL;
}
void deleteNode(struct node *n)
{
if (n->previous != NULL)
n->previous->next = n->next;
else // n is head
head = n->next;
if (n->next != NULL)
n->next->previous = n->previous;
else
last = n->previous;
free(n);
}
char delete_from_middle(char words[99])
{
struct node *target = find(words);
if (target != NULL)
deleteNote(target);
}
答案 3 :(得分:0)
您的代码存在问题。 尝试: 遍历整个链表 像
for(node temp=head; temp!=null; temp=temp->next)
{
if(temp->data==words)
{
//update links
temp->previous=temp->next;
temp->next->previous=temp->previous->next;
break;
}
}
free (temp); //delete/free node
答案 4 :(得分:0)
First in while loop, if condition is always true because of this line -
strcpy(temp->data, words);
Now there are two parts in if -> first ( if(temp->previous == NULL)
) if there is only a single element in the list then list will set
to null by setting head = NULL and last = NULL;
Second part -> If list has more than one elements then your
operations are
var->next=temp1; //This one is wrong cause var is not assigned its only declared
temp1->previous = var; //this one is causing problem free(temp); you freed the memory that was allocated for temp but
//forget to delete temp .
return 0;
//At last your element is not actually deleted and you freed the
//memory that was allocated for that element.
For deleting a specific element simplest code is ->
char delete_from_middle(char words[99])
{
struct node *h;
h = head;
while ( h! = NULL ) {
if ( strcmp(h->data, words) == 0)
{
if ( h->previous == NULL )
{
if( head == last )
{
head = NULL;
last = NULL;
}
else
{
head = head->next;
head->previous = NULL;
}
}
else
{
if( h->next!=NULL ) h->next->previous = h->previous;
h->previous->next = h->next;
}
printf(" Data deleted from list is %s \n", words);
free(h);
return 0;
}
h = h->next;
}
printf(" Data not found");
return 0; }
答案 5 :(得分:0)
代码中存在很多问题,但不保留删除列表的完整性。以下应该是从列表中删除节点的方法:
void delete_from_middle(char words[99])
{
struct node *temp;
temp=head;
while (temp!=NULL)
{
if (strcmp(temp->data,words)==0) //this is the data we are looking for, go and delete this
{
if (temp->previous==NULL) //this is the head
{
head=temp->next;
temp->next->previous=NULL;
free(temp);
printf(" Data deleted from list is %s \n", words);
return;
}
else if(temp->next==NULL) //this is last node
{
temp->previous->next=NULL;
free(temp);
printf(" Data deleted from list is %s \n", words);
return;
}
else
{
temp->previous->next=temp->next;
temp->next->previous=temp->previous;
free(temp);
printf(" Data deleted from list is %s \n", words);
return;
}
}
else //this node does not contain the data, go to the next node
{
temp=temp->next;
}
}
//search ended
printf(" Data not found\n");
return;
}