zoukankan      html  css  js  c++  java
  • C++双向循环链表

    DuLink:

    #include<iostream>
    using namespace std;
    typedef int elemType;
    
    typedef struct DulNode
    {
        elemType data;
        DulNode *prior;
        DulNode *next;
    }*DuList;
    
    void InitList(DuList *L)
    {
        *L = new DulNode;
        (*L)->next = (*L)->prior = *L;
    }
    
    int LengthList(DuList *L)//获取表长
    {
        DuList p = (*L)->next;
        int length = 0;
        while((p!=NULL) && (p!=*L))
        {
            p = p->next;
            length++;
        }
        return length;
    }
    
    //获取第pos个位置的元素,若正确获取返回1,否则返回0
    int GetElem(DuList *L, int pos, elemType *x)
    {
        DuList p = (*L)->next;
        int i=1;
        while(p!=(*L) && i<pos)
        {
            p=p->next;
            i++;
        }
        if(p==*L || i>pos)
        {
            cout<<"get position error"<<endl;
            return 0;
        }
        *x = p->data;
        return 1;
    }
    
    //在带头结点的双向循环链表的第pos个位置前插入元素e
    bool InsertList(DuList *L, int pos, elemType e)
    {
        DuList p,q;
        int i=1;
        p = (*L)->next;
        while(p!=*L && i<pos-1)
        {
            p = p->next;
            i++;
        }
        if((p==*L|| i>pos-1) && ((*L)->next!=(*L)->prior))
            return false;
        q = new DulNode;
        q->data = e;
    
        q->prior = p;
        q->next = p->next;
        p->next->prior = q;
        p->next = q;
    }
    
    //删除第pos个位置的元素
    bool DeleteList(DuList *L, int pos)
    {
        DuList p,q;
        int i=1;
        p = (*L)->next;
        while(p!=*L && i<pos)
        {
            p = p->next;
            i++;
        }
        if((p==*L|| i>pos) && ((*L)->next!=(*L)->prior))
            return false;
    
        p->prior->next = p->next;
        p->next->prior = p->prior;
    }
    
    void PrintList(DuList *L)
    {
        DuList p;
        int i=1;
        p = (*L)->next;
        while(p!=*L)
        {
            cout<< i <<":	"<< p->data << endl;
            p = p->next;
            i++;
        }
    }
    
    int main()
    {
        DuList L;
        InitList(&L);
        int length;
        for(int i=1;i<6;i++)
            InsertList(&L,i,i);
        cout<<"the length of Dulist is: "<< LengthList(&L) <<endl;
        PrintList(&L);
        cout<<endl;
    
        int num;
        GetElem(&L, 2, &num);
        cout<<"the 2th item of list is: "<< num <<endl;
    
        DeleteList(&L, 3);
        cout<<"after delete the 3th item:"<<endl;
        PrintList(&L);
        cout<<endl;
    
        return 0;
    }
  • 相关阅读:
    bzoj1066: [SCOI2007]蜥蜴(最大流)
    bzoj4551: [Tjoi2016&Heoi2016]树(树链剖分)
    bzoj2663: [Beijing wc2012]灵魂宝石(二分+匈牙利)
    bzoj2150: 部落战争(匈牙利)
    bzoj1797: [Ahoi2009]Mincut 最小割(最小割+强联通tarjan)
    bzoj3993: [SDOI2015]星际战争(网络流)
    bzoj3504: [Cqoi2014]危桥(网络流)
    bzoj3212: Pku3468 A Simple Problem with Integers(线段树)
    bzoj4590: [Shoi2015]自动刷题机(二分答案)
    [WC2013]糖果公园
  • 原文地址:https://www.cnblogs.com/jx-yangbo/p/4857535.html
Copyright © 2011-2022 走看看