|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#define LN sizeof(struct student)
struct student
{
int num;
int score;
struct student *next;
};
struct student *add(); //增加链表+填充内容
struct student *del(struct student *,int m); //删除节点
void print(struct student *head); //打印链表
int n;
int main()
{
struct student *stu;
int m;
stu = add();
print(stu);
printf("\nplease input the delete num:"); //没有读到这里,似乎出现了问题
scanf("%d",&m);
print(del(stu,m));
system("pause");
return 0;
}
struct student *add()
{
struct student *head;
struct student *p1,*p2;
p1 = p2 = (struct student *)malloc(LN);
printf("please input the num:");
scanf("%d",&p1->num);
printf("please input the score:");
scanf("%d",&p1->score);
head = NULL;
n = 0;
while(p1->num != 0)
{
n++;
if(n == 1)
{
head = p1;
}else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LN);
printf("please input the num:");
scanf("%d",&p1->num);
printf("please input the score:");
scanf("%d",&p1->score);
}
return head;
}
struct student *del(struct student *head,int m)
{
struct student *p1,*p2;
p1 = head;
if(p1 == NULL)
{
printf("This is a NULL!!\n");
goto END;
}
while(p1->num != m && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(m == head->num)
{
head = p1->next;
}else
{
p2->next = p1->next;
}
printf("\nDelete %d number is succeful!",m);
n = n-1;
END:
return head;
}
void print(struct student *head)
{
struct student *p;
p = head;
printf("\n\n");
printf("There is %d records!",n);
printf("\n\n");
while(p->next != NULL)
{
printf("学号为:%d,分数是:%d\n",p->num,p->score);
p = p->next;
}
}
不知道为什么没有读到删除的那个部分 |
|