带头结点的链表的基本操作

构建带头结点的链表

#include <bits/stdc++.h>
using namespace std;
typedef struct Node{
    struct Node *next;
    int val;
    Node(){
        next = NULL;
    }
}*Link;
Link insert(Link p,int key){
    Link t = new Node();
    t->val = key;
    p->next = t;
    p = p->next;
    return p;
}
void print(Link h){
    Link p = h->next;
    while(p){
        printf("%d\n",p->val);
        p = p->next;
    }
}
int main(){
    Link h1 = new Node();
    Link h2 = new Node();   //带头节点
    int x;
    Link p1 = h1;
    Link p2 = h2;
    while(~scanf("%d",&x) && x != -1)   p1 = insert(p1,x);
    while(~scanf("%d",&x) && x != -1)   p2 = insert(p2,x);
    return 0;
}

合并

void merge(Link h3,Link h1,Link h2){
    Link p1 = h1->next;
    Link p2 = h2->next;
    Link p = h3;
    while(p1 && p2){
        if(p1->val < p2->val)   p->next = p1,   p1 = p1->next;
        else    p->next = p2,   p2 = p2->next;
        p = p->next;
    }
    while(p1)   p->next = p1,   p1 = p1->next,  p = p->next;
    while(p2)   p->next = p2,   p2 = p2->next,  p = p->next;    
}

交集

void intersection(Link h3,Link h1,Link h2){
    Link p1 = h1->next;
    Link p2 = h2->next;
    Link p = h3;
    while(p1 && p2){
        if(p1->val < p2->val)    p1 = p1->next;
        else if(p2->val < p1->val)    p2 = p2->next;
        else if(p1->val == p2->val){
            p->next = p1;
            p = p->next;
            p1 = p1->next;
            p2 = p2->next;
        }
    }
}

反转链表

Link reverse(Link head){
    Link nh = new Node();
    Link np = nh;
    while(head->next){
        Link t = head; //后一个结点
        Link p = t->next;    //当前结点
        while(p->next){
            p = p->next;
            t = t->next;
        }
        np->next = p;
        np = np->next;
        t->next = NULL;
    }
    np = nh->next;
}

循环链表(带头结点的双向链表)

POJ3750(约瑟夫问题)

#include <iostream>
using namespace std;
int n,w,s;
typedef struct Node{
    struct Node *next,*pre;
    string name;
    Node(){next = pre = NULL;}
}*Link;
Link insert(Link head,Link tail,string s){
    Link np = new Node();
    np->name = s;
    np->pre = tail;
    np->next = head;
    tail->next = np;
    tail = tail->next;
    return tail;
}
void solve(Link h,int st,int cir){
    int count = 0;
    Link p = h;
    do{
        p = p->next;
        count++;
        if(count == st) break;
    }while(true);
    count = 1;
    int all = 0;
    while(all != n){
        if(count == cir){   //先判断count,因为count可能为1
            cout<<p->name<<endl;
            all++;
            p->pre->next = p->next; //删除p结点
            count = 0;
        }
        count++;
        p = p->next;
        if(p == h)  p = p->next;    //如果是头结点不算
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    Link h = new Node();
    h->pre = h->next = h;
    Link tail = h;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        tail = insert(h,tail,s);    //尾指针,减少搜索
    }
    cin>>w;
    cin.ignore();
    cin>>s;
    solve(h,w,s);
    return 0;
}

 

posted @ 2020-05-05 15:34  月光下の魔术师  阅读(50)  评论(0)    收藏  举报