#include<stdio.h> #include<stdlib.h> #define N 3//不包括头节点共N个 //创建结构体 typedef struct Node { int value; struct Node* next; }Node,*pNode; int main() { pNode head, left, right; head =left=(pNode)malloc(sizeof(Node));//head,left指向同一个内存空间,如何没有头节点,整个链表缺少把柄,无法找到之前创建的链表,也无法遍历 head->value = N; //创建链表 for (int i = 0; i < N; i++) { right = (pNode)malloc(sizeof(Node)); printf("Input:The value of Node %d:", i + 1); scanf("%d", &right->value); left->next = right; left = right; } right->next = NULL; //遍历链表 int count = 1; for (int i = 0; i < 21; i++) printf("*"); pNode p = head; printf("\nThe amount of Nodes:%d\n", p->value); p = p->next; while (p != NULL) { printf("Output:The value of Node %d:%d\n", count,p->value); count++; p = p->next; } return 0; }
调试:
原文:https://www.cnblogs.com/iGhost/p/14630139.html