单链表的创建,删除,插入,显示,排序

#include  < stdio.h >
#include 
< malloc.h >

struct  info 
{
    
int num;
    
int score;
}
;

struct  student
{
    
struct info infor;    
    
struct student *next;
}
;
int  n;

struct  student  * creat( void )
{
    
struct student *head, *p1, *p2;
    n 
= 0;
    p1 
= p2 = (struct student *)malloc(sizeof(struct student));
    scanf(
"%d, %d"&p1->infor.num, &p1->infor.score);
    head 
= NULL;
    
while (p1->infor.num != 0 )
    
{
        n
++;
        
if (n == 1)
            head 
= p1;
        
else
            p2
->next = p1;
        p2 
= p1;
        p1 
= (struct student *)malloc(sizeof(struct student));
        scanf(
"%d, %d"&p1->infor.num, &p1->infor.score);
    }

    p2
->next = NULL;
    
return head;
}


void  show( struct  student  * head)
{
    
struct student *p;
    printf(
" Now, These %d records are:  ", n);
    p 
= head;
    
if (head != NULL)
    
{
        
do
        
{
            printf(
"%d  %d ", p->infor.num, p->infor.score);
            p 
= p->next;
        }
while (p != NULL); 
    }

}


struct  student  * insert( struct  student  * head,  struct  student  * stud)
{
    
struct student *p1, *p2, *p3;
    p2 
= head;
    p1 
= stud;
    
if (head == NULL)
    
{
        head 
= p1;
        p1
->next = NULL;
    }

    
else
    
{
        
while (p1->infor.num > p2->infor.num && p2->next != NULL)
        
{
            p3 
= p2;
            p2 
= p2->next;
        }

        
if (p1->infor.num <= p2->infor.num)
        
{
            
if (head == p2)
            
{
                head 
= p1;
            }

            
else
            
{
                p3
->next = p1;
            }
 
            p1
->next = p2;
        }
 
        
else
        
{
            p2
->next = p1;
            p1
->next = NULL;
        }

    }

    n
++;
    
return head;
}


struct  student  * del( struct  student  * head,  int  num)
{
    
struct student *p1, *p2;
    
if (head == NULL)
    
{
        printf(
" , list null ");
        
return head;
    }

    p1 
= head;
        
    
while (p1->infor.num != num && p1->next != NULL)
    
{
        p2 
= p1;
        p1 
= p1->next;
    }

    
    
if (p1->infor.num == num)
    
{
        
if (p1 == head)
        
{
            head 
= p1->next;
        }

        
else
        
{
            p2
->next = p1->next;
        }

        printf(
"  delete: %d  ", num);
        n
--;
    }

    
if (p1 == NULL)
    
{
        printf(
"  %d not been found  ", num);
    }

    
return head;
}


struct  student  * sort( struct  student  * head)
{
    
struct student *p1, *p2;
    
struct info temp;
    
if (head == NULL)
    
{
        printf(
"  list null  ");
        
return head;
    }

    p1 
= head;
    p2 
= p1->next;
    
while (p1 != NULL)
    
{
        
while (p2 != NULL)
        
{
            
if (p1->infor.score > p2->infor.score)//可以设置为根据成绩排序或学好排序
            {
                temp 
= p1->infor;
                p1
->infor = p2->infor;
                p2
->infor = temp;
            }

            p2 
= p2->next;
        }

        p1 
= p1->next;
        
if (p1->next == NULL)
        
{
            
break;
        }

        
else
        
{
            p2 
= p1->next;
        }

    }

    
return head;
}


int  main()
{

    
struct student *head, *stu;
    
int num;
    
    printf(
"input records: ");
    head 
= creat();
    show(head);
    
    printf(
"  input the deleted number:");
    scanf(
"%d"&num);
    
while (num != 0)
    
{
        head 
= del(head, num);
        show(head);
        printf(
"  input the deleted number:");
        scanf(
"%d"&num);
    }

    
    printf(
"  input the inserted record:");
    stu 
= (struct student *)malloc(sizeof(struct student));
    scanf(
"%d, %d"&stu->infor.num, &stu->infor.score);
    
while (stu->infor.num != 0)
    
{
        head 
= insert(head, stu);
        show(head);
        printf(
"  input the inserted record:");
        stu 
= (struct student *)malloc(sizeof(struct student));//重新分配一次内存,以免覆盖掉上一次的节点
        scanf("%d, %d"&stu->infor.num, &stu->infor.score);
    }

    
    head 
= sort(head);
    show(head);
    
    
return 0
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值