2016-03-23 12 views
1

リストの先頭にノードを挿入する必要がありますが、どうすればいいですか?このコードでリストの先頭にノードを挿入する

while(tmp!=NULL){ 
    printf("__________"); 
    printf("\nCodigo: %d\n",tmp->code); 
    printf("Nombre: %s\n",tmp->name); 
    printf("Apellido: %s\n",tmp->last); 
    tmp = tmp->next; 

}; 

iがリストを印刷し、これは私が見るものである。


Codigo:3
ノンブル:第
Apellido:ノード


Codigo:2
ノンブル:SECC
Apellido:ノード


Codigo:1
ノンブル:最初
Apellido:ノード

ので、私はbeginnigに何かを挿入する場合私は見るべきである


Codigo:3
ノンブル:第
Apellido:ノード


Codigo:2
ノンブル:SECC
Apellido:ノード


Codigo:1
ノンブル:最初
Apellido:ノード


Codigo:4
ノンブル:第
Apellido:私はそれを行う方法ノード

?私はこれを試してみました:

tmp_aux = lista;// creating an aux list 
    while(tmp_aux->next!=NULL){ 
     tmp_aux->next = tmp_aux; 
    }; // i used this becouse the last printed (up) is the first node 
    new_aux = (struct nodo*) malloc(1*sizeof(struct nodo)); 
    printf("ingrese el codigo: "); 
    scanf("%d",&(*new_aux).code); 
    printf("ingrese el nombre: "); 
    scanf("%s",&(*new_aux).name); 
    printf("ingrese el apellido: "); 
    scanf("%s",&(*new_aux).last); 



    new_aux->next = tmp_aux;// then i put the aux on the next of my new node 
    lista = new_aux;// and make my list the new one 
個人的に
+0

ループの先頭で 'tmp_aux'がnullでないと仮定すると、それは決して終了しません。 'tmp_aux-> next = tmp_aux'はリストをトラバースせず、ただちに値を再割り当てします。おそらく 'tmp_aux = tmp_aux-> next'をしたかったでしょうか? –

+0

私はそれを試みましたが、リストの最後に最初のノードを印刷しました。私は最後のノードをスキャンするのが底にあるべきだと思います...そうですか? –

+1

この例では、リストの最後に挿入されていない部分が先頭に挿入されています。それが最初にあった場合は、新しいノードが最後に印刷されませんでしたか?あなたは本当に欲しいですか?リストの始めか終わりですか? – kaylum

答えて

1

私は最初のノードが(コメントを参照して)最初に印刷されるべきだと思うが、それは私が思うだけの意味です。

私はリンクリストを使用しているたびに、私はheadtailポインタを使用しました。 headポインターは、リストの最初の項目を指しています。tailは、リストの最後の項目を指しています。リストからアイテムを追加したり削除したりするたびにこれらの最新情報を保持するために必要な余分な簿記がありますが、それは努力する価値があると思います。 headで始まり、tailに行くので、リストを反復する必要がある操作(特定のノードの検索、すべてのアイテムの印刷など)は、より簡単に行われます。以下のような何かあなたが始める必要があります,,これは、すべての包括的なプログラムであることを意図されていません。

static struct nodo *head = NULL, *tail = NULL; 

struct nodo* insert_at_head(struct nodo* new_aux) 
{ 
    if (head == NULL && tail == NULL) 
    { 
    // our list is empty; any item inserted is both the beginning and end 
    head = new_aux; 
    tail = new_aux; 
    new_aux->next = NULL; // only 1 item in the list, there is no next element 
    } 
    else 
    { 
    // if maintained properly, this should be the only other possibility 
    new_aux->next = head; // new_aux is the new head of the list, so the previous head is now the 2nd item 
    head = new_aux; // make new_aux the new head of the list 
    } 

    // in fact, since head = new_aux happens in both branches, that should just go here 

    return head; // this could be a void function, but returning head and checking that it equals new_aux shows that new_aux is now the head of the list 
} 

struct nodo* remove_head() 
{ 
    if (head != NULL) // our list is not empty, so it does in fact have a head 
    { 
    struct nodo* temp = head 
    head = head->next; // even if there is one item in the list, head->next should be NULL, so now head is NULL 
    free(temp); 
    } 
    else 
    { 
    // this means our list is empty, optionally print an error message or warning "Trying to delete head from empty list!" 
    return NULL; 
    } 

    return head; 
} 

// now iterating over all the nodes is easy, you just have to go from head to tail. 
void print_list() 
{ 
    struct nodo* cur_aux; 
    for (cur_aux=head; cur_aux!=NULL; cur_aux=cur_aux->next) 
    { 
    // print whatever you want here 
    } 
} 

// you can have several other functions, for manipulating the list. Their prototypes *might* look like the following: 
// do not forget to maintain head and tail pointers for all of these! 
struct nodo* insert_at_tail(stuct nodo* new_aux); // similar to insert_at_head(), except now you want to make the current last node the 2nd to last node 
struct nodo* locate_aux(const struct nodo* aux); // iterate head to tail and return the node that matches all fields of struct nodo, return NULL if not found 
void delete_aux(struct nodo* aux); // iterate through the list and delete aux if found 
void clean_up_list(); // iterate from head to tail and delete all items 
struct nodo* insert_aux_after(struct nodo* insert_after, struct nodo* new_aux); // this will insert new_aux after insert_after 

int main(int argc, char* argv[]) 
{ 
    // something like this 
    struct nodo* new_aux = malloc(sizeof(struct nodo)); 
    struct nodo* new_aux2 = malloc(sizeof(struct nodo)); 
    struct nodo* new_aux3 = malloc(sizeof(struct nodo)); 

    // fill in the fields for each new_aux 
    if (insert_at_head(new_aux) != new_aux) 
    { 
    // some error happened on insertion,, handle it 
    } 
    insert_at_head(new_aux2); 
    insert_at_head(new_aux3); 

    print_list(); 
    // the output should print new_aux3, then new_aux2, and finally new_aux 

    clean_up_list(); 
    return 0; 
} 

あなたは、リストの最初または最後であることをheadtailを調整することができますが、一般的な慣習がheadようにラベルを付けますリストの最初の項目。私はおそらく他のプロトタイプのためのいくつかのコードを記入することができます。実際にはtailポインタなしで上記のすべてを実装することができます。リスト全体の繰り返しをheadに開始し、->next == NULLまで行ってください。また、リスト内の項目数の実行回数を維持するstatic size_t num_auxを維持することを検討することもできます。これは、リストから項目を削除しようとすると、成功または失敗を判断する場合に特に役立ちます。私はあなたがリンクリストのチュートリアルをGoogleに提供すれば、私が提供したコードよりもはるかに良いコードを得ることができますが、私が示したことは、リンクリストを扱うための少なくとも1つの妥当なアプローチでなければなりません。

関連する問題