自己写的第一个综合链表

#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
 

/*节点的结构体     */
typedef struct student
{
  long num;
  int score;
  struct student * next;
}STU;
/*void main(){

int j;
j=LEN;
printf("%d",j);      1,2,3在这儿竟都是四个字节!

1.printf("%d",sizeof(long));
2.printf("%d",sizeof(int));
2.printf("%d",sizeof(struct student *));
}
*/
STU *creat()/*creat函数用于创建链表*/
{STU * head,*p1,*p2;
head=NULL;
p1=p2=(STU *)malloc(LEN);/*创建第一个内存段*/
 scanf("%ld,%d",&p1->num,&p1->score);
if (p1->num!=NULL)
{                  /*若满足条件,则将其定位为第一个节点*/
head=p1;

}

p1=(STU *)malloc(LEN);
scanf("%ld,%d",&p1->num,&p1->score);
head->next=p1;


while(p1->num!=NULL)
{p2->next=p1;
p2=p1;/*综合调试时,这句忘了写了,直接导致p2 始终志向head,导致error*/
p1=(STU *)malloc(LEN);   /*令p1做头p2做尾*/
scanf("%ld,%d",&p1->num,&p1->score);


}
p2->next=NULL;
return (head);/*最后条件不符,将p2的next志向0*/
}
/*void main(){
STU *head;
head=creat();
printf("%0",head);

}*/

 

 


/*输出链表*/
void print(STU * head)
{
STU * p;
p=head;
if (head!=NULL)
{

 do{
 printf("%ld,%d/n",p->num,p->score);
 p=p->next;
 }while(p!=NULL);
}

}

 


/*在表中找相应的num,并返回相应的结果,若找到则将相应的节点的地址返回*/
STU * search(STU * head,long num)
{
   STU * p=head;
   if(head==NULL)
   {printf("this is an empty table!");
   return(NULL);}
   else {
       while(p->next!=NULL && p->num!=num)
    {p=p->next;
   
   
    }
    if(p->next==NULL){printf("not exist in this table,sorry!");
    return(NULL);}
    else{printf("it exist in this table ,yeah");return(p);}
   }
}

 

 

 

/*删除链表*/
STU * del(STU * head,long num)
{
  STU *p1,*p2;
  p1=p2=head;
  if(head==NULL)      /*若是空表,则直接goto end 跳出程序*/
  {printf("this is an empty table");return(NULL);goto end;}
 
  if(head->num==num){head=p1->next;}/*删第一个*/
 
  else{
   do {
   p2=p1;
   p1=p1->next;
   }while(p1->next!=NULL && p1->num!=num);
   if(p1->next==NULL)
   {
    if(p1->num==num){p2->next=NULL;}/*删最后一个*/
    else{printf("not exist in this table");return (head);}
       /*不在表中*/
  
   }
   else{p2->next=p1->next;return(head);}
 
      /*在表的中间*/
 
 
 
 
 
 
 
 
  }
end:return(head);

}

 

/*插入节点*/
STU * insert(STU * head,STU * stud)
{
  STU * p0,*p1,*p2;
  p1=head;
  p0=stud;
  if(head==NULL){head=p0;p0->next=NULL;}/*空链表,则将其插入,链表中只有一个节点*/
  else{
  while((p0->num>p1->num)&&(p1->next!=NULL))
  {p2=p1;       /*p0是待插入的节点,p1是链表节点,p1不断向后检索*/
  p1=p1->next;
  }
  /*当条件不满足时,跳出while*/
  if(p0->num<=p1->num){
   if(p1==head)head=p0; /*志向第一个节点*/
   else p2->next=p0; /*志向中间的节点*/
   p0->next=p1;
  }
  else{p1->next=p0;p0->next=NULL;}
 
  }

return(head);
}

 


void main(){

STU *head,stu;
long del_num;
printf("intput records;/n");
head=creat();
print(head);
printf("intput the deleted num/n");
scanf("%ld",&del_num);

head=del(head,del_num);
print(head);
printf("input the inserted record:/n");
scanf("%ld,%d",&stu.num,&stu.score);
head=insert(head,&stu);
print(head);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值