|  | 
 
 发表于 2014-2-12 16:42:11
|
显示全部楼层 
| 本帖最后由 oggplay 于 2014-2-21 00:02 编辑 
 好吧,今天又巩固了一下结构体,以下程序编译成功
 复制代码#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct stuednt)
int n;   //全局变量
struct stuednt
{
        int num;
        float score;
        struct stuednt *next;
};
struct stuednt *creat();     //创建链表
struct stuednt *del(struct stuednt *head, int num); // 传递的是 头和要删除的数
struct stuednt *print(struct stuednt *head);  //打印出来
int main()
{
        struct student *stu, *p;
        int n;
      p=  creat();
       
        print(p);
        printf("Please enter the delete num : ");
        scanf("%d", &n);
        print(del(p, n));//打印删除后的所有学生成绩
       
        system("pause");
        return 1;
}
struct stuednt *creat()
{
        struct stuednt *head, *p1, *p2;
        p1 = p2 = (struct stuednt *)malloc(LEN);
        printf("Please enter the num : ");
        scanf("%d", &p1->num);
        printf("Please enter the score : ");
        scanf("%f", &p1->score);
        head = NULL;
        n = 0;
        while(p1->num)
        {
                n++;
                if(n == 1)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                p1 = (struct stuednt *)malloc(LEN);
                printf("Please enter the num :");
            scanf("%d", &p1->num);
            printf("Please enter the score :");
            scanf("%f", &p1->score);
        }
        p2->next = NULL;
        return head;
}
struct stuednt *print(struct stuednt *p)
{
  struct stuednt *p1;
  p1=p;
  while(p)
    { printf("%d号学生的成绩是:%f\n\n",p->num,p->score);
     p = p->next;}
  p=p1;  return p;
}
struct stuednt  *del(struct stuednt *head, int num)
{
        struct stuednt *p1, *p2;
        if(head == NULL)
        {
                printf("在忽悠人!\n");
               return head;
        }
        p1 = head;
        while(p1->num != num && p1->next != NULL)
        {
                p2 = p1;
                p1 = p1->next;
        }
        if(p1->num == num)
        {
                if(head == p1)
                {
                        head = p1->next;
                }
                else
                {
                        p2->next = p1->next;
                }
        printf("\nDelete No: %d succeed!\n",num);
                n = n-1;
        }
        else
        {
                printf("%d not been found!\n", num);
        }
        return head;
}
 
 | 
 
  |