zoukankan      html  css  js  c++  java
  • 师--链表的结点插入

    师--链表的结点插入

    Time Limit: 1000 ms Memory Limit: 65536 KiB

    Problem Description

    给出一个只有头指针的链表和 n 次操作,每次操作为在链表的第 m 个元素后面插入一个新元素x。若m 大于链表的元素总数则将x放在链表的最后。

    Input

    多组输入。每组数据首先输入一个整数n(n∈[1,100]),代表有n次操作。
    接下来的n行,每行有两个整数Mi(Mi∈[0,10000]),Xi。

    Output

    对于每组数据。从前到后输出链表的所有元素,两个元素之间用空格隔开。

    Sample Input

    4
    1 1
    1 2
    0 3
    100 4

    Sample Output

    3 1 2 4

    Hint

     

    Source

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef struct node
     4 {
     5     int data;
     6     struct node*next;
     7 }tree;
     8 int main()
     9 {
    10     int n,m,x;
    11     struct node*head,*tail,*p,*q;
    12     while(~scanf("%d",&n))//注意多组输入 
    13     {
    14         head=new tree;
    15         head->next=NULL;//顺序建立链表 
    16         tail=head; 
    17         while(n>0)
    18         {
    19             q=head;//一开始将q游动指针放在头指针位置处来进行初始化 
    20             scanf("%d %d",&m,&x);
    21             while(m--&&q->next)//这个while循环的作用是确定题目中给的目标位置 
    22             {
    23                 q=q->next;
    24             }
    25             p=new tree;//确定了之后开始用P游动指针来建立链表 
    26             p->data=x;
    27             p->next=NULL;
    28             p->next=q->next;
    29             q->next=p;
    30             n--;
    31         }
    32         p=head->next;//最后进行链表的输出 
    33         while(p)
    34         {
    35             if(p->next==NULL)
    36             {
    37                 printf("%d
    ",p->data);
    38             }
    39             else
    40             {
    41                 printf("%d ",p->data);
    42             }
    43             p=p->next;
    44         }
    45     }
    46     
    47     return 0;
    48 }
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 struct node
     4 {
     5     int data;
     6     struct node *next;
     7 };
     8 int main()
     9 {
    10     int n,m,x;
    11     struct node *head,*p,*q;
    12     while(~scanf("%d",&n))
    13     {
    14         head=(struct node *)malloc(sizeof(struct node));
    15         head->next=NULL;
    16         while(n--)
    17         {
    18             q=head;
    19             scanf("%d %d",&m,&x);
    20             while(m--&&q->next)
    21                 q=q->next;
    22             p=(struct node *)malloc(sizeof(struct node));
    23             p->data=x;
    24             p->next=NULL;
    25             p->next=q->next;
    26             q->next=p;
    27         }
    28         q=head->next;
    29         while(q)
    30         {
    31             if(q->next==NULL) printf("%d
    ",q->data);
    32             else printf("%d ",q->data);
    33             q=q->next;
    34         }
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    【微信开发】【Asp.net MVC】-- 微信分享功能
    利用JS-SDK微信分享接口调用(后端.NET)
    C# ThreadPool类(线程池)
    C#多线程--线程池(ThreadPool)
    MongoDB允许其它IP地址访问
    解决ASP.Net第一次访问慢的处理(IIS8)
    GitLab版本管理
    基于句子嵌入的无监督文本摘要(附代码实现)zt
    简约机器学习复习笔记/速查手册(缺点是2018年1月的旧了)
    CRF学习的文章
  • 原文地址:https://www.cnblogs.com/xiaolitongxueyaoshangjin/p/12063605.html
Copyright © 2011-2022 走看看