Chinaunix首页 | 论坛 | 博客
  • 博客访问: 185009
  • 博文数量: 36
  • 博客积分: 230
  • 博客等级: 二等列兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-22 18:09
文章分类

全部博文(36)

文章存档

2013年(29)

2011年(5)

2010年(2)

我的朋友

分类: C/C++

2013-03-18 14:49:49

链表创建,打印,插入,删除等操作。(谭浩强:《c语言程序设计-v2》)
#include
#include
#include

#define LEN sizeof(struct student) 
struct student
{
  long num;
  float score;
  struct student *next;
};
int n;
// ---- creat chain
struct student *creat_chain(void)
{
   struct student *head = NULL, *p = NULL, *q = NULL
   n = 0;
   q = p = (struct student *) malloc(LEN); 
  scanf("%ld, %f", &p->num, &p->score);  // 输入时用逗号隔开
  printf("Num is %d,Score is %5.1f \n", p->num, p->score);
  while (p->num != 0)
  { 
    n=n+1;
    if (n == 1)     head = p;
    else   q->next = p;
  q = p;
   p = (struct student *) malloc(LEN); 
   scanf("%ld, %f", &p->num, &p->score);
   printf("Num is %d,Score is %f \n", p->num, p->score);
  }
 q->next = NULL;
 return (head);
}

//---- print chain

void print_chain(struct student *head)
{
    struct student *p;
    printf("The chain is : \n");
    p = head;
  if (head != NULL)
     do  
    {
       printf("%ld, %5.1f\n", p->num, p->score);
       p = p->next;
    }
   while(p!=NULL);
 }

//----

int main(void)
{
  struct student *head;
   head = creat_chain();
   print_chain(head);
   return 0;
}  

/*--  E  --*/
谢谢欣赏
阅读(825) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~