sdut pta 链表3(优化)-----7-3 sdut-C语言实验-链表的结点插入

简介: sdut pta 链表3(优化)-----7-3 sdut-C语言实验-链表的结点插入

7-3 sdut-C语言实验-链表的结点插入


分数 20


全屏浏览


切换布局


作者 马新娟


单位 山东理工大学


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


输入格式:

多组输入。每组数据首先输入一个整数n(n∈[1,100]),代表有n次操作。

接下来的n行,每行有两个整数Mi(Mi∈[0,10000]),Xi。

输出格式:

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


输入样例:

1. 4
2. 1 1
3. 1 2
4. 0 3
5. 100 4

输出样例:

3 1 2 4


代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next;
};//定义结构体类型;
int main()
{
    struct node *head,*p;
    int n,i;
    int x;
    while(~scanf("%d",&n))
    {
        head = (struct node*)malloc(sizeof(struct node));
        //要多组输入,每一组输入都要重新开辟一个头结点;
        head -> next = NULL;
        for(i=0; i<n; i++)
        {
 
            p = (struct node*)malloc(sizeof(struct node));
            p -> next = NULL;
            scanf("%d%d",&x,&p -> a);
            struct node *q = head -> next;
            struct node *qi = head;//定义两个游动指针
            while(q&&x--)//寻找插入的位置;
            {
                q = q -> next;
                qi = qi -> next;
            }
            qi -> next = p;
            p -> next = q;//插入节点;
 
        }
        struct node *p;
    p = head -> next;
    while(p)
    {
        if(p -> next)
        {
            printf("%d ",p -> a);
        }
        else printf("%d\n",p -> a);
        p = p -> next;
    }
    }
 
    return 0;
}


#include <stdio.h> #include <stdlib.h>


这两行代码包含了标准的输入输出库和标准库,后者提供了动态内存分配的功能。


struct node { int a; struct node *next; };//定义结构体类型;


定义了一个名为node的结构体,它包含一个整型数据a和一个指向同类型结构体的指针next。这个结构体用于表示链表中的每个节点。


int main() { // ... }


定义了main函数,程序的执行从这里开始。


struct node *head, *p; int n, i; int x;


声明了头节点指针head,新节点指针p,循环计数器n和i,以及用于临时存储输入值的x。


while(~scanf("%d",&n)) { // ... }


使用while循环读取每组输入的整数数量n,直到输入失败(例如文件结束EOF)。~scanf是一种技巧,用于检查scanf是否成功读取了输入。


head = (struct node*)malloc(sizeof(struct node)); head->next = NULL;


为链表的头节点分配内存,并初始化头节点的next指针为NULL。


for(i = 0; i < n; i++) { // ... }


循环n次,每次读取一对整数。


p = (struct node*)malloc(sizeof(struct node)); scanf("%d%d", &x, &p->a);


为新节点分配内存,并读取一个整数到x,然后将p->a设置为读取的值。


struct node *q = head->next; struct node *qi = head; while(q && x--) { // ... }


这段代码试图通过x--来寻找插入新节点的位置,


qi->next = p; p->next = q;


将新节点p插入到链表中qi节点的后面。


p = head->next; while(p) { // ... }


遍历链表并打印所有节点的数据。


return 0; }


main函数返回0,表示程序正常结束。


目录
相关文章
|
2天前
链表5(考试用)7-5 sdut-C语言实验-链表的逆置
链表5(考试用)7-5 sdut-C语言实验-链表的逆置
6 0
|
2天前
链表8(法二考试专用)7-8 sdut-C语言实验-双向链表
链表8(法二考试专用)7-8 sdut-C语言实验-双向链表
4 0
|
2天前
链表9(优化版)7-9 sdut-C语言实验-约瑟夫问题
链表9(优化版)7-9 sdut-C语言实验-约瑟夫问题
6 0
|
2天前
链表4(法二)------7-4 sdut-C语言实验-单链表中重复元素的删除
链表4(法二)------7-4 sdut-C语言实验-单链表中重复元素的删除
4 0
|
2月前
【移除链表元素】LeetCode第203题讲解
【移除链表元素】LeetCode第203题讲解
|
26天前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
26天前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
|
1月前
|
存储 算法 Java
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
【经典算法】Leetcode 141. 环形链表(Java/C/Python3实现含注释说明,Easy)
11 2
|
2月前
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点.
<数据结构>五道LeetCode链表题分析.环形链表,反转链表,合并链表,找中间节点
28 1
|
23天前
|
算法
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表
【经典LeetCode算法题目专栏分类】【第7期】快慢指针与链表