2014-12-02 72 views
0

當我運行此代碼並調用「List()」函數時,它僅打印用戶添加的最後一個。我希望從頭到尾打印。我希望你能幫助我瞭解我的清單功能。它只打印最後一個。嵌套鏈表打印問題

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

struct seat { 
    int k_no; 
    int k_name; 
    struct seat *next_k,*previous_k; 
} *first_k,*temp_k,*last_k; 


struct sefer { 
    char name[20]; 
    int no; 
    struct sefer *next,*previous; 
    struct seat *bus; 
} *first,*last,*temp; 

void list(); 
void seat_link(int val); 
void sefer_search(); 

int main() 
{ 
    int val; 
    printf ("how many names do you want to type ->"); 
    scanf ("%d",&val); 

    int i; 
    int j; 

    for(i=0;i<val;i++) {  
     if (first==NULL) { 
      first =(sefer *)malloc(sizeof(struct sefer)); 
      fflush(stdin); 
      printf (" %d. name->",i+1); 
      scanf ("%s",&first->name); 

      printf (" %d. capacity ->",i+1); 
      scanf ("%d",&first->no); 

      first->next=NULL; //2 inci düğüm daha oluşmadığı için null 
      first->previous=NULL; 
      last=first; //şimdilik sadece ilk düğüm olduğu için aynı zamanda son oluo 

      last->bus=NULL; 

      for(j=0;j<first->no;j++) { 

       //KOLTUKLAR OLUŞTURULCAK 

       if (last->bus==NULL){ 

        first_k =(seat *)malloc(sizeof(struct seat)); 
        fflush(stdin); 

        first_k->k_no=j; 
        first_k->k_name=1; 

        first_k->next_k=NULL; 
        first_k->previous_k=NULL; 
        last_k=first_k; 
        last->bus=first_k;     
       }  
       else { 

        temp_k =(seat *)malloc(sizeof(struct seat)); 
        fflush(stdin); 

        temp_k->k_no=j; 
        temp_k->k_name=0; 

        last_k->next_k=temp_k; 
        temp_k->previous_k=last_k; 
        last_k=temp_k; 
        last_k->last_k=NULL;    
       }  
      } 
     } 
     else if (last==first) { 
      printf ("\n"); 
      last = (sefer *)malloc(sizeof(struct sefer)); 
      fflush(stdin); 
      printf (" %d. name ->",i+1); 
      scanf ("%s",&last->name); 

      printf (" %d. capacitiy ->",i+1); 
      scanf ("%d",&last->no); 


      first->next=last; 
      last>next=NULL; 
      last->previous=first; 

      last->bus=NULL; 

      for(j=0;j<last->no;j++) { 

       //KOLTUKLAR OLUŞTURULCAK 

       if (last->bus==NULL) { 

        first_k =(seat *)malloc(sizeof(struct seat)); 
        fflush(stdin); 

        first_k->k_no=j; 
        first_k->k_name=2; 

        first_k->last_k=NULL; 
        first_k->previous_k=NULL; 
        last_k=first_k; 
        last->bus=first_k; 

       } 
       else {  
        temp_k =(seat *)malloc(sizeof(struct seat)); 
        fflush(stdin); 

        temp_k->k_no=j; 
        temp_k->k_name=0; 

        last_k->next_k=temp_k; 
        temp_k->previous_k=last_k; 
        last_k=temp_k; 
        last_k->last_k=NULL; 
       }  
      } 
     }   
     else { // kayıt eklenmişse diğer düğümler oluşturulcak  
      printf ("\n"); 
      temp=(sefer *) malloc(sizeof(struct sefer)); 
      fflush(stdin); 
      printf (" %d. name",i+1); 
      scanf ("%s",&temp->name); 

      printf (" %d. capacity->",i+1); 
      scanf ("%d",&temp->no); 

      last->next=temp; 
      temp->previous=last; 
      last=temp; 
      last->next=NULL; 

      last->bus=NULL; 

      for(j=0;j<temp->no;j++){ 

       //KOLTUKLAR OLUŞTURULCAK 

       if (last->bus==NULL){ 
        first_k =(seat*)malloc(sizeof(struct seat)); 
        fflush(stdin); 

        first_k->k_no=j; 
        first_k->k_name=3; 

        first_k->last_k=NULL; 
        first_k->previous_k=NULL; 
        last_k=first_k; 
        last->bus=first_k; 

       } 
       else{ 
        temp_k =(seat *)malloc(sizeof(struct seat)); 
        fflush(stdin); 

        temp_k->k_no=j; 
        temp_k->k_name=0; 

        last_k->next_k=temp_k; 
        temp_k->previous_k=last_k; 
        last_k=temp_k; 
        last_k->next_k=NULL; 
       }  
      } 
     } 
    } 

    list(); 
    system("PAUSE"); 
    return 0;   
} 


void sefer_search(){ //bağda arama yapar 

    int searching; 

    printf ("\n\t\t Aranacak Sefer Numarasını Giriniz:"); 
    scanf ("%d",&searching); 

    temp=first; 

    while (1) { 
     if (temp->no==searching) { 
      break; 
     } 
     temp=temp->next; 
    } 
} 


void seat_link(int val) { 
    int j;    
} 

我的問題是在這裏實際上是:

void list(){ 
    temp=first; 
    while (temp !=NULL) { 
     printf("\t%s --%d \n", temp->name,temp->no); 
     temp=temp->next; 
    }   

    printf ("\n"); 

    last->bus=first_k; 
    while (last->bus !=NULL) {    
     printf("\t%d --%d \n",last->bus->k_name,last->otobus->k_no); 
     last->bus=last->bus->next_k;  
    } 
} 

請幫我

+0

你的程序甚至不會編譯:'deg','sefer'和'koltuk'都是*未聲明的標識符*,還有其他錯誤'xxx'不是'yyy'的成員。 – 2014-12-02 18:54:46

+0

他聲稱他的代碼編譯和工作,所以我在他的話他帶他。如果他們真的有必要解決這個問題,也許他應該進一步闡述,但從我所看到的,這是一個指針問題。 – MSalmo 2014-12-02 19:21:15

+0

是的,我認爲你也是這樣。這可能是指針問題,但我還沒有弄清楚我該怎麼辦才能解決那個指針問題。 – 2014-12-02 19:48:42

回答

1

你的代碼有一堆問題。首先,它不會因各種原因而編譯。例如,你有時使用struct koltuk而沒有定義它。 「koltuk」意思是土耳其語中的「座位」,所以我將假設在所有這些情況下你的意思是struct seat。其次,deg未定義;我假定你的意思是val

還有其他一些問題,如我能夠通過做出合理的猜測來解決的問題。但還有以下基本問題:

  1. 您使用全局變量作爲局部變量,例如temp_ktemp。這使得人們幾乎不可能分析你的代碼並理解控制流程。

  2. 你已經複製代碼到處都是。您有三個獨立的代碼塊來分配和初始化sefer,具體取決於它是全局鏈表中的第一個,第二個還是更高。使用子程序!

  3. 您的數據模型似乎很混亂。你有全局變量struct *first_k,*temp_k,*last_k;,這使得它看起來像你有一個全局鏈接的所有sefer結構的鏈接列表,但是struct seat沒有以明顯的方式返回到擁有它的sefer,這意味着每個sefer都有一個單獨的,私人名單seat s。

把所有這三個問題放在一起,我不能真正看到哪裏,特別是你會出錯。爲了消除這些問題,我重新編寫了代碼。試試吧,看它是否解決你的問題:

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

typedef struct seat{ 
    int k_no; 
    int k_name; 
    struct seat *next_k, *previous_k; 

} seat; 

typedef struct sefer { 
    char name[20]; 
    int no; 
    struct sefer *next,*previous; 
    struct seat *bus; 

} sefer; 

void list(); 

sefer *sefer_search(); 

void link_sefer(sefer **pp_first, sefer **pp_last, sefer *p_added) 
{ 
    p_added->next = p_added->previous = NULL; 
    if (*pp_first == NULL) 
    { 
     *pp_first = *pp_last = p_added; 
    } 
    else 
    { 
     (*pp_last)->next = p_added; 
     p_added->previous = *pp_last; 
     *pp_last = p_added; 
    } 
} 

void link_seat(seat **pp_first, seat **pp_last, seat *p_added) 
{ 
    p_added->next_k = p_added->previous_k = NULL; 
    if (*pp_first == NULL) 
    { 
     *pp_first = *pp_last = p_added; 
    } 
    else 
    { 
     (*pp_last)->next_k = p_added; 
     p_added->previous_k = *pp_last; 
     *pp_last = p_added; 
    } 
} 

sefer *create_and_link_sefer(sefer **pp_first_sefer, sefer **pp_last_sefer, int i) 
{ 
    sefer *new_sefer; 
    seat *p_first_seat = NULL; 
    seat *p_last_seat = NULL; 
    int j; 


    // Allocate and initialize sefer 

    printf ("\n"); 
    new_sefer = calloc(1, sizeof(struct sefer)); 
    fflush(stdin); 
    printf (" %d. name->",i+1); 
    scanf ("%s",&new_sefer->name); 

    printf (" %d. capacity->",i+1); 
    scanf ("%d",&new_sefer->no); 
    new_sefer->bus = NULL; 

    // Link sefer 

    link_sefer(pp_first_sefer, pp_last_sefer, new_sefer); 

    // Allocate seats 
    for(j=0;j<new_sefer->no;j++){ 

     //KOLTUKLAR OLUŞTURULCAK 
     seat *p_seat = calloc(1, sizeof(struct seat)); 
     fflush(stdin); 
     p_seat->k_no=j; 
     p_seat->k_name = (j != 0 ? 0 : (i+1 > 3 ? 3 : i+1)); // I CAN'T FIGURE OUT WHAT THIS IS SUPPOSED TO BE 

     link_seat(&p_first_seat, &p_last_seat, p_seat); 
    } 

    new_sefer->bus = p_first_seat; 

    return new_sefer; 
} 

void sefer_list(sefer *first, sefer *last){ 

    sefer *temp=first; 
    while (temp !=NULL) 
    { 
     seat *seat; 

     printf("\t%s --%d \n", temp->name,temp->no); 

     for (seat = temp->bus; seat != NULL; seat = seat->next_k) 
     { 
      printf("\t\t%d --%d \n",seat->k_name, seat->k_no); 
     } 

     printf ("\n"); 
     temp=temp->next; 
    } 
} 

sefer *sefer_search(sefer *first, sefer *last){ //bağda arama yapar 

    int arama; 
    sefer *temp; 

    printf ("\n\t\t Aranacak Sefer Numarasını Giriniz:"); 
    scanf ("%d",&arama); 

    temp=first; 

    while (temp != NULL){ 

     if (temp->no==arama){ 
      break; 
     } 

     temp=temp->next; 
    } 

    return temp; 
} 

sefer *first = NULL; 
sefer *last = NULL; 

int main() 
{ 
    int val; 
    int i; 

    printf ("how many names do you want to type ->"); 
    scanf ("%d",&val); 

    for(i=0;i<val;i++){ 
     create_and_link_sefer(&first, &last, i); 
    } 

    sefer_list(first, last); 

    system("PAUSE"); 
    return 0; 
} 

看來工作,但因爲我不知道你正在嘗試做的做,它可能無法正常工作,只要你想。

+0

是的,我改變了變量名稱,因爲它可能很容易理解英文。但是我錯過了一些單詞。請繼續關注。順便說一下,代碼工作的方式。除了所有這些,你可以給我你的電子郵件地址,請 ? – 2014-12-02 20:22:56

+0

那麼評論者如何期望知道錯誤編碼的代碼和錯誤複製的代碼之間的區別? – 2014-12-02 20:48:50

+0

是的,你是對的。 – 2014-12-02 20:58:48

0

由邏輯,這意味着temp還真是不會去任何地方看。我的猜測是,當您在main的前幾行中設置指針*first, *last, *temp;時,您確實沒有指向firstNULL,導致它跳過第一個if語句。指針默認不指向NULL,它們指向事先在該內存位置的任何內容。在這種情況下,垃圾。嘗試在第一個if語句之前添加first=NULL;

+0

謝謝,但是,我添加了第一個= NULL;頂部的主要功能。但仍然不工作我有同樣的問題。 - – 2014-12-02 19:52:32