链表在原空间逆序

 

注意:这个链表的值是从hesd->next开始的

#include<stdio.h>
#include<stdlib.h>

typedef struct stu{
    int data;
    struct stu *next;
}st;
st *init_list()
{
    st *head;
    head = (st *)malloc(sizeof(st));
    head->next = NULL;    
    return head;
}

st *create()
{
    int n,x;
    st *head,*r,*s;
    head = init_list();
    r = head;
    printf("请输入元素个数:");
    scanf("%d",&n);
    while(n)
    {
    s = (st *)malloc(sizeof(st));    
    scanf("%d",&x);
    s->data = x;
    s->next = r->next;
    r->next = s;
    r = s;        
    n--;    
    } 
    return head;    
        
}
st *list_sequence(st *head)//链表逆序 
{
    st *p,*q;
    p = head->next;
    head->next = NULL;//置为空表 
    while(p)
    {
    q = p;
    p = p->next;
    q->next = head->next;//类似于头插 
    head->next = q;            
    } 
 return head;    
}  
main()
{
    st *head,*p;
    head = create();
    head = list_sequence(head);
    p = head->next;
    while(p)
    {
        printf("%d ",p->data);
        p = p->next;
    }
     
}   

 

posted @ 2019-07-24 10:55  浅滩浅  阅读(175)  评论(0编辑  收藏  举报