链表倒置问题答案--2008年4月份二级C

前端之家收集整理的这篇文章主要介绍了链表倒置问题答案--2008年4月份二级C前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node * next;
}*head;
struct node * newnode()
{
return (struct node *)malloc(sizeof(struct node));
}
void view()
{
struct node *p=head;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
void relist()/*链表倒置*/
{
struct node *p1,*p2,*p3;
p1=head;
p2=p1->next;
p3=p2->next;
p1->next =NULL;
while(p3)
{
p2->next =p1;
p1=p2;
p2=p3;
p3=p3->next ;
}
p2->next =p1;head=p2;} void main() { struct node *p1,*p2; int i; head=newnode(); p1=head; head->data=0; head->next=NULL; for(i=1;i<=10;i++) { p2=newnode(); p2->data=i; p2->next =NULL; p1->next=p2; p1=p2; } view(); printf("\n"); relist(); view(); }
原文链接:https://www.f2er.com/javaschema/288077.html

猜你在找的设计模式相关文章