2014-05-15 77 views
-2

我試圖打印出一個雙向鏈表的值,但是我的printAll函數只打印出第一個節點的內容出於某種原因。在c中打印一個雙向鏈表的內容只打印出第一個節點

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

struct clientsOn 
{ 
    int id; 
    char filename[20]; 
    struct clientsOn* next; 
    struct clientsOn* prev; 
}; 

struct clientsList 
{ 
    int count; 
    struct clientsOn* head; 
}; 

void printAll(struct clientsList* list); 
void add(struct clientsList *list,struct clientsOn *newC); 
struct clientsList* createList(); 
struct clientsOn* createNode(char* profile); 

main() 
{ 
struct clientsList* people = malloc(sizeof(struct clientsList)); 
people = createList(); 
printf("Number of people currently on: %d\n",people->count); 

struct clientsOn* pers1, *pers2, *pers3; 

char boy[20],boy2[20],boy3[20]; 
printf("String: \n"); 
scanf("%s",boy); 
printf("String: \n"); 
scanf("%s",boy2); 
printf("String: \n"); 
scanf("%s",boy3); 

pers1 = createNode(boy); 
pers2 = createNode(boy2); 
pers3 = createNode(boy3); 

add(people,pers1); 
add(people,pers2); 
add(people,pers3); 
printf("people count: %d", people->count); 

printAll(people); 
} 

struct clientsList* createList() 
{ 
    struct clientsList* list = malloc(sizeof(struct clientsList)); 
    if(list){ 
     list->head = NULL; 
     list->count = 0; 
    } 
    return list; 
} 

struct clientsOn* createNode(char* profile) 
{ 
    struct clientsOn* clients = malloc(sizeof(struct clientsOn)); 

    if(clients){ 
     clients->next = clients; 
     clients->prev = clients; 
     strcpy(clients->filename,profile); 
    } 
    return clients; 
} 

void add(struct clientsList *list,struct clientsOn *newC) 
{ 

    if(newC){ 
    if(list->head == NULL){ 
     list->head = newC; 
    }else{ 
     list->head->prev = newC; 
     newC->prev = list->head->prev; 
     newC->prev->next = newC; 
     newC->next = list->head; 
    } 
    list->count++; 
    } 
} 

void printAll(struct clientsList* list) 
{ 
    struct clientsOn* node = list->head; 

    if(node != NULL){ 
     do{ 
      printf("list content is: %s",node->filename); 
      node = node->next; 
     }while(node!=list->head); 
    } 
} 

我的附加功能添加節點列表的盡頭,當我輸出列表計數

printf("people count: %d", people->count); 

它顯示添加到列表中的項目數,但是當我嘗試打印出個別項目,

printAll(people); 

它不超出第一個節點。任何人都有類似的經歷嗎?

+0

「*它沒有超越......曾經經歷過類似的事情*」是的,有點兒,同時和年輕的馬匹一起玩...... ;-) – alk

回答

1

在您的add()函數中,有一個問題不會適當地創建列表。

list->head->prev = newC; //sets head->prev to newC 
newC->prev = list->head->prev; //here setting newC->prev to head->prev which is NewC 
newC->prev->next = newC; //here newC->prev->next is actually newC->next is set to newC 

嘗試使用gdb或其他調試器來驗證您的邏輯。

+0

1+至少沒有提出使用調試器! – alk

+0

感謝您的提示,它適用於您的更改,猜測我有我的邏輯都搞砸了。 – user3640687

0

函數嘗試使用這個邏輯。我認爲它應該工作。

if(newC){ 
    if(list->head == NULL){ 
     list->head = newC; 
    }else{ 
     newC->next = list->head; 
     newC->prev = list->head->prev; 
     list->head->prev->next = newC; 
     list->head->prev = newC; 
    } 
    list->count++; 
    } 

更好的嘗試GDB或者如果你想了解你可以先使用圖表的邏輯。

+0

感謝您的提示,我會嘗試使用GDB從現在開始更好地理解 – user3640687