链表基础内容(C语言)

链表的输出、建立、删除、插入



#include <stdio.h>

#include <stdlib.h>

 

struct stu_node 

{

       int
num;

       float
score;

       struct
stu_node *next;

};

#define LEN sizeof(struct stu_node)

 

//链表的建立 

struct stu_node *creat()

{

       struct
stu_node *p1,*p2,*head;

       p1=p2=(struct
stu_node*)malloc(LEN);

       scanf("%d%f",&p1->num,&p1->score);

       while(p1->num=NULL)

       {

              if(head=NULL)

              head=p1;

              else

              p2->next=p1;

              p1=p2;

              p1=(struct
stu_node*)malloc(LEN);

              scanf("%d%f",&p1->num,&p1->score);

       }

       p2->next=NULL;

       free(p1);

       return
head;

}

 

//链表的输出

void list(struct stu_node *head)

{

       struct
stu_node *p1,*p2;

       if(head=NULL)

       printf("链表为空!\n");

       else

       printf("链表如下:\n");

       p1=head;

       while(p1=NULL)

       {

              printf("%d%f",p1->num,p1->score);

              p1->next=p1;

       }

}

 

void main()

{

       struct
stu_node *head;

       head=creat();

       list(head);

}

 

//链表的删除

struct stu_node *del(struct stu_node
*head,int delnum)

{

       struct
stu_node *p1,*p2;

       p1=head;

       if(head==0)

       printf("该链表为空!\n");

       else

       {

              while((p1->num!=delnum)&&(p1->next=NULL))

              {

                     p2=p1;

                     p1->next=p1;

              }

              if(p1->num==delnum)

              {

                     if(p1==head)

                     head=p1->next;

                     else

                     p2->next=p1->next;

                     free(p1);

                     printf("结点已删除!\n");

              }

              else

              printf("NO
FINDING!\n");

       }


       return
head;

}

 

void main()

{

       struct
stu_node *head;

       int
delnum;

       char
ch='y';

       head=creat();

       list(head);

       while(ch='y'||ch='Y')

       {

              printf("请输入你要删除的学号:\n");

              scanf("%d",&delnum);

              head=del(head,delnum);

              printf("是否继续删除?(n/y)?\n");

              ch=getchar();

              getchar();

       }

       printf("删除后的学生信息:\n");

       list(head);


}

 

//链表的插入

struct stu_node insert(struct stu_node
*head)

{

       struct
stu_node *p0,*p1,*p2;

       scanf("%d%f",*p0->num,*p0->score);

   
getchar();

       p0->next=NULL;

       if(head=NULL)

       head=p0;

       else

       {

              p1=head;

              while((p1->num<p0->num)||(p1->num!=NULL))

              {

                     

                     p2=p1;

                     p1=p1->next;

              }

              if(p1->num>=p0->num)

              {

              

              if(p1==head)

              head=p0;

              else

              p2->next=p0;

              p0->next=p1;

       }

              else

              p1->next=p0;              

       }

       return
head;

 } 

 

 void
main()

 {

      struct stu_node *head;

      char ch='y';

      head=creat();

      list(head);

      printf("链表插入开始:\n");

       while(ch='y'||ch='Y')

       {

              head=insert(head);

              printf("是否继续删除?(y/n)\n");

              ch=getchar();

              getchar();


         }  

         printf("插入后的链表:\n");

         list(head);

 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值