考试 链表

链表的倒序

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

struct student
{
     long num;
     int score;
     struct student *next;
};

struct student *create(int n)
{
     struct student *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct student *)malloc(LEN);
        scanf("%ld",&p1->num);
        scanf("%d",&p1->score);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

void print(struct student *head)
{
    struct student *p;
    p=head;
    while(p!=NULL)
    {
        printf("%8ld%8d",p->num,p->score);
        p=p->next;
        printf("\n");
    }
}

struct student *reverse(struct student *head)
{
    /*
    0---->0---->0
    |     |     |
    p1    p2    p3
    */
    struct student *p1=head,*p2=p1->next,*p3=NULL;
    while(p2!=NULL)//当前指针不为空时执行
    {
       p3=p2->next;//p3指向p2的后一个结点(保留p2的后继结点)
       p2->next=p1;//当前指针的下一个结点指向当前结点
       if(p1==head)
        p1->next=NULL;//如果p1是头结点的话,那么就让头结点的next为空,不然逆向之前的头指针还是会指向第二个结点
       p1=p2;//当前指针后移
       p2=p3;//p2指向下一个结点
    }
    head=p1;//指向当前结点
    return(head);
}

main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);
    head=create(n);
    print(head);
    head=reverse(head);
    print(head);
}

链表的合并

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

struct student
{
     long num;
     int score;
     struct student *next;
};

struct student *create(int n)//建立链表函数
{
     struct student *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct student *)malloc(LEN);//申请空间
        scanf("%ld",&p1->num);
        scanf("%d",&p1->score);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

struct student *merge(struct student *head, struct student *head2)//连接链表
{
   struct student *p;
   p=head;
    while(head->next!=NULL)//先让链表循环到最后一个结点
    {
        head=head->next;
    }
    head->next=head2;//将最后的结点连接到第二个链表
    return p;//返回连接好的链表
}


void print(struct student *head)//输出链表函数
{
    struct student *p;
    p=head;
    while(p!=NULL)
    {
        printf("%8ld%8d",p->num,p->score);
        p=p->next;
        printf("\n");
    }
}
main()
{
    struct student *head, *head2;
    int n;
    long del_num;
    scanf("%d",&n);
    head=create(n);//创建第一个链表
    print(head);//输出第一个链表
    scanf("%d",&n);
    head2=create(n);//创建第二个链表
    print(head2);//输出第二个链表
    head = merge(head, head2);//连接链表
    print(head);//输出连接后的链表
    return 0;
}

链表的排序

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)

struct student
{
     long num;
     int score;
     struct student *next;
};

struct student *create(int n)
{
     struct student *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct student *)malloc(LEN);
        scanf("%ld",&p1->num);
        scanf("%d",&p1->score);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

void print(struct student *head)
{
    struct student *p;
    p=head;
    while(p!=NULL)
    {
        printf("%8ld%8d",p->num,p->score);
        p=p->next;
        printf("\n");
    }
}
struct student *insert(struct student *head, struct student *stud)
{  struct student *p0,*p1,*p2;
    p1=head;  p0=stud;
    if(head==NULL)
      {head=p0;}
    else
   { while( (p0->num > p1->num) && (p1->next!=NULL) )
       { p2=p1;     p1=p1->next;}
     if( p0->num <= p1->num )
      {  if( head==p1 ) head=p0;
           else p2->next=p0;
         p0->next=p1; }
     else {  p1->next=p0;}
     }
    return(head);
}
struct student *del(struct student *head,long num)
{
    struct student *p1,*p2;
    p1=head;
    while(p1!=NULL)
    {
        if(p1->num == num)
        {
          if(p1 == head) head=p1->next;
          else p2->next=p1->next;
          free(p1);
          break;
        }
        p2=p1;
        p1=p1->next;
    }
    return(head);
}
struct student *sort(struct student *head)
{
   struct student *p1=NULL,*p2=NULL,*min,*q=NULL,*p;
   while(head!=NULL)
   {for(p=head,min=head;p->next!=NULL;p=p->next)
       {   if(p->next->num<min->num)
            { q=p;
            min=p->next;
           }}
   if(p1==NULL)
   {p1=min;
   p2=min;}
   else
   {p2->next=min;
    p2=min;}
   if(min==head)
   {
   head=head->next;
   }
   else
   {
       q->next=min->next;
   }}
   p2->next=NULL;
   return p1;
}

main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);
    head=create(n);
    print(head);
    head=sort(head);
    print(head);
}

链表的有序合并

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct DATA)

struct DATA
{
     long num;
     struct DATA *next;
};

struct DATA *create(int n)
{
     struct DATA *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct DATA *)malloc(LEN);
        scanf("%ld",&p1->num);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}
struct DATA *merge(struct DATA *head, struct DATA *head2)
{
    struct DATA *p;
    if(head==NULL)
        return head2;
    if(head2==NULL)
        return head;
    p=head;
    while(p->next!=NULL)
        p=p->next;
        p->next=head2;
    return head;
}
struct DATA *insert(struct DATA *head, struct DATA *d)
{
    struct DATA *p0,*p1,*p2;
    d->next=NULL;
    p1=head;
    p0=d;
    if(head==NULL)
    {
        head=p0;
    }
    else
    {
        while((p0->num>=p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<p1->num)
        {
            if(head==p1)
                head=p0;
            else
                p2->next=p0;
            p0->next=p1;
        }
        else
        {
            p1->next=p0;
        }
    }
    return head;
}
struct DATA *sort(struct DATA *head)
{
    struct DATA *head2=NULL;
    struct DATA *p;
    p=head;
    while(p!=NULL)
    {
        head=head->next;
        head2=insert(head2,p);
        p=head;
    }
    head=head2;
    return head;
}

void print(struct DATA *head)
{
    struct DATA *p;
    p=head;
    while(p!=NULL)
    {
        printf("%ld",p->num);
        p=p->next;
        printf("\n");
    }
}
main()
{
    struct DATA *head, *head2;
    int n;
    long del_num;
    scanf("%d",&n);
    head=create(n);
    scanf("%d",&n);
    head2=create(n);
    head = merge(head, head2);
    head = sort(head);
    print(head);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值