2017-04-15 7 views
1

次のコードを記述しましたが、create()関数を実行した後に動作を停止します。ヘッドノードから始まる代替要素を削除したい。私のdelete_Alt()関数は正しいですか?私が間違っているところを教えてください。あなたのプログラムでCの二重リンクリストの代替ノードを削除するにはどうすればよいですか?

#include <stdio.h> 
#include <stdlib.h> 

// using a structure 
typedef struct mynode { 
    int data; 
    struct mynode *prev; // to point to previous node 
    struct mynode *link; // to point to next node 
} node; 
node *head = NULL; 

// creating the list 
void create() { 
    node *p, *q; 
    int ch; 
    do { 
     p = (node *)malloc(sizeof(node)); 
     printf("enter data\n"); 
     scanf("%d", &p->data); 
     if (head == NULL) 
     { 
      p->prev = head; 
      q = p; 
     } 
     else 
     { 
      p->prev = q; 
      p->link = NULL; 
      q->link = p; 
      q = p; 
     } 
     printf("create another node?, press 1 "); 
     scanf ("%d",&ch); 
    } while(ch==1); 
} 

//to delete alternate elements 
void delete_Alt() { 
    if (head == NULL) 
     printf("Empty list...ERROR"); 

    node *previous, *current, *next;  
    previous = head; 
    current = head->link; 
    while (previous !=NULL && current != NULL) { 
     previous->prev = current->prev; 
     previous->link = current->link; 

     next = current->link; 
     previous->link = next; 
     next->prev = previous; 

     free(current); 
    } 
} 

// print the list 
void display() { 
    node *temp; 
    temp = head; 
    while (temp != NULL) { 
     printf("%d ",temp->data); 
     temp = temp->link; 
    } 
    printf("\n"); 
} 

int main() { 
    node *head = NULL; 
    create(); 
    printf("List before deleting is: "); 
    display(); 
    delete_Alt(); 
    printf("List after deleting is:  "); 
    display(); 
return 0; 
} 
+0

あなたの 'while'ループの生涯で' current'(それに含まれるアドレス)の値を考えてみましょう。ループの前に* once *を設定します。それは決して再び変更されることはありませんが、逆参照され、自由に繰り返されます。それは*おそらく*正しいことはできません。 'head'で指し示された最初のノードが解放された最初のノードである場合、なぜ' head'に保持されているアドレスが決して変更されないのかということは非常に疑わしいです。 *予想される入出力サンプルは、この質問を行うでしょう、btw。 – WhozCraig

答えて

0

、あなたは一度だけ頭に値を割り当てる:

node *head = NULL; 

そして、その値は変化しません。

0

リストの最初に作成された要素には "頭"を割り当てていません。したがって、常にnullです。このお試しください:あなたのdelete_altで

if (head == NULL) 
{ 
    p->prev = head; 
    head = p;  
    q = p; 
} 

を、あなたがこれを実行する必要があります。

while (previous !=NULL && current != NULL) { 
    previous->link = current->link; 
    next = current->link; 
    free(current); 
    if(next) { 
     next->prev = previous; 
     current = next->link; 
    } 
    else current = NULL; 
    previous = next; 
} 

ここでそれを試してみてください:https://repl.it/HK2P/0

1

あなたが削除機能ならびにを作成中に、いくつかのマイナーなミスをしています...

ここに更新コードがあります。

#include <stdio.h> 
#include <stdlib.h> 

// using a structure 
typedef struct mynode { 
    int data; 
    struct mynode *prev; // to point to previous node 
    struct mynode *link; // to point to next node 
} node; 
node *head = NULL; 

// creating the list 
void create() { 
    node *p, *q; 
    int ch; 
    do { 
     p = (node *)malloc(sizeof(node)); 
     printf("enter data\n"); 
     scanf("%d", &p->data); 
     p->link = NULL; 
     if (head == NULL) 
     { 
      p->prev = NULL; 
      head = p; 
     } 
     else 
     { 
      q = head; 
      while (q->link != NULL) 
      q = q->link; 
      p->prev = q; 
      q->link = p; 
     } 
     printf("create another node?, press 1 "); 
     scanf ("%d",&ch); 
    } while(ch==1); 
} 

//to delete alternate elements 
void delete_Alt() { 
    if (head == NULL) 
     printf("Empty list...ERROR"); 

    node *previous, *current, *next;  
    previous = head; 
    current = head->link; 
    while (previous !=NULL && current != NULL) 
    { 
     previous->link = current->link; 
     next = current->link; 
     free(current); 
     if(next) 
     { 
      next->prev = previous; 
      current = next->link; 
     } 
     else 
     current = NULL; 
     previous = next; 
    } 
} 

// print the list 
void display() { 
    node *temp; 
    temp = head; 
    while (temp != NULL) { 
     printf("%d ",temp->data); 
     temp = temp->link; 
    } 
    printf("\n"); 
} 

int main() { 
    node *head = NULL; 
    create(); 
    printf("List before deleting is: "); 
    display(); 
    delete_Alt(); 
    printf("List after deleting is:  "); 
    display(); 
return 0; 
} 
関連する問題