单向链表逆序输出c语言(单链表逆置c语言代码)

单向链表逆序输出c语言(单链表逆置c语言代码)1 include lt stdio h gt 2 include lt stdlib h gt 3 4 struct student 5 6 int data 7 struct student next 8 9 10 int iCount 定义全局变量保存代码长度 11 12 struct

大家好,我是讯享网,很高兴认识大家。



 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 }

讯享网


讯享网

小讯
上一篇 2025-05-21 22:47
下一篇 2025-06-16 12:45

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/185548.html