标签:include log turn print pen gif one == creat
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。
代码:
1 #include<stdio.h> 2 #include"malloc.h" 3 typedef struct node 4 { 5 struct node *next; 6 int data; 7 }*ListNode; 8 9 //尾插法创建链表(不带头结点) 10 ListNode creatrList() 11 { 12 ListNode p = (ListNode)malloc(sizeof(ListNode)); 13 ListNode s,q; 14 p ->next = NULL; 15 q = p; 16 int x=0; 17 printf("创建链表,以-1结束:"); 18 scanf_s("%d", &x); 19 while (x!=-1) 20 { 21 s = (ListNode)malloc(sizeof(ListNode)); 22 s->next = NULL; 23 p->data = x; 24 p->next = s; 25 p = s; 26 scanf_s("%d", &x); 27 } 28 return q; 29 } 30 //反转链表 31 ListNode ReverseList(ListNode pHead) 32 { 33 ListNode prev = NULL; 34 ListNode pNode = pHead; 35 ListNode ReverPHead=NULL; 36 while (pNode != NULL) 37 { 38 ListNode pNext = pNode->next; 39 if (pNext == NULL) 40 ReverPHead = pNode; 41 pNode->next = prev; 42 prev = pNode; 43 pNode = pNext; 44 } 45 printf("%d",ReverPHead->data); 46 return ReverPHead; 47 } 48 49 int main() 50 { 51 ListNode h,RH; 52 h = creatrList(); 53 54 RH=ReverseList(h); 55 if (RH->next == NULL) 56 printf("链表为空\n"); 57 else 58 { 59 RH = RH->next; 60 while (RH != NULL) 61 { 62 printf("%3d", RH->data); 63 RH = RH->next; 64 } 65 printf("\n"); 66 } 67 return 0; 68 69 } 70 /* 71 创建链表,以-1结束:1 2 3 4 5 6 -1 72 6 5 4 3 2 1 73 请按任意键继续. . . 74 75 创建链表,以-1结束:-1 76 链表为空 77 请按任意键继续. . . 78 79 创建链表,以-1结束:4 -1 80 4 81 请按任意键继续. . . 82 */
标签:include log turn print pen gif one == creat
原文地址:http://www.cnblogs.com/xyzyj/p/7392265.html