我試圖打印出一個雙向鏈表的值,但是我的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);
它不超出第一個節點。任何人都有類似的經歷嗎?
「*它沒有超越......曾經經歷過類似的事情*」是的,有點兒,同時和年輕的馬匹一起玩...... ;-) – alk