标题:链表的逆置大问题
只看楼主
浅暗花璃
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2016-3-31
结帖率:77.78%
 问题点数:0 回复次数:0 
链表的逆置大问题
#include <stdio.h>
#include <stdlib.h>
struct student {
    int old;
    struct student *next;
};

struct student *create();
 struct student *f(struct student *head);
void output(struct student *head);

void main()
{

    struct student *head;
    head=create();
     head=f(head);
    output(head);
}
struct student *create()
{
    struct student *head,*p,*q;
    int n,i=0;
    head=(struct student *)malloc(sizeof(struct student));
   
    p=(struct student *)malloc(sizeof(struct student));
    printf("plase input 节点数:");
    scanf("%d",&n);
    printf("plase input 年龄:");
    scanf("%d",&p->old);
    head->next=p;
    p->next=NULL;
    while(1)
    {
        if(i==n)
            break;
            i++;
        q=(struct student *)malloc(sizeof(struct student));
        if(i<n){

            printf("plase input 年龄:");
            scanf("%d",&q->old);
            p->next=q;
            
            p=q;
        }
        
    }
    free(q);
   p->next=NULL;
    return(head);
}
  struct student *f(struct student *head)
{
    struct student *p,*q,*s;
    int i=0;
  
    p=head->next;
    q=p->next;
    s=q->next;
    while(s!=NULL)
    {
        q->next=p;
        p=q;
        q=s;
        s=s->next;
        
    }
   
   
    q->next=p;
    head->next=q;
    p->next->next=NULL;

    return(head);
   
}
   



void output(struct student *head)
{
    struct student *p=head->next ;
    while(p!=NULL)
    {

        printf("%d\n",p->old);
        p=p->next;
    }
}
为什么3个节点是好的,多了就不行了




搜索更多相关主题的帖子: include create 
2016-06-10 11:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465776-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.867521 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved