#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
typedef struct node node;
node*reverse(node*head)
{
node*p;
node*q;
node*m;//头
node*n;//反p
p=head->next;
q=head;
while(p->next!=NULL)
{
p=p->next;
q=q->next;
}
m=p;
n=m;
q->next=NULL;
p->next=q;
while(head->next!=NULL)
{
p=head->next;
q=head;
while(p->next!=NULL)
{
p=p->next;
q=q->next;
}
q->next=NULL;
p->next=q;
}
return m;
}
int main(void)
{
int n;
scanf("%d",&n);
node*head;
node*p;
node*q;
p=(node*)malloc(sizeof(node));
head=p;
q=p;
scanf("%d",&p->data);
int i;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
q->next=p;
q=p;
scanf("%d",&p->data);
}
p->next=NULL;
p=NULL;
node*m;
m=reverse(head);
while(m!=NULL)
{
printf("%5d",m->data);
m=m->next;
}
return 0;
}
自己关于单链表的逆置的一种方法
最新推荐文章于 2024-09-29 08:48:38 发布