zoukankan      html  css  js  c++  java
  • 将一个链表按逆序排列,即将链头当链尾,链尾当链头

    #include <stdio.h>
    #include 
    <stdlib.h>
    #define N 10
    typedef 
    struct student
    {
        
    int num;
        
    float score;
        
    struct student *next;
    }STU;

    STU 
    *create()
    {
        
    int i;
        STU 
    *p,*head=NULL,*tail=head;
        
    for (i=0;i<N;i++)
        {
            p
    =(STU *)malloc(sizeof(STU));
            scanf(
    "%d%f",&p->num,&p->score);
            p
    ->next=NULL;
            
    if (p->num<0)
            {
                free(p);
                
    break;
            }
            
    if(head==NULL)
                head
    =p;
            
    else
                tail
    ->next=p;
            tail
    =p;
        }
        
    return head;
    }

    void output(STU *p)
    {
        
    while (p!=NULL)
        {
            printf(
    "%d\t%.2f\n",p->num,p->score);
            p
    =p->next;
        }
    }

    STU 
    *reverse(STU *p)
    {
        STU 
    *p1,*p2,*head;
        p1
    =p2=head=p;
        
    while (p!=NULL)
        {
            p
    =p->next;
            
    if(p1==head)
                p1
    ->next=NULL;
            
    else
                p1
    ->next=p2;
            p2
    =p1;
            p1
    =p;

        }
        head
    =p2;
        
    return head;
    }

    int main(int argc, char *argv[]) 
    {
        STU 
    *a;
        printf(
    "\n请输入链表a的信息,学号小于零时结束输入:格式(学号 成绩)\n");
        a
    =create();
        printf(
    "\n链表a的信息为:\n");
        output(a);
        a
    =reverse(a);
        printf(
    "\n反序链表a的信息为:\n");
        output(a);


        
    return 0;
    }
  • 相关阅读:
    Spring 泛型依赖注入
    Spring 注解配置(2)——@Autowired
    Spring bean注解配置(1)
    Spring bean三种创建方式
    Spring中bean的生命周期!
    spring spel表达式语言
    [转]SVN版本冲突解决详解
    [转]Mybatis极其(最)简(好)单(用)的一个分页插件
    [转]Hibernate查询对象所有字段,单个字段 ,几个字段取值的问题
    [转] JPQL
  • 原文地址:https://www.cnblogs.com/qixin622/p/1240004.html
Copyright © 2011-2022 走看看