#include
#include
#include
typedef struct Node {
int data;
struct Node * next;
}Node;
typedef struct List {
Node * head, *tail;
}List;
void listInsert(List *l, int a) {
if (!l) {
return;
}
else if (!l->head) {
l->head = (Node *)malloc(sizeof(Node));
l->head->data = a;
l->head->next = nullptr;
l->tail = l->head;
}
else {
Node * p = (Node *)malloc(sizeof(Node));
p->data = a;
p->next = nullptr;
l->tail = p;
Node * temp = l->head;
//为什么这种循环方式就只能遍历一个结点,有什么逻辑错误吗?
/*while (temp) {
temp = temp->next;
}
temp=p*/
while (temp->next) {
temp = temp->next;
}
temp->next= p;
}
}
void listTravel(List * l) {
if (!l) {
return;
}
else if (!l->head) {
return;
}
else {
while (l->head) {
printf("%d", l->head->data);
l->head = l->head->next;
}
}
}
int main() {
List l;
l.head = l.tail = nullptr;
listInsert(&l, 1);
listInsert(&l, 2);
listInsert(&l, 3);
listInsert(&l, 4);
listInsert(&l, 5);
listTravel(&l);
return 0;
}