#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#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(); }