2054数据结构实验之链表九:双向链表

本文分享了一段使用C++编程语言处理列表数据的代码实例,重点介绍了如何利用标准模板库(STL)中的list容器进行元素查找、遍历及输出前后相邻元素的操作。代码中包含了详细的注释,有助于读者理解实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题地址
记录一下自己通过查百度而写出来的代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iomanip>
#include <string.h>
#include <list>
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
/*struct shi
{
    int x;
    int T;
} s[100000];
int cmp(shi a,shi b)
{
    return a.x<b.x;
}
long long cmd(long long a, long long  b)
{
    return a > b;
}*/
//int m , n,sum,k;
/*
typedef struct node {
    int num;
    struct node *next;

}student;
student * creatlist(int b){
    student *head = new student;
    student *pre = head;
    for(int i=1;i<=b;i++){
        student *p = new student ;
        p->num = i;
        pre->next = p;
        pre = p;
        p->next = NULL;
    }
    pre->next = head->next;
    return head;

};
void kill(struct node *head , int n){
    while(head->next!=head){
        for(int i=1;i<=n-1;i++){
            head = head->next;
        }
        head->next = head->next->next;
    }
    cout<<head->num<<endl;
}*/
int main()
{
    int m , n;
    cin >>m >>n;
    list<int> lis;
    for(int i=1;i<=m;i++){
        int j;
        cin >>j;
        lis.push_back(j);
    }
    /*for(auto it= lis.begin();it != lis.end();it++)
        cout<<*it<<endl;*/
    for(int i=0;i<n;i++){
        int j,falg=1;
        list<int>::iterator  it=lis.begin();
        cin >>j;
        it = find(lis.begin(),lis.end(),j);
        if(it==lis.begin()){
            falg=0;
            cout<<" ";
        }
        else{
            advance(it , -1);//主要是这里记录一下
        if(it!=lis.begin())
            cout<<*it;
        }
        cout<<" ";
        it = find(lis.begin(),lis.end(),j);
        advance(it, 1);
        if(it!=lis.end())
            cout<<*it;
        cout<<endl;


    }
    /*
    node *head = creatlist(m);
    kill(head,n);*/
    return 0;

}


/***************************************************
User name: gpytql
Result: Accepted
Take time: 0ms
Take Memory: 196KB
Submit time: 2019-08-08 09:13:03
****************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值