2015-03-18 2 views
1

Где ошибка следующего кода?Связанный список add() метод в C

addNode() функция не работает, также нет traverse() функция.

Последние данные не показаны.

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

struct isimler{ 
    char isim[10]; 
    struct isimler *next; 
}; 

typedef struct isimler node; 
node *head; 

void createList(){ 
    int k, n; 
    node *po; 
    printf("Eleman Sayisi: "); scanf("%d", &n); 

    for(k = 0; k<n; k++){ 
     if(k == 0){ 
      head = (node *) malloc(sizeof(node)); 
      po = head; 
     }else{ 
      po->next = (node *) malloc(sizeof(node)); 
      po = po->next; 
     } 
     printf("Isim Girin: "); scanf("%s",po->isim); 
    } 
    po->next = NULL; 
} 

void addNode(){ 
    node *po, *newNode; 
    po = head; 

    while(po != NULL){ 
     po = po->next; 
    } 

    po = (node *) malloc(sizeof(node)); 
    printf("Isim Girin: "); scanf("%s", po->isim); 
    po->next = NULL; 
} 

void traverseList(){ 
    node *po; 
    int i=0; 
    po = head; 

    while(po != NULL){ 
     printf("%d.\t%s\n",i,po->isim); 
     po = po->next; 
     i++; 
    } 
} 

int main(){ 
    createList(); 
    traverseList(); 
    addNode(); 
    traverseList(); 

    return 1903; 
} 
+0

Просьба разъяснить, какие ошибки вы получаете так что мы можем помочь вам найти его. Действительно ли ваш код компилируется? Разве это не делает то, что вы ожидаете? – bwegs

+0

Код запускается, но последние данные не отображаются. – origamic

+0

Что последнее на сайте? – bwegs

ответ

0

Ваш текущий addNode() метод создает новый узел, но он не добавляет его в связанный список.

Вы должны изменить свой метод addNode() следующим образом:

void addNode(){ 
    node *po, *newNode; 
    po = head; 

    while(po->next != NULL) { // change this so you don't lose the end of the list 
     po = po->next;  // po is now pointing to the last node in the list 
    } 

    newNode = (node *) malloc(sizeof(node)); // change this to newNode 
    printf("Isim Girin: "); scanf("%s", newNode->isim); 
    po->next = newNode; // add the new node here, don't set it to NULL 
    newNode->next = NULL; 
} 
+0

thanx, он запущен. Просто, я добавляю «newNode-> next = NULL;» после от сканированияf метод.Thanx много .. – origamic

+0

Я также добавил его к ответу. – bwegs

+0

thanx за вашу помощь (: – origamic

-1

Вы можете искать этот код и узнать, где именно вы имеете проблему

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

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

    struct test_struct *head = NULL; 
    struct test_struct *curr = NULL; 

    struct test_struct* create_list(int val) 
    { 
     printf("\n creating list with headnode as [%d]\n",val); 
     struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); 
     if(NULL == ptr) 
     { 
      printf("\n Node creation failed \n"); 
      return NULL; 
     } 
     ptr->val = val; 
     ptr->next = NULL; 

     head = curr = ptr; 
     return ptr; 
    } 

    struct test_struct* add_to_list(int val, bool add_to_end) 
    { 
     if(NULL == head) 
     { 
      return (create_list(val)); 
     } 

     if(add_to_end) 
      printf("\n Adding node to end of list with value [%d]\n",val); 
     else 
      printf("\n Adding node to beginning of list with value [%d]\n",val); 

     struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct)); 
     if(NULL == ptr) 
     { 
      printf("\n Node creation failed \n"); 
      return NULL; 
     } 
     ptr->val = val; 
     ptr->next = NULL; 

     if(add_to_end) 
     { 
      curr->next = ptr; 
      curr = ptr; 
     } 
     else 
     { 
      ptr->next = head; 
      head = ptr; 
     } 
     return ptr; 
    } 

    struct test_struct* search_in_list(int val, struct test_struct **prev) 
    { 
     struct test_struct *ptr = head; 
     struct test_struct *tmp = NULL; 
     bool found = false; 

     printf("\n Searching the list for value [%d] \n",val); 

     while(ptr != NULL) 
     { 
      if(ptr->val == val) 
      { 
       found = true; 
       break; 
      } 
      else 
      { 
       tmp = ptr; 
       ptr = ptr->next; 
      } 
     } 

     if(true == found) 
     { 
      if(prev) 
       *prev = tmp; 
      return ptr; 
     } 
     else 
     { 
      return NULL; 
     } 
    } 

    int delete_from_list(int val) 
    { 
     struct test_struct *prev = NULL; 
     struct test_struct *del = NULL; 

     printf("\n Deleting value [%d] from list\n",val); 

     del = search_in_list(val,&prev); 
     if(del == NULL) 
     { 
      return -1; 
     } 
     else 
     { 
      if(prev != NULL) 
       prev->next = del->next; 

      if(del == curr) 
      { 
       curr = prev; 
      } 
      else if(del == head) 
      { 
       head = del->next; 
      } 
     } 

     free(del); 
     del = NULL; 

     return 0; 
    } 

    void print_list(void) 
    { 
     struct test_struct *ptr = head; 

     printf("\n -------Printing list Start------- \n"); 
     while(ptr != NULL) 
     { 
      printf("\n [%d] \n",ptr->val); 
      ptr = ptr->next; 
     } 
     printf("\n -------Printing list End------- \n"); 

     return; 
    } 

    int main(void) 
    { 
     int i = 0, ret = 0; 
     struct test_struct *ptr = NULL; 

     print_list(); 

     for(i = 5; i<10; i++) 
      add_to_list(i,true); 

     print_list(); 

     for(i = 4; i>0; i--) 
      add_to_list(i,false); 

     print_list(); 

     for(i = 1; i<10; i += 4) 
     { 
      ptr = search_in_list(i, NULL); 
      if(NULL == ptr) 
      { 
       printf("\n Search [val = %d] failed, no such element found\n",i); 
      } 
      else 
      { 
       printf("\n Search passed [val = %d]\n",ptr->val); 
      } 

      print_list(); 

      ret = delete_from_list(i); 
      if(ret != 0) 
      { 
       printf("\n delete [val = %d] failed, no such element found\n",i); 
      } 
      else 
      { 
       printf("\n delete [val = %d] passed \n",i); 
      } 

      print_list(); 
     } 

     return 0; 
    } 
+0

thanx, но почему мой код работает? – origamic

Смежные вопросы