嵌套链表打印问题

时间:2014-12-02 18:39:47

标签: c nested

当我运行此代码并调用“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;       
    }  
}

请帮帮我

2 个答案:

答案 0 :(得分:1)

您的代码存在许多问题。首先,由于各种原因,它不会编译。例如,您有时使用struct koltuk而无需定义它。 &#34; koltuk&#34;意味着&#34;座位&#34;在土耳其语中,所以我会假设你在所有这些情况下都是struct seat。其次,deg未定义;我认为你的意思是val

还有其他一些问题,我可以通过做出合理的猜测来解决这个问题。但这留下了以下基本问题:

  1. 您使用全局变量作为局部变量,例如temp_ktemp。这使得人类几乎不可能分析您的代码并理解控制流程。

  2. 您已经在整个地方复制了代码。您有三个独立的代码块来分配和初始化sefer,具体取决于它是否是全局链表中的第一个,第二个或更晚。使用子程序!

  3. 您的数据模型似乎很混乱。您有全局变量struct *first_k,*temp_k,*last_k;,这使得您似乎有一个全局链接的所有sefer结构的席位列表,但struct seat没有以明显的方式返回{sefer拥有它的1}},这意味着每个sefer都有一个单独的私有seat列表。

  4. 将所有这三个问题放在一起,我无法确切地看到你出错的地方。我重写了你的代码,以消除这些问题。试试看它是否能解决你的问题:

    #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;
    }
    

    它似乎有用,但由于我不知道你想要做什么,它可能无法正常工作。

答案 1 :(得分:0)

从逻辑来看,这意味着temp真的不会去任何地方。我的猜测是,当您在*first, *last, *temp;的前几行中设置这些指针main时,您并没有将first指向NULL,导致它跳过第一个如果声明。默认情况下,指针不指向NULL,它们指向事先在该内存位置的任何内容。在这种情况下,垃圾。尝试在第一个if语句之前添加first=NULL;