單鏈表逆序詳解
單鏈表逆序詳解
?1、具有鏈表頭的單鏈表
一段單鏈表逆序的程序?
typedef struct student
{
???int number;
???char name[20];
???int score;
???struct student *next;
}student;
student *reverse(student *stu)
{
???student *p1,*p2,*p3;
???if(stu == NULL ||stu->next == NULL)
??????return stu;
???p1=stu->next;??????????????????????????//p1指向鏈表頭節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
???p2=p1->next;
???p1->next=NULL;
???while(p2)
???{
??????p3=p2->next;
??????p2->next = p1;
??????p1=p2;
??????p2=p3;
???}
???printf("p1 = %d,next = %dn",p1->number,p1->next->number);
???stu->next=p1;??????????????????????????//將鏈表頭節(jié)點(diǎn)指向p1