帮编个程序呗: 将不带头节点的单链表就地逆置
就这一个题目, 本来题目是将不带节点的单链表就地逆置,估计少写个字用C语言编啊
#include <stdio.h> #include <string.h> #include <stdlib.h> struct student { int number; struct student *next; }; typedef struct student stu; stu *creatlist(int num); stu *reverselist(stu *head); void display(stu *head); int main() { int num;//学生总数 printf("pls enter the total number of students:\n"); scanf("%d",&num); stu *head; head=creatlist(num); display(head); head=reverselist(head); display(head); return 0; } stu *creatlist(int num)//创建链表 { stu *head,*pf,*pb; int i; for(i=0;i<num;i++) { pb=(stu *)malloc(sizeof(stu)); printf("pls enter the data:\n"); scanf("%d",&pb->number); if(pb!=NULL) { if(i==0) pf=head=pb; else pf->next=pb; pb->next=NULL; pf=pb; } else { printf("creat list head failed!\n"); exit(1); } } printf("creat list successfully!\n"); return head; } stu *reverselist(stu *head) { stu *pf,*pb,*t;//pf上一个结点,pb当前结点。 stu *temp; temp=head; head=head->next; pb=head; while(pb!=NULL) { pf=pb; pb=pb->next; } t=pf;//最后一个结点标记 pf->next=temp; temp->next=NULL;//头结点放到最后面去; int stop=0; stu *temp1; while(!stop) { temp1=head; head=head->next; pb=head; while(pb!=temp) { pf=pb; pb=pb->next; }//此时pb=temp; pf->next=temp1; temp1->next=temp; temp=temp1; if(head==t) stop=1; } return head; } void display(stu *head) { while(head!=NULL) { printf("%d\n",head->number); head=head->next; } printf("\n\n"); }