马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
RT,第58课离奇失踪了 是没有么还是我下载的课件不全啊
附上链表作业#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student *creat();
void print(struct student *);
struct student *dele(struct student *, int);
struct student *add(struct student *);
struct student
{
int num;
float score;
struct student *next;
};
int n = 0;
void main()
{
int m;
struct student *p = creat();
printf("\nstudent is num = %d\n\n", n);
print(p);
printf("\nplease delete num: ");
scanf("%d", &m);
p = dele(p, m);
printf("\nstudent is num = %d\n\n", n);
print(p);
p = add(p);
printf("student is num = %d\n\n", n);
print(p);
}
struct student *creat()
{
struct student *p1, *p2, *head;
char ch;
int num = 0;
p1 = p2 = (struct student *)malloc(LEN);
printf("please input num: ");
scanf("%d", &p1->num);
#if 0
while((ch = getchar()) != '\n')
{
if(ch >='0' && ch<='9')
{
num = num * 10 + (ch - '0');
}
}
p1->num = num;
#endif
printf("please input score: ");
scanf("%f", &p1->score);
head = NULL;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nplease input num: ");
scanf("%d", &p1->num);
printf("please input score: ");
scanf("%f", &p1->score);
}
free(p1);
p2->next = NULL;
return head;
}
void print(struct student *p)
{
do
{
printf("\ttnum = %d\t\tscore = %.2f\n", p->num, p->score);
p = p->next;
}while(p);
}
struct student *dele(struct student *head, int m)
{
struct student *p1, *p2;
if(!head)
{
printf("\nThis list is null!\n");
goto END;
}
p1 = head;
while(p1->num != m && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(m == p1->num)
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
free(p1);
}
printf("\nDelete No: %d succeed!\n", m);
n--;
}
else
{
printf("%d not been found!\n", m);
}
END:
return head;
}
struct student *add(struct student *p)
{
struct student *pn, *head = p, *p1;
pn = (struct student *)malloc(LEN);
printf("\nplease input add num: ");
scanf("%d", &pn->num);
printf("please input add score: ");
scanf("%f", &pn->score);
if(!p)
{
p = pn;
pn->next = NULL;
}
else
{
while(pn->num > p->num && p->next)
{
p1 = p;
p = p->next;
}
if(pn->num <= p->num)
{
if(head == p)
{
head = pn;
}
else
{
p1->next = pn;
}
pn->next = p;
}
else
{
p->next = pn;
pn->next = NULL;
}
}
n++;
return head;
}
好像确实没有
|