2017-05-28 83 views
1

我想在headnode或其他节点后面添加newnode,但节点不添加whay我应该怎么做?C链表列表节点不添加

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

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

} Node; 

Node *head, *tail, *behind, *prev,*twonext; 

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 =' '; 
int k; 

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

do { 
    int v = 0; 
    int s = 0; 

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

add headnode ----------------------------------------- ---------------------------

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; 
     } 
     fflush(stdin); 
     printList(); 
     break; 

add tail node ------------- -------------------------------------------------- -----

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

     } 
     fflush(stdin); 
     printList(); 
     break; 

加在节点后面我在这里有问题我应该怎么做? -----------------------------------------------

case 'b': 
      printf("Enter node value:"); 
      scanf("%d",&v); 
      Node *np = new_node(v); 
      printf("Adding value [%d] in new node:",v); 

     // behind = behind->next = new_node(v); 
     if(head == NULL) 
     { 
     printf("No node to insert behind:"); 

     } 
     else 
     { 

      printf("\nAdd new node behind the value:"); 
      scanf("%d", &s); 

      np = head; 
      while(np->val != s); 

      { 
       prev=np; 
       np =np->next; 

      } 

      twonext=np->next; 

      np->next=NULL; 
      np->next=twonext; 

     } 
     fflush(stdin); 
     printList(); 
     break; 

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

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

return 0; 
} 
+0

你使用过'gdb'吗?跟踪内存地址。 –

+0

当您键入一个数字并点击'enter'时,换行符可能会出现问题。 –

+0

@Nguaial我应该如何解决这个问题兄弟你能告诉我吗? – Crystals

回答

0

基本上,如果你想要把其他人之间的新节点您对链接prev->接下来用新的和新建 - >接下来的下一个节点。就是这些,这里是一个代码示例。

case 'b': 
      printf("Enter node value:"); 
      scanf("%d",&v); 
      Node *np = new_node(v); 
      printf("Adding value [%d] in new node:",v); 

     // behind = behind->next = new_node(v); 
     if(head == NULL) 
     { 
      printf("No node to insert behind:"); 

     } 
     else 
     { 

      printf("\nAdd new node behind the value:"); 
      scanf("%d", &s); 

      Node *tmp = head; /*You need a tmp variable to go through the list*/ 
      while(tmp->val != s && tmp != NULL); 

      { 
       prev=tmp; /*Save the prev node*/ 
       tmp =tmp->next; /*Go through*/ 

      } 
      if(tmp != NULL){ 
       prev->next = np; /*Link the prev with the new*/ 
       np->next = tmp; /*Link the new one with the subsequent*/ 
      } 
     } 
     fflush(stdin); 
     printList(); 
     break; 
+0

非常感谢你 我的所有通过2夜任务完成 我和我的朋友是如此赞赏 – Crystals