输出结果:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//函数声明
void printNode(struct Node *head);
void createNode(struct Node *head);
//创建链表
struct Node{
int data;
struct Node *next;
}Node;
//链表初始化
struct Node *intList() {
struct Node *head = (struct Node *)malloc(sizeof(struct Node));
head->data = 0;
head->next = head;
return head;
}
//创建结点
void createNode(struct Node *head, int i) {
struct Node *tail = (struct Node *)malloc(sizeof(struct Node));
tail->data = i;
tail->next = head->next;
head->next = tail;
printf("%d ", i);
}
//打印数据
void printNode(struct Node *head) {
struct Node *node = head->next;
printf("输出数据:");
for(int i = 0; i < 10; i++) {
if (node = head) {
node = node->next;
}
printf("%d ", node->data);
node = node->next;
}
}
//主函数
int main() {
struct Node *list = intList();
for (int i = 0; i < 5; i++) {
createNode(list, i);
}
printNode(list);
return 0;
}
printNode 那里是 node == head
不是 node = head
if (node = head)
->
if (node == head)