鱼C论坛

 找回密码
 立即注册
查看: 2567|回复: 2

看过小甲鱼老师视频的或者没看过的帮忙解答下,疑问在代码中写着!

[复制链接]
发表于 2012-7-28 14:50:00 | 显示全部楼层 |阅读模式
1鱼币
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student //定义链表结构
{
int num;
int score;
struct student *next;
};
void main ()
{
struct student *creat(struct student *head);      //链表建立函数
struct student *del(struct student *head,int num);     //链表删除函数
struct student *insert(struct student *head,struct student *stu); //链表插入函数
void print(struct student *head);         //链表打印函数
int m;

struct student *head,*stu;
head=NULL;

head=creat(head);
print(head);
printf("\n");


printf("Please input the num you want to delete:");
scanf("%d",&m);
printf("\n");
print(del(head,m));
printf("\n");

stu=(struct student *)malloc(sizeof(struct student));
printf("Please input the num you want to insert:");
scanf("%d",&stu->num);
printf("Please input the score you want to insert:");
scanf("%d",&stu->score);
printf("\n");
head=insert(head,stu);
print(head);


}
int n=0;
struct student *creat(struct student *head)
{
struct student *p1,*p2;
p1=p2=(struct student *)malloc(sizeof(struct student));
printf("Please intput the num:");
scanf("%d",&p1->num);
printf("Please input the score:");
scanf("%d",&p1->score);
printf("\n");
p1->next=NULL;
while(p1->num!=0)
{
  n++;
  if(head==NULL)
  {
   head=p1;
  }
  else
  {
   p2->next=p1;
  }
  p2=p1;
  p1=(struct student *)malloc(sizeof(struct student));
  printf("Please intput the num: ");
  scanf("%d",&p1->num);
  printf("Please input the score: ");
  scanf("%d",&p1->score);
  printf("\n");
}
p2->next=NULL;
return head;

}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(head==NULL)
{
  printf("It's a null!\n");
  goto END;
}
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
  p2=p1;
  p1=p1->next;
}
if(p1->num==num)
{
  if(head->num==num)
  {
   head=head->next;
  }
  else
  {
   p2->next=p1->next;
  }
  printf("The num you input has been deleted!");
  n--;
}
else
{
  printf("The num you input is nut in!\n");
}
END:
return head;
}
struct student *insert(struct student *head,struct student *stu)
{
struct student *p0,*p1,*p2;
p0=stu;
p1=head;
if(head==NULL)
{
  head=p0;
  p0->next=NULL;
}
else
{
  while(p0->num>p1->num&&p1->next!=NULL)
  {
   p2=p1;
   p1=p1->next;
  }
  if(p0->num<=p1->num)
  {
   if(p1==head)//为什么这里不能使p0==head,因为如果等于头指针地址,即p1=head,p1==p0,那么head=p0是错的么?
   {
    head=p0;
   }
   else
   {
    p2->next=p0;
    p0->next=p1;
   }
  }
  else
  {
   p1->next=p0;
   p0->next=NULL;
  }
  
}
n++;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
printf("There are %d record(s)!\n",n);
while(p!=NULL)
{
  printf("%d %d\n",p->num,p->score);
  p=p->next;
}
printf("\n");
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2012-7-28 14:50:34 | 显示全部楼层
这是一个链表的建立、删除和插入函数!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2012-7-28 18:33:32 | 显示全部楼层
那分,,,走人
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 17:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表