实现单链表的逆置

来源:互联网 发布:ping 大包 linux 不通 编辑:程序博客网 时间:2024/05/08 04:37

//单链表 逆置
typedef struct LinkNode
{
 int data ;
 struct LinkNode *next;
}node;

void nizhi(node *head)
{
 node *s0,*s1,*s2,*p;
 s0 = head->next;
 s1=NULL;
 s2= NULL;
 while(s0)
 {
  s1=s0;
  s0=s0->next;
  s1->next = s2;
  s2=s1;
 }
 head->next = s1;
 p = head->next;
    while(p)
 {
  printf("%2d",p->data);
  p=p->next;
 }
// return head;
}

void main()
{
 node *head,*p0 ,*p1,*p2,*head1;
 head = (node*)malloc(sizeof(node));
 p0 = (node*)malloc(sizeof(node));
 p1 = (node*)malloc(sizeof(node));
 p2 = (node*)malloc(sizeof(node));
 head->next = p0;
 p0->next = p1;
 p1->next = p2;
 p2->next= NULL;
 p0->data = 0;
 p1->data = 1;
 p2->data = 2;
    nizhi(head);

}

原创粉丝点击