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

typedef struct Node { int data; struct Node* next; } Node;

Node* createList(int n) { Node head = NULL, tail = NULL; int i, data; for (i = 0; i < n; i++) { printf('请输入第%d个元素的值:', i + 1); scanf('%d', &data); Node node = (Node)malloc(sizeof(Node)); node->data = data; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; }
} return head; }

void displayList(Node* head) { int count = 0; Node* p = head; while (p != NULL) { printf('%d ', p->data); p = p->next; count++; } printf(' 链表中元素个数:%d ', count); }

int main() { int n; printf('请输入链表中元素个数:'); scanf('%d', &n); Node* head = createList(n); displayList(head); return 0; }

C语言链表创建与显示 - 代码示例

原文地址: https://www.cveoy.top/t/topic/nraQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录