链表习题集

        一一有亿~~~点长

题目难度有点乱,不会先做下一道

1、从尾到头打印双向循环链表

输入样例:
5
1 3 5 7 9
输出样例:
9 7 5 3 1

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node *prev,*next;
};
int main(){
	int n,x;
	cin>>n>>x;
	node *now,*head,*p;
	head=new node;
	head->data=x;
	head->next=NULL;
	head->prev=NULL;
	now=head;
	for(int i=2;i<=n;i++){
		cin>>x;
		p=new node;
		p->data=x;
		p->next=NULL; 
		p->prev=now;
		now->next=p;
		now=p;
	}
	now->next=head;
	head->prev=now; 
	//链表构造完毕
	for(int i=1;i<=n;i++){
		cout<<(now->data)<<" ";
		now=now->prev;
	} //输出 
	return 0;
}

2、打印链表的倒数k个节点

输入样例:
5 2
1 3 5 7 9
输出样例:
7

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node *prev,*next;
};
int main(){
	int n,m,x;
	cin>>n>>m>>x;
	node *now,*head,*p;
	head=new node;
	head->data=x;
	head->next=NULL;
	head->prev=NULL;
	now=head;
	for(int i=2;i<=n;i++){
		cin>>x;
		p=new node;
		p->data=x;
		p->next=NULL; 
		p->prev=now;
		now->next=p;
		now=p;
	}
	now->next=head;
	head->prev=now; 
	//链表构造完毕
	for(int i=1;i<m;i++){
		now=now->prev; 
	} //移到指定节点 
	cout<<(now->data);
	//输出 
	return 0;
}

3、反转链表(单向链表反转)

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
输入样例:
5
1 3 5 7 9
输出样例:
9 7 5 3 1

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node*next;
};
int main(){
	int n,x;
	cin>>n>>x;
	node  *now,*head,*p;
	head=new node;
	head->data=x;
	head->next=NULL;
	now=head;
	for(int i=1;i<n;i++){
		cin>>x;
		p=new node;
		p->data=x;
		p->next=NULL; 
		now->next=p;
		now=p;
	}
	//链表构造完毕
	node *fan;
	fan=now;
	//fan:反转后的头节点
	
	for(int i=1;i<n;i++){
        p=head;
        for(int j=1;j<=n-i-1;j++)
            p=p->next;
        now->next=p;
        now=now->next;
    }

    // 输出链表中的每个节点的数据
    now=fan;
    for(int i=1;i<=n;i++){
        cout<<now->data<<' ';
        now=now->next;
    }
	return 0;
}

4、有序链表的合并

输入样例:
3 3
1 2 4
1 3 4
输出样例:
1 1 2 3 4 4

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node *next;
};
int main(){
	int n1,n2;
	cin>>n1>>n2;
	node *head,*now,*head1,*now1,*head2,*now2,*p; 
	//建第一个链表 
	int x;
	cin>>x;
	head1=new node;
	head1->data=x;
	head1->next=NULL;
	now=head1;
	for(int i=1;i<n1;i++){
		p=new node;
		cin>>x;
		p->data=x;
		p->next=NULL;
		now->next=p;
		now=now->next;
	}
	// 建第二个链表 
	cin>>x;
	head2=new node;
	head2->data=x;
	head2->next=NULL;
	now=head2;
	for(int i=1;i<n2;i++){
		p=new node;
		cin>>x;
		p->data=x;
		p->next=NULL;
		now->next=p;
		now=now->next;
	}
	now1=head1;now2=head2;
	head=new node;
	head->next=NULL;
	now=head;
	while(now1!=NULL && now2!=NULL){//不断进行比较插入,直至一个链表为空 
		if((now1->data)<(now2->data)){
			now->next=now1;
			now1=now1->next;
		}
		else{
			now->next=now2;
			now2=now2->next;
		}
		now=now->next;
	}
	//  合并 
	now->next=(now1==NULL)?now2:now1;//偷个懒,三目运算表达式,当now1为空时将now2挂到now后面,否则将now1挂到now后面
	now=head->next;
	while(now!=NULL){
		cout<<now->data<<' ';
		now=now->next;
	}
	//  打印 
	return 0;
}

5、复杂链表的复制

在这里插入图片描述
在这里插入图片描述

6、两个链表的第一个公共节点

输入样例:
3 3
1 2 4
4 3 2
输出样例:
2

7、删除链表中的节点

输入样例:
5 2
1 3 5 7 9
输出样例:
1 5 7 9

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node *prev,*next;
};
int main(){
	int n,m,x;
	cin>>n>>m>>x;
	node *now,*head,*p;
	head=new node;
	head->data=x;
	head->next=NULL;
	head->prev=NULL;
	now=head;
	for(int i=2;i<m;i++){
		cin>>x;
		p=new node;
		p->data=x;
		p->next=NULL; 
		p->prev=now;
		now->next=p;
		now=p;
	}
	cin>>x;
	//直接跳过需要删除的节点,然后直接输出就行了 
	for(int i=m+1;i<=n;i++){
		cin>>x;
		p=new node;
		p->data=x;
		p->next=NULL; 
		p->prev=now;
		now->next=p;
		now=p;
	}
	now->next=head;
	head->prev=now; 
	now=head;
	//链表构造完毕
	for(int i=1;i<n;i++){
		cout<<(now->data)<<" ";
		now=now->next;
	} //输出 
	return 0;
}

8、队列安排(洛谷:P1160)

题目描述

一个学校里老师要将班上N个同学排成一列,同学被编号为1∼N,他采取如下的方法:

  1. 先将1号同学安排进队列,这时队列中只有他一个人;

  2. 2∼N号同学依次入列,编号为i的同学入列方式为:老师指定编号为i的同学站在编号1∼(i−1)中某位同学(即之前已经入列的同学)的左边或右边;

  3. 从队列中去掉 M 个同学,其他同学位置顺序不变。

在所有同学按照上述方法队列排列完毕后,老师想知道从左到右所有同学的编号。

输入格式

第一行一个整数N,表示了有N个同学。

第2∼N行,第 ii 行包含两个整数k,p,其中k为小于i的正整数,p为0或者1。若p为0,则表示将i号同学插入到k号同学的左边,p为1则表示插入到右边。

第N+1行为一个整数M,表示去掉的同学数目。

接下来M行,每行一个正整数x,表示将x号同学从队列中移去,如果x号同学已经不在队列中则忽略这一条指令。

输出格式

一行,包含最多N个空格隔开的整数,表示了队列从左到右所有同学的编号。

输入输出样例

输入

4
1 0
2 1
1 0
2
3
3

输出

2 4 1

说明/提示

【数据范围】

对于 20% 的数据,1≤N≤10。

对于 40% 的数据,1≤N≤1000。

对于 100% 的数据,1<M≤N≤105。

#include<cstdio>
#include<cstring>
int a[100010][3],n,m;
//a[i][2]表示学号为i的同学右边同学的学号
//a[i][3]表示学号为i的同学左边同学的学号
int main()
{
    scanf("%d",&n); int j=1;
    memset(a,0,sizeof(a)); a[1][1]=1;
    for(int i=2;i<=n;i++)
    {
        int x,y; scanf("%d %d",&x,&y);
        a[i][1]=i;
        if(y==0)
        //插入左边
        { 
            a[a[x][3]][2]=i; a[i][2]=x;
            a[i][3]=a[x][3]; a[x][3]=i;
            if(x==j) j=i;
            //比较麻烦,要改链表
        }
        else
        //插入右边
        {
            a[i][2]=a[x][2]; a[a[x][2]][3]=i;
            a[x][2]=i; a[i][3]=x;
        }
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int x; scanf("%d",&x);
        if(a[x][1]!=0)
        //该同学还在 
        {
            a[x][1]=0;
            //踢掉……
            a[a[x][3]][2]=a[x][2];
            a[a[x][2]][3]=a[x][3];
            n--;
            if(x==j) j=a[x][3];
        }
    }
    int i=1,x=j;
    while(i<=n)
    {
        printf("%d ",a[x][1]);
        x=a[x][2]; i++;
    }//完美结束 
    return 0;
}

9、求链表的平均值

输入样例:
5
1 3 5 7 9
输出样例:
5

#include<bits/stdc++.h>
using namespace std;
struct node{
	int data;
	node *prev,*next;
};
int main(){
	int n,x;
	cin>>n>>x;
	node *now,*head,*p;
	head=new node;
	head->data=x;
	head->next=NULL;
	head->prev=NULL;
	now=head;
	for(int i=2;i<=n;i++){
		cin>>x;
		p=new node;
		p->data=x;
		p->next=NULL; 
		p->prev=now;
		now->next=p;
		now=p;
	}
	now->next=head;
	head->prev=now; 
	//链表构造完毕
	int ans;
	for(int i=1;i<=n;i++){
		ans+=(now->data);
		now=now->next;
	}//求和 
	cout<<ans/n;
	//暴力但简单 
	return 0;
}

  • 36
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值