实验11 链表操作

1099 [填空题]链表的合并 

下面程序创建两个链表,然后将第二个链表合并到第一个链表未尾,但合并部分的代码未完成,请你完成这部分代码。
#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;//如果只建立一个链表 头指针指向p1
        else p2->next=p1;
        p2=p1;
    }
    return(head);
}

struct student *merge(struct student *head, struct student *head2)
{
   struct student *p1;
   p1=head;
   while (p1->next!=NULL)
    p1=p1->next;
   p1->next=head2;
   return (head);
   
   /* struct student *p1;//定义结构体指针变量p1
    p1=head;//p1指向head
    while(p1->next!=NULL)p1=p1->next;//使p1指向head链表的最后一个元素
    p1->next=head2;//p1的next指向head2头指针
    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");
    }
}

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);
}

//标程
if(head==NULL) return head2;
   struct student *p=head;
   while(p->next!=NULL) p=p->next;
   p->next = head2;
   return head;

1098 [填空]链表结点的插入 

完成插入链表结点的函数(按学号顺序),并调试通过、提交。
//题目
#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);
}

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是空表 则令head指向p0 ,p0的next指向null
    {
        head=p0;
        p0->next=NULL;
    }
    else //否则当需要插入的号码大于原有表中的号码且原有表p1的next不为空时,
        //让p2指向p1,p1指向下一个
    {
        while((p0->num>p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        if(p0->num<=p1->num)
//说明p0在p1前面,如果前面没有其他元素了,head->p0->p1;
//如果前面还有其他元素head->...->p2->p0->p1
        {
            if(head==p1)head=p0;
            else p2->next=p0;
            p0->next=p1;
        }
        else//如果p0在p1后面 p1->p0->null
        {
            p1->next=p0;
            p0->next=NULL;
        }
    }
    return(head);
--------------------------------
}

main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);   
    head=create(n);
    print(head);
    stu=(struct student *)malloc(LEN);
    scanf("%ld",&stu->num);        
    scanf("%d",&stu->score);    
    stu->next = NULL;
    head=insert(head,stu);
    print(head);
}
//标程
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);

1104 [填空题]链表的倒序

下面程序,先创建一个链表,然后调用reverse函数,将链表中各结点变为倒序排列。请完成reverse函数,
#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);
}

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)
{
--------------------------------------------------
  struct student *p1 = head,*p2 = NULL,*p3 = NULL;//如果不初始化为NULL可能会不行,可能!
    while (p1!= NULL) {//迭代算法。把链表中的每个数往后挪,最后一个数挪到第一个,遍历链表,
                       //最后达到倒序的效果。
        p2 = p1->next;
        p1->next = p3;
        p3 = p1;
        p1 = p2;
    }
    return p3;
--------------------------------------------------
}
main()
{
    struct student *head,*stu;
    int n;
    scanf("%d",&n);  
    head=create(n);
    print(head);
    head=reverse(head);
    print(head);
}

链表逆序算法_石锅拌饭的博客-CSDN博客,找到了两种方法,迭代算法、递归算法

1101 [填空题]链表的排序 

下面程序,先创建一个链表(链表中各结点未按学号由小到大排序),然后调用sort函数,将链表中各结点按学号由小到大排序。
#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);
}

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=head,*p2=NULL,*p3=NULL;
//    while(p1!=NULL)
//    {
//        if(p1->next<p1)
//           {
//            p2=p1;
//            p1=p1->next;
//            p1->next=p2;
//           }
//    }
//    return(head);

//保持冒泡,将满足判断语句(后比前小)结构体里面的成员替换就好了。比如1-5-4-6,当p指向5时,
//发现下一个的num更小,那么我就把下一个结点的num和我当前的num换一下,结点还是原来的结点,
//无需变动,只需改变成员变量就行了。
 struct student *p1,*p2;
        for(p1=head;p1!=NULL;p1=p1->next)
        {
            for(p2=p1->next;p2!=NULL;p2=p2->next)
            {
                if(p2->num<p1->num)
                    {
                        int t;
                        t = p1->num;
                        p1->num=p2->num;
                        p2->num=t;
                        t=p1->score;
                        p1->score=p2->score;
                        p2->score=t;
                    }
            }
        }
           return head;
//标程
 struct student *head2=NULL;
    if(head==NULL) return NULL;
    struct student *p=head, *p2;
    while(p!=NULL)
    {    //应该是将链表中的结点依次,按顺序大小插入到链表中的位置
        p2=p->next;
        p->next=NULL;
        head2=insert(head2,p);
        p=p2;
    }
    return head2;
-------------------------------------------------
}

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表归并是指将两个有序链表合并成一个有序链表的过程。实验11_9的目的是通过链表归并的实现,加深对链表操作的理解和应用。 具体实现步骤如下: 1. 定义一个链表结构体Node,包含一个整型数据域和一个指向下一个节点的指针域。 2. 定义一个链表归并函数merge,输入参数为两个链表的头节点指针head1和head2,输出参数为归并后的链表头节点指针head3。 3. 在merge函数内部,定义一个临时节点指针p,将其指向头节点较小的那个链表的头节点,将头节点较大的那个链表的头节点赋值给head3。 4. 在while循环中,判断p的指针域是否为空,如果为空,则将p的指针域指向头节点较大的那个链表的头节点,并将该链表的头节点指针head3赋值给p。 5. 在while循环中,判断p所指向的节点的数据域与另一个链表头节点的数据域的大小关系,将p的指针域指向数据域较小的节点。 6. 在while循环结束后,将剩余的节点直接接到归并后的链表尾部。 完整代码如下: ``` #include<iostream> using namespace std; struct Node{ int data; Node* next; }; Node* merge(Node* head1,Node* head2){ if(head1==NULL) return head2; if(head2==NULL) return head1; Node* head3=NULL; Node* p=NULL; if(head1->data<head2->data){ p=head1; head1=head1->next; } else{ p=head2; head2=head2->next; } head3=p; while(head1!=NULL&&head2!=NULL){ if(head1->data<head2->data){ p->next=head1; head1=head1->next; } else{ p->next=head2; head2=head2->next; } p=p->next; } if(head1!=NULL) p->next=head1; if(head2!=NULL) p->next=head2; return head3; } int main(){ Node* head1=new Node; Node* head2=new Node; Node* p1=head1; Node* p2=head2; int n,m; cout<<"请输入第一个链表的长度:"; cin>>n; cout<<"请输入第一个链表的元素:"; for(int i=0;i<n;i++){ int x; cin>>x; Node* node=new Node; node->data=x; p1->next=node; p1=node; } cout<<"请输入第二个链表的长度:"; cin>>m; cout<<"请输入第二个链表的元素:"; for(int i=0;i<m;i++){ int x; cin>>x; Node* node=new Node; node->data=x; p2->next=node; p2=node; } Node* head3=merge(head1->next,head2->next); cout<<"合并后的链表为:"; while(head3!=NULL){ cout<<head3->data<<" "; head3=head3->next; } cout<<endl; return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值