簡體   English   中英

打印列表的循環單鏈表問題

[英]circular singly linked list problem with printing the liste

我的問題是編譯后的程序沒有顯示列表中的最后一個元素。 除了接下來我將提供的功能外,我還有一個 function 用於初始化列表,另一個用於將元素添加到列表中,它們工作得很好,現在我知道這是因為,假設我輸入了 4 個元素“afficher_liste” function(應該打印列表)僅打印 3。如果我添加第 5 個元素,“afficher_liste”函數將打印 4 個元素,如果我添加第 6 個元素,它將打印 5 個元素。 新的最后一個元素進入列表並且是其中的一部分,它只是沒有被打印出來。 我希望我能夠解釋這個問題..

   typedef struct{
    char nom[20];
    float surface;
    int habitants;
    }ville;
    typedef struct{
     ville data;
    struct element* suivant;
    }element;

    typedef struct{
    ville* T;
    }liste;  




void afficher_ville(ville*v){
printf("****************info ville********************");
printf("le nom de la ville :%s \n", v->nom);
printf("le nombre des habitants de la ville :%d \n", v->habitants);
printf("la surface de la ville :%.2f \n", v->surface);


}
void afficher_liste(liste* l)
{
    if(l->T==NULL){exit(-1);}

    element *aide = l->T;
 int lg=0;
    do
    { lg++;
        afficher_ville(&aide->data);
        aide= aide->suivant;
    }   while(aide->suivant != l->T);

    printf("longeur* %d\n",lg);
}

您的退出條件是下一個節點是 null 並且按照您的邏輯,在最后一個節點上,您的應用程序將退出,而不是打印該節點。

中斷遞歸,然后在退出應用程序之前手動打印最后一行將解決問題。

我希望這有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM