完整的程序我不会啊!追加分!!

来源:百度知道 编辑:UC知道 时间:2024/05/25 06:43:23
我刚刚学习数据结构,它算法教的很多,可是完整的程序我却一点也写不来。我想编个单链表就地逆置,算法我已经写好了,大体是这样的。
linklist *p,*q,*r;
p=head;
q=p→next;
if(q==null)return(p)
p→next=null;
if(q→next==null)
{q→next=p;
return(q);
}
r=q→next;
while(r→next!=null)
{q→next=p;
p=q;
q=r;
r=r→next;
}
q→next=p;
r→next=q;
return(r);
}请高手帮我把完整程序写好,写的好的追加分数!

#define NULL 0

struct linklist
{
int id;
struct linklist* next;
};

linklist* reverse(linklist* head)
{
linklist *p,*q,*r;
p=head;
q=p->next;
if(q==NULL) return(p);
p->next=NULL;
if(q->next==NULL)
{q->next=p;
return(q);
}
r=q->next;
while(r->next!=NULL)
{q->next=p;
p=q;
q=r;
r=r->next;
}
q->next=p;
r->next=q;
return(r);
}

int main()
{
linklist* a1 = (struct linklist*)(malloc)(sizeof(struct linklist));
linklist* a2 = (struct linklist*)(malloc)(sizeof(struct linklist));
linklist* a3 = (struct linklist*)(malloc)(sizeof(struct linklist));
a1->id=1;
a1->next = a2;
a2->id = 2;
a2->next = a3;
a3->id = 3;
a3->next = NULL;
linklist* head = reverse(a1);
while(head!=NULL)
{
printf("%d ",head->id