1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <stdio.h> #include <stdlib.h>
struct node { int data; struct node* next; }; int main(){ struct node *head,*t,*q,*p; int i, a, n; head = NULL; q = NULL; printf("你要输入几位数:"); scanf_s("%d", &n); printf("请输入数字:"); for (i = 1; i <= n; i++) { p = malloc(sizeof(struct node)); scanf_s("%d",& p->data); p->next = NULL; if (head == NULL) { head = p; } else { q->next = p; } q = p; } printf("请输入你要插入的数:"); scanf_s("%d", &a); t = head; while (t != NULL) { if (t->next->data > a) { p = malloc(sizeof(struct node)); p->data = a; p->next = t->next; t->next = p; break; } t = t->next;
} t = head; while (t != NULL) { printf("%d ", t->data); t = t->next; }
getchar(); getchar(); return 0; }
|