[英]Doubly Linked List Deleting string from list
我是C的新手,需要您的幫助。 我已經發布了我所遇到的大型鏈表問題的2部分,因為我不想用大代碼轟炸你們所有人,所以我分部分進行。 不過,這是一個新問題,因此,如果您能向我解釋,我將一如既往地感謝您。
我在雙向鏈接列表中有一個函數,該函數應該刪除列表中的字符串,但是似乎有一個問題,它沒有刪除任何內容。 事實上,這讓我陷入困境,我什么也無法輸入。 我想粘貼我的代碼,以便您更好地了解我正在處理的內容。 愛你的幫助!
這是我的結構節點:
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;
}
如果代碼看起來很長,我真的很抱歉,但是我真的想弄清楚該怎么做。 有什么幫助嗎? 請讓我知道我是否遺漏了一些東西,或者我的問題沒有正確提出。
好吧,線
if (temp->data == words) {
當然不會執行您期望的操作:您正在比較指針,而不是指針后面的字符串! 為此使用strcmp()
。
確切地說: ==
運算符用於比較兩個數組,但是這些數組會衰減為指向其第一個元素的指針,代碼等效於
if (&temp->data[0] == &words[0]) {
但這可能是您以后應該學習的一課,它會使足夠有經驗的C程序員感到困惑...
您可以通過將temp設置為指向列表的開頭來啟動該功能。 然后用搜索字符串替換head-> data。 顯然現在head-> data ==單詞,而上一個== null,所以head被釋放,函數返回零
您的代碼非常復雜,似乎包含多個問題。 您可能應該將函數切成較小的部分,以更輕松地發現錯誤。
例如:
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);
}
您的代碼中有問題。 嘗試:遍歷整個鏈表,例如
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
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; }
代碼中有很多問題,而刪除列表的完整性並沒有得到維護。 以下是從列表中刪除節點的方法:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.