要件は、ユーザーの入力から作成されるリンク リストのルートにinput
a を返す関数です。お気に入り:Node*
input
Node *input()
{
Node *root; //not yet allocated
while(1)
{
//take in numbers from the user and correspondingly add them
//to the linked list and quit when anything else is entered
if(input character is a number)
{
//allocate memory to a fresh pointer and push the number to it
//and add this new Node to the LL
}
}
return root;
}
-> while ループ本体の前に、 メモリを事前に割り当てroot
て最初の番号をプッシュする解決策があります。しかしここで、ユーザーが何も入力しない場合は、すぐに削除する必要があります。
->また、root が while ループで NULL かどうかを確認し、そうであれば、 memory を割り当てて、それが 1 回だけ発生するようにする方法もあります。
しかし、LL の根本から生じる奇妙な状況を解消する解決策があるかどうかを知りたいです。
->たぶん、値を保持しないダミーノードを最初に保持できます。
しかし、それとは別に?全体を行うためのより良い方法があれば、それを提案してください。