链表笔记

/*
STL的list:双向链表。它的内存空间不必连续,通过指针来进行数的访问,高效率地在任意地方删除和插入,
插入和删除操作是常数时间。
list和vector的优缺点正好相反,它们的应用场景不同:
    (1)vector:插入和删除操作少,随机访问元素频繁;
    (2)list:插入和删除频繁,随机访问较少。
*/

/*D - 例4
Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:
从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三
的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数...,以后从头开始轮流进行一至二报数、
一至三报数直到剩下的人数不超过三人为止。   //注意报数是按一至二全部报完一遍再从头一至三全部报完一遍...
Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。 
Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。 
Sample Input
2
20
40
Sample Output
1 7 19
1 19 37
*/

//法一、用STL实现
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <list>
using namespace std;
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        int k;
        k=2;
        list<int >blist;
        list<int >::iterator it;   //定义链表迭代器
        for(int i=1;i<=n;i++){
            blist.push_back(i);
        }
        while(blist.size()>3){
            int num;
            num=1;
            for(it=blist.begin();it!=blist.end();){
                if(num++%k==0){
                    it=blist.erase(it);
                }else{
                    it++;
                }
            }
            if(k==2){
                k=3;
                continue;
            }else{
                k=2;
                continue;
            }
            //k==2?k=3:k=2;
        }
        for(it=blist.begin();it!=blist.end();it++){
            if(it!=blist.begin()){
                cout<<" ";
            }
            cout<<*it;
        }
        cout<<endl;
    }
    return 0;
}


//法二、用list实现
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <list>
using namespace std;
typedef struct _node{
    int value;
    struct _node *next;
} Node;
typedef struct _list{
    Node* head;
}List;
void addl(List *pList,int number){
    Node *p=(Node*)malloc(sizeof(Node));
    p->value=number;
    p->next=NULL;
    Node *last=pList->head;
    if(last){
        while(last->next){
            last=last->next;
        }
        last->next=p;
    }else{
        pList->head=p;
    }
}
int list_size(List *pList){
    int _size;
    _size=0;
    Node *p;
    for(p=pList->head;p;p=p->next){
        _size++;
    }
    return _size;
}
void print(List *pList){
    Node *p;
    for(p=pList->head;p;p=p->next){
        if(p!=pList->head){
            printf(" ");
        }
    printf("%d",p->value);
    }
    printf("\n");
}
int main(){
    int t,n;
    cin>>t;
    while(t--){
        cin>>n;
        int k;
        k=2;
        List blist;
        blist.head=NULL;
        for(int i=1;i<=n;i++){
            addl(&blist,i);
        }
        while(list_size(&blist)>3){
            int num;
            num=1;
            Node *q;
            q=NULL;
            for(Node *p=blist.head;p;q=p,p=p->next){
                if(num++%k==0){
                    if(q){
                        q->next=p->next;
                    }else{
                        blist.head=p->next;
                    }
                    free(p);
                }
            }
            if(k==2){
                k=3;
                continue;
            }else{
                k=2;
                continue;
            }
        }
        print(&blist);
    }
    return 0;
}
发布了7 篇原创文章 · 获赞 1 · 访问量 110

猜你喜欢

转载自blog.csdn.net/qq_45599068/article/details/104083046