2017-05-26 5 views
1

3番目のノードを作成すると、そのノードの無限ループが作成されました。私は何をすべきか? ノードの後ろに挿入ノード用のコード 'b'を挿入してください。リンクされたリストノードノードの無限ループとノード間の挿入

パート1 --------------------------------------------- -------------------------------------------------- ------------------------------

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

char p,j ; 
int v; 

struct node{ 
int val; 
struct node *next; 
}; 

struct node *head=NULL; 
struct node *curr=NULL; 
struct node *temp=NULL; 
struct node *prev=NULL; 
struct node *tail=NULL; 
struct node *after=NULL; 

part2 ------------ -------------------------------------------------- -------------------------------------------------- -------------

struct node *creatFirstNode(int val){ 

    printf("\ncreating list with headnode as [%d]\n",val); 
    struct node *ptr=(struct node *)malloc(sizeof(struct node)); 
    if(ptr==NULL){ 
        printf("\nCreated Failed \n"); 
        return NULL;  
    } 
    ptr->val=val; 
    ptr->next=NULL; 

    head=ptr; 
    curr=ptr; 

    return ptr; 
} 

part3 ----------------------------- -------------------------------------------------- ----------------------------------------------

main(){ 
    int n,i; 
    struct node *A=(struct node *)malloc(sizeof(struct node)); 
    struct node *B=(struct node *)malloc(sizeof(struct node)); 
    struct node *C=(struct node *)malloc(sizeof(struct node)); 
    struct node *new=(struct node *)malloc(sizeof(struct node)); 
    struct node *addEnd=(struct node *)malloc(sizeof(struct node)); 
    struct node *addAmong=(struct node *)malloc(sizeof(struct node)); 


    printf("\n-------- Welcome to Linked List Program -----------\n\n"); 

    do{ 

    printf("\nAdd to 'h'ead or 't'ail or 'b'ehind value:"); 
    scanf("%c",&p); 
    fflush(stdin); 

パート4 ------------------------------------------- -------------------------------------------------- --------------------------------

switch(p) 
{ 
    case 'h': 
     printf("Enter value node:"); 
     scanf("%d",&v); 
     fflush(stdin); 
     if(head == NULL) 
     { 
       creatFirstNode(v); 
       fflush(stdin); 
     } 
     else 
     { 
      curr=head; 
      new->val=v; 
      new->next=NULL; 
      curr=new; 
      curr->next=head; 
      curr=curr->next; 
      ("\ncreating list with headnode as [%d]\n",v); 
      head=new; 
      fflush(stdin); 
     } 
     //void printList(); 

    curr=head; 
    printf("\n----Value in Liked list----\n"); 

    while(curr!=NULL){ 
       printf("[%d], ",curr->val); 
       curr=curr->next; //change current node   
        } 
     break; 

5 ---------- -------------------------------------------------- -------------------------------------------------- ---------------

case 't': 
     printf("Enter value node tail:"); 
     scanf("%d",&v); 



     curr=head; 
     while(curr!=NULL){ //Seek for last node 
       tail=curr; 
       curr=curr->next; // shift to next node 

      } 

     addEnd->val=v; 
     addEnd->next=NULL; 
     tail->next=addEnd;   
     tail=new; 
     fflush(stdin); 

     //void printList();. 
     curr=head; 
    printf("\n----Value in Liked list----\n"); 

    while(curr!=NULL){ 


       printf("[%d], ",curr->val); 
       curr=curr->next; //change current node 

      } 

     break; 

part6 --------------------------- -------------------------------------------------- ------------------------------------------------

case 'b': 
     printf("Enter value node behind:"); 
     scanf("%d",&v); 
     fflush(stdin); 

     printf("Adding value [%d] in new node:",&v); 
     printf("Add new node behind the value:"); 


     void printList(); 
     break; 



     default: 

     printf("\n  Invalid Input  "); 
     getch(); 


} 

}while(p != 'h' || p != 't' || p != 'b'); 

getch(); 
return 0; 
} 
+0

私はあなたにも構造体のノードを宣言するべきだと思います*次回= NULL; : –

+0

1)各ノードを割り当てます。 – BLUEPIXY

+1

変数名として[new](http://en.cppreference.com/w/cpp/keyword)を使用しないでください.ACコンパイラを使用していても、それは良い方法ではありません。 – BPL

答えて

0

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

typedef struct node { 
    int val; 
    struct node *next; 
} Node; 

Node *head, *tail; 

Node *new_node(int val){ 
    struct node *ptr = malloc(sizeof(*ptr)); 
    if(ptr==NULL){ 
     perror("malloc:"); 
     printf("\nFailed to create a new node.\n"); 
     return NULL; 
    } 
    ptr->val = val; 
    ptr->next = NULL; 

    return ptr; 
} 

Node *creatFirstNode(int val){ 
    return head = tail = new_node(val); 
} 

void printList(void){ 
    Node *np = head; 

    printf("\n----Value in Liked list----\n"); 
    while(np){ 
     printf("[%d], ", np->val); 
     np = np->next; 
    } 
    printf("NULL\n"); 
} 

void freeList(void){ 
    while(head){ 
     Node *np = head->next; 
     free(head); 
     head = np; 
    } 
    tail = NULL; 
} 

int main(void){ 
    char cmd =' '; 

    printf("\n-------- Welcome to Linked List Program -----------\n\n"); 

    do { 
     int v = 0; 

     printf("\nAdd to 'h'ead or 't'ail or 'b'ehind value or 'p'rint or 'q'uit:"); 
     scanf(" %c", &cmd); 

     switch(cmd){ 
     case 'h': 
      printf("Enter value node head:"); 
      scanf("%d", &v); 
      if(head == NULL){ 
       creatFirstNode(v); 
      } else { 
       Node *np = new_node(v); 
       np->next = head; 
       head = np; 
      } 
      break; 

     case 't': 
      printf("Enter value node tail:"); 
      scanf("%d", &v); 
      if(head == NULL){ 
       creatFirstNode(v); 
      } else { 
       tail = tail->next = new_node(v); 
      } 
      break; 

     case 'b': 
      printf("Enter value node behind:"); 
      scanf("%d", &v); 
      printf("\nUnimplemented yet.\n"); 
      break; 

     case 'p': 
      printList(); 
      break; 
     case 'q': 
      freeList(); 
      printf("\nBye!\n"); 
      break; 

     default: 
      printf("\n  Invalid Input  "); 
     } 
    }while(cmd != 'q'); 

    return 0; 
} 
+0

あなたのコードに感謝します。予期しないループはもうありません! 1つ左の問題:ノードを追加して、ノードの後ろに挿入する場合は 'b'が必要です。 thx –

+0

'case 'b':'それは自分で行います。 – BLUEPIXY

+0

うーん...大丈夫Thx非常にブロ! :)) –

関連する問題