c - 嵌套链表打印问题

标签 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;       
    }  
}

请帮助我

最佳答案

您的代码存在很多问题。首先,由于各种原因它无法编译。例如,您有时使用 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有一个单独的私有(private)列表 seat

将所有三个问题放在一起,我真的看不出具体哪里出了问题。我在重写你的代码来消除这些问题方面做了一些努力。尝试一下,看看是否能解决您的问题:

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

它似乎有效,但由于我不知道你想做什么,它可能无法按你想要的方式工作。

关于c - 嵌套链表打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27256596/

相关文章:

jquery:mouseout适用于嵌套元素

javascript - 无法嵌套 forEach 以在 Javascript 中工作

与库链接的 C 程序

c - C中是否有内置的交换功能?

c - 带有省略号的函数定义

Android ndk构建无法构建32位可执行文件

java - 如何将复杂的 Java 对象写入 CSV 文件

c - 在32位ARM架构中安装gcc

c++ - 关于强类型枚举前向声明

c++ - 嵌套数据结构中的STL排序