c语言链表萌新,萌新一枚,关于链表问题求大佬解答
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
代碼如下
#include
#include
#include
struct student
{
int num;
float score;
struct student *next;
};
struct student *creat();
struct student *del(struct student *head,int num);
void print(struct student *head);
int n;
int main()
{
system("color f5");
int a;
struct student *stu;
stu = creat();
print(stu);
printf("輸入要刪除的學生學號:");
scanf("%d",&a);
del(stu,a);
print(stu);
system("pause");
return 0;
}
struct student *creat()
{
struct student *head,*p1,*p2;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
printf("請輸入學生學號:");
scanf("%d",&p1->num);
printf("請輸入學生的分數:");
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 student *)malloc(sizeof(struct student));
printf("請輸入學生學號:");
scanf("%d",&p1->num);
printf("請輸入學生的分數:");
scanf("%f",&p2->score);
}
p2->next = NULL;
return head;
};
void print(struct student *head)
{
struct student *p;
p = head;
if(head)
{
do
{
printf("學號為%d的學生的成績是:%f\n",p->num,p->score);
p = p->next;
}while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(head)
{
printf("\n這是一個空表。");
goto END;
}
else
{
p1 = head;
while(p1->num != NULL&&p1->num != num)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num == num)
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
n = n-1;
}
else
{
printf("%d找不到。",num);
}
}
END:
return head;
};
總結
以上是生活随笔為你收集整理的c语言链表萌新,萌新一枚,关于链表问题求大佬解答的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 内存条速度对比:A品牌惊艳,B品牌稳定,
- 下一篇: 内存颗粒:从传统束缚到技术革新