思路:创建结点,建立哨兵位,输入,打印。
#include<stdio.h> #include<stdlib.h> #include<assert.h> typedef struct list { int val; struct list* next; }list; list* Buynode(int x) { list* node = (list*)malloc(sizeof(list)); assert(node); node->val = x; node->next = NULL; return node; } void print(list* head) { list* cur = head; while(cur) { printf("%d ",cur->val); cur = cur->next; } } int main() { int a; int i,x; scanf("%d",&a); list* head = (list*)malloc(sizeof(list)); list* tail = (list*)malloc(sizeof(list)); tail->next = NULL; list* cur = NULL; tail = head; for(i = 0;i < a; i++) { cur = Buynode(i); scanf("%d",&(cur->val)); tail->next = cur; tail = tail->next; } print(head->next); }
CC7 牛牛的单向链表
于 2022-05-16 19:23:12 首次发布