2016-08-28 12 views
-8

リストに3つ以上の要素がある場合、最後の要素を削除するだけで正常に削除できます。しかし、リストに2つの要素がある場合、最後の要素を削除しようとするたびに、最初の要素も削除されます。単一リンクリストに2つの要素しかない場合の最後の要素の削除

node *tail = new node; 
    admintemp = adminhead; 

    while (admintemp->next!=NULL) 
    { 
     tail=admintemp; 
     admintemp=admintemp->next; 
    } 
    if (tail) 
    { 
     tail->next=NULL; 
    } 
    delete admintemp; 
+2

あなたはそれが最初の要素を削除結論に来ましたか?そしてbtw。 'tail'を初期化するために使う' node'のインスタンスをリークします。 – StenSoft

+0

その後、すべての要素が表示されます。 –

+0

admintempとadminheadを定義する –

答えて

0
Explaination: 
    With the help of temp we reach upto last node so as far as your problem    is 
concerned imagine you have two nodes only.so with temp we reach to second 
node 
Now with the help of temp1 we traverse up to temp,we make temp1's next to 
NULL coz now it is end.free memory of temp with help of free(temp)  





void Delete() 
{ 
    class Node* temp,*temp1; 
    temp=temp1=start; 
    while(temp->next!=NULL) 
{ 
temp=temp->next; 
} 
while(temp1->next!=temp) 
{ 
temp1=temp1->next; 
} 
temp1->next=NULL; 
free(temp); 
} 
関連する問題