これは、リンクされたリストの先頭に要素を追加する方法です
//typedef void* VoidPtr
//typedef node* NodePtr
struct Node
{
NodePtr next
VoidPtr data
};
void LinkedList::Addelement(VoidPtr horoscope)
{
if(head == NULL)
{
head = new Node;
head->data = horoscope;
head->next = NULL;
}
NodePtr temp = new Node;
temp -> data = horoscope;
temp -> next = head;
head = temp;
}
これは、リンクされたリストの末尾に要素を追加する方法です
void LinkedList::addelementfromback(VoidPtr horoscope)
{
NodePtr temp=head;
if(head == NULL)
{
head = new Node;
head->data = horoscope;
head->next = NULL;
}
while( temp->next != NULL)
{
temp=temp->next
}
NodePtr element=New Node;
element->data=horoscope;
element->next=NULL;
temp->next=element;
}
リンクされたリストの先頭に追加するために temp=element を使用する理由がわかりませんが、リンクされたリストの末尾に追加するには、temp->next=element を使用します。リンクされたリストの末尾に要素を追加するために while temp=next を使用できない理由がわかりません