struct node * del(struct node * temp1,int num)
{
struct node *temp2;
temp2=NULL;
if(temp1==NULL)
{
return NULL;
}
if(temp1->data==num)
{
temp2=temp1->next;
free(temp1);
return temp2;
}
else
{
temp1->next=del(temp1->next,num);
}
return temp1;
}
我引用上面的代碼從列表中刪除元素,但它不能刪除所有重複的值。請修正上面的代碼或給出任何簡單的代碼。名單是單獨線性 期待刪除列表中同一元素的重複出現
input list = 11 22 11 33 11 44
output - after deletion of 11 list = 22 33 44
void del()
{
int i,d;
struct node *list,*temp;
printf("Enter data to delete\t");
scanf("%d",&d);
list=start;
for(i=1;i<n;i++)
{
if(start->data==d)
{
temp=start;
start=start->next;
list=start;
free(temp);
}
else
{
if(list->next->data==d)
{
if(list->next->next==NULL)
{
temp=list->next;
list->next=NULL;
free(temp);
break;
}
temp=list->next;
list->next=temp->next;
list=list->next;
free(temp);
continue;
}
list=list->next;
}
}
}
我遵循這個代碼還,但它不能正常工作海合會compiler.where n爲節點的總數。
一個簡單的解決辦法是保持調用刪除功能,直到它沒有找到問題的關鍵。 – arunmoezhi 2014-09-24 06:33:57
@ arunmoezhi-先生,我正在嘗試,但無法取得成功。請給出一個代碼。 – SumS 2014-09-24 06:42:25
爲什麼你有一個遞歸的代碼。儘量避免遞歸。在這種情況下,很容易爲列表遍歷編寫迭代代碼 – arunmoezhi 2014-09-24 06:45:44