若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

2221: 两个链表之间问题(线性表)

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,478

题目描述

已知两个单链表A和B,其头指针分别为heada和headb,编写一个过程从单链表A中删除自第i个元素起的共len个元素,然后将单链表A插入到单链表B的第j个元素之前。

输入

前三个数分别表示i,len,j

一个整数m,表示A链表的长度m。
m个数表示A链表中的m个数据元素。
一个整数n,表示B链表的长度n。
n个数表示B链表中的n个数据元素。

输出

操作后的结果。

样例输入

 1 3 5

11

13 5 14 62 3 43 71 5 72 34 5

15

5 20 3 53 7 81 5 42 6 8 4 6 9 10 23

样例输出

5 20 3 53 62 3 43 71 5 72 34 5 7 81 5 42 6 8 4 6 9 10 23 



参考代码

#include<stdio.h>      
#include<malloc.h>      
struct node 
{
    int data;
    struct node *next;
}
;
struct node *creat(int m) 
{
    struct node *head,*p,*q;
    head=(struct node *)malloc(sizeof(struct node));
    q = head;
    while((m--)&&(p=(struct node *)malloc(sizeof(struct node)) , scanf("%d",&p->data)!=EOF)) 
    {
        q->next = p;
        q = p;
    }
    q->next = NULL;
    return head;
}
int main() 
{
    int i,len,j;
    int m,n;
    struct node *heada,*headb,*p,*q;
    scanf("%d%d%d;",&i,&len,&j);
    scanf("%d",&m);
    heada = creat(m);
    scanf("%d",&n);
    headb = creat(n);
    p = q = heada;
    while(--i) 
    {
        p = p->next;
        q = q->next;
    }
    while(len--) 
    {
        q = q->next;
    }
    p->next = q->next;
    while(q->next != NULL)
            q = q->next;
    p=headb;
    while(--j)
            p = p->next;
    q->next = p->next;
    p->next = heada->next;
    p = headb->next;
    while(p != NULL) 
    {
        printf("%d ",p->data);
        p = p->next;
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 原站点 Skip
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist