android-nuc / 18-C-Train

18级Android实验室(人工智能+移动互联) C语言培训
16 stars 10 forks source link

徐维英 (链表倒序)(已改) #27

Closed xyweiying closed 5 years ago

xyweiying commented 5 years ago

include"stdio.h"

define null 0

include"stdlib.h"

struct node { int data; struct node *next; };

//链表正序 struct node front() { struct node head,p,q; int i = 0; head = p = q = (struct node )malloc(sizeof(struct node)); scanf("%d",&p->data); head = p; while(p->next->data != 0) { i++; if(i == 1) { q = (struct node )malloc(sizeof(struct node )); scanf("%d",&q->data); head->next = q; } else { p = q; q = (struct node )malloc(sizeof(struct node )); scanf("%d",&q->data); p->next = q; } } p->next = null; return head; } //链表倒序 struct node reverse(struct node head) { int i = 0; struct node p,q; q = head; p = head->next; q->next = null; while(p->next != 0) { i++; if(i == 1) { head = head; } if(i == 2) { head = p; } else { head = p->next; p->next = q; q = p; p = head; } } p->next = q; return p; } //输出 void print(struct node head) { struct node *q; q = head; while(q != null) { printf("%5d",q->data); q = q->next; } }

main() { struct node *head; printf("输入数据:"); head = front(); printf("正序输出:\n"); print(head);

printf("\n"); printf("倒序输出:\n"); head = reverse(head); print(head); }

wmpscc commented 5 years ago

点评:

修改!