Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2287046
  • 博文数量: 668
  • 博客积分: 10016
  • 博客等级: 上将
  • 技术积分: 8588
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-29 19:22
文章分类

全部博文(668)

文章存档

2011年(1)

2010年(2)

2009年(273)

2008年(392)

分类:

2009-05-13 11:49:49

#include
#include

#define LEN sizeof(struct student)
#define FORMANT() \
                  printf("==================================\n");\
                  printf("num     eng     math     avg(auto)\n");\
                  printf("==================================\n")

struct student
{
       int    num;
       float  english;
       float  math;
       float  avg;
       struct student *next;
}; 

int n = 0;

struct student *creat();
void prt(struct student *head);
struct student *exchange(struct student *head);

int main(int argc, char *argv[])
{
  struct student stu, *head;
  int pos,position;
  
  printf("创建链表:\n");
  head = creat();
  prt(head);
  printf("\n逆转链表:\n");
  head = exchange(head);
  prt(head);
  
  system("PAUSE");    
  return 0;
}

struct student *creat()
{
       struct student *p1, *p2, *head = NULL;
       
       p1 = p2 = (struct student *)malloc(LEN);
       printf("请输入学生的信息:\n");
       FORMANT();
       scanf("%d%f%f", &p1->num,&p1->english,&p1->math);
       p1->avg = (p1->english + p1->math)/2;
       
       while(p1->num != 0)
       {
               n++;
               if(n == 1)
               {
                    head = p1; 
               }      
               else
               {
                   p2->next = p1;
               }
               p2 = p1; 
               p1 = (struct student *)malloc(LEN);
               scanf("%d%f%f", &p1->num,&p1->english,&p1->math);
               p1->avg = (p1->english + p1->math)/2;
       }    
       p1->next = NULL;
       return head;
}

void prt(struct student *head)
{
     struct student *p1;
     p1 = head;
     printf("\n显示链表:\nnow there are %d datas.\n",n);
     FORMANT();
     while(p1 != NULL)
     {
           printf("%-8d%-8.2f%-9.2f%-8.2f\n", p1->num, p1->english, p1->math, p1->avg);
           p1=p1->next;
     }
}

struct student *exchange(struct student *head)
{
       struct student *p1, *p2;
       
       p1 = head;
       if(p1->next == NULL)
       {
            head = p1;
       }
       else
       {
           p2 = p1->next;
           head = exchange(p2);
           p2->next = p1;
           p1->next = NULL;
       }
       return head;
}

阅读(1303) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~