1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct student 5 { 6 int data; 7 struct student *next; 8 }; 9 10 int iCount; //定义全局变量保存代码长度 11 12 struct student *Create() 13 { 14 struct student *pHead = NULL; 15 struct student *pNew,*pEnd; 16 iCount = 0; 17 pEnd = pNew = (struct student*)malloc(sizeof(struct student)); 18 printf("请输入数据:"); 19 scanf("%d",&pNew->data); 20 while(pNew->data!=0) 21 { 22 iCount++; 23 if(iCount == 1) //从本条if语句开始就要多注意指针的交接了哦,比较容易错 24 { 25 pNew->next = NULL; 26 pEnd = pNew; 27 pHead = pNew; 28 } 29 else 30 { 31 pNew->next = NULL; 32 pEnd->next = pNew; 33 pEnd = pNew; 34 } 35 pNew = (struct student*)malloc(sizeof(struct student)); 36 printf("请输入数据:"); 37 scanf("%d",&pNew->data); 38 } 39 free(pNew); 40 return pHead; 41 } 42 43 struct student *reverse(struct student *pHead) //链表逆置函数 44 { 45 struct student *p,*q,*t; //p为前置指针,q为后置指针,t为交换指针 46 q = pHead; 47 p = (q->next); 48 q->next = NULL; 49 while(t!=NULL) 50 { 51 t = p->next; 52 p->next = q; 53 q = p; 54 if(t!=NULL) p = t; 55 else; 56 } 57 return (p); 58 } 59 60 void showlist(struct student *pHead) //指针输出函数 61 { 62 struct student *temp; 63 temp = pHead; 64 65 while(temp) 66 { 67 printf(" %d ",temp->data); 68 temp = temp->next; 69 } 70 printf(" "); 71 } 72 73 int main() 74 { 75 struct student *first; 76 77 first = Create(); 78 printf("链表逆置前的数据:"); 79 showlist(first); 80 81 first = reverse(first); 82 83 printf("链表逆置后的数据:"); 84 showlist(first); 85 86 return 0; 87 }到此这篇单向链表逆序输出c语言(单链表逆置c语言代码)的文章就 介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!
版权声明:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权、违法违规、事实不符,请将相关资料发送至xkadmin@xkablog.com进行投诉反馈,一经查实,立即处理!
转载请注明出处,原文链接:https://www.xkablog.com/goyykf/82239.html