zoukankan      html  css  js  c++  java
  • 循环双链表基本操作

    #include<iostream>
    using namespace std;
    typedef char ElemType;
    typedef struct DNode
    {
        ElemType data;
        struct DNode * prior;
        struct DNode * next;
    }DLinkNode;
    
    void CreateListF(DLinkNode *&L,ElemType a[],int n)//头插法建立
    {
        DLinkNode *s;
        L = (DLinkNode*)malloc(sizeof(DLinkNode));//创建头结点
        L->next = NULL;
        for(int i = 0;i<n;i++)
        {
            s = (DLinkNode*)malloc(sizeof(DLinkNode));
            s->data = a[i];
            s->next = L->next;
            if( L->next != NULL )//s插入原始结点之前,头结点之后
                L->next->prior = s;
            L->next = s;
            s->prior = L;
        }
        s = L->next;
        while(s->next != NULL)
        {
            s = s->next;
        }//s指向尾结点
        s->next = L;
        L->prior =  s;
    }
    
    void CreateListR(DLinkNode *&L,ElemType a[],int n )//尾插法建立
    {
        DLinkNode *s;
        DLinkNode *r;
        L = (DLinkNode*)malloc(sizeof(DLinkNode));
        L->next = NULL;
        r = L;//r始终指向尾结点
        for(int i = 0;i<n;i++)
        {
            s = (DLinkNode*)malloc(sizeof(DLinkNode));
            s->data = a[i];
            r->next = s;
            s->prior = r;
            r = s;
        }
        r->next = L;
        L->prior = r; 
    }
    
    void InitList(DLinkNode *&L)
    {
        L = (DLinkNode*)malloc(sizeof(DLinkNode));
        L->next = L->prior = L;
    }
    
    void DestoryList(DLinkNode *&L)
    {
        DLinkNode *pre = L;
        DLinkNode *p = L->next;
        while(p != L)
        {
            free(pre);
            pre = p;
            p = p->next;//pre /p 同步后移
        }
        free(pre);
    }
    
    bool ListEmpty(DLinkNode *L)
    {
        return (L->next == L);
    }
    
    int ListLength(DLinkNode *L)
    {
        int j = 0;
        DLinkNode *p = L;
        while(p->next != L)
        {
            j++;
            p = p->next;
        }
        return (j);
    }
    
    void DispList(DLinkNode *L)
    {
        DLinkNode * p = L->next;
        while(p != L)
        {
            cout<<" "<<p->data;
            p = p->next;
        }
        cout<<endl;
    }
    
    bool GetElem(DLinkNode *L,int i ,ElemType &e)
    {
        int j = 1;
        DLinkNode *p = L->next;
        if(i <= 0  || L->next == L)
        {
            return false;
        }
        if(i == 1)//i == 1 特殊情况
        {
            e = p->next->data;
            return true;
        }
        else    
        {
            while( j < i && p != L)
            {
                j ++;
                p = p->next;
            }
            if(p == L)//没有找到
            {
                return false;
            }
            else//找到第i个
            {
                e = p->data;
                return true;
            }
        }
    }
    
    int LocateElem(DLinkNode *L,ElemType e)
    {
        DLinkNode * p = L->next;
        int j = 1;
        while( p != NULL && p->data != e)
        {
            j++;
            p = p->next;
        }
        if(p == NULL)//不存在,返回false
        {
            return false;
        }
        else
        {
            return (j);
        }
    }
    
    bool ListInsert(DLinkNode *&L,int i ,ElemType e)//插入第i个元素
    {   
        int j = 1;
        DLinkNode *p = L;
        DLinkNode *s;
        if(i<0)
        {
            return false;
        }
        if(p->next == L)//若链表为空时
        {
            s = (DLinkNode*)malloc(sizeof(DLinkNode));
            s->data = e;
            p->next = s;
            s->next = p;
            p->prior = s;
            s->prior = p;
            return true;
        }
        else if(i == 1)//L不为空 i == 1
        {
            s = (DLinkNode *)malloc(sizeof(DLinkNode));
            s->data = e;
            s->next = p->next;
            p->next = s;
            s->next->prior = s;//结点s插入结点p之后
            s->prior->next = p;
            return true;
        }
        else//i !=1 
        {
            p = L->next;
            while(j < i-1 && p != L)
            {
                j++;
                p = p->next;
            }
            if(p == L)
            {
                return false;
            }
            else
            {
                s = (DLinkNode*)malloc(sizeof(DLinkNode));
                s->data = e;
                s->next = p->next;
                if(p->next != NULL)
                {
                    p->next->prior = s;
                }
                s->prior = p;
                p->next = s;
                return true;
            }
        }
    }
    
    bool ListDelete(DLinkNode *&L,int i ,ElemType &e)
    {
        DLinkNode *p = L;
        DLinkNode *q;
        int  j = 1;
        if(i <=0 || L->next == L)
        {
            return false;
        }
        if(i == 1)//i == 1 特殊情况处理
        {
            q = L->next;
            e = q->data;
            L->next = q->next;
            q->next->prior = L;
            free(q);
            return true;
        }
        else
        {
            p = L->next;
            while(j < i-1 && p != NULL)
            {
                j++;
                p = p->next;            
            }
            if(p == NULL)//未找到i-1 个结点
            {
                return false;
            }
            else//找到i-1 个结点
            {
                q = p->next;//q指向待删除结点
                if(q == NULL)//不存在要删除的结点
                {
                    return 0;
                }
                e = q->data;//删除q
                p->next = q->next;
                if(p->next != NULL)
                    p->next->prior = p;
                free(q);//释放q
                return true;
            }
        }
    }
    
    int main()
    {
        DLinkNode *L;
        ElemType e;
        cout<<"循环双链表的基本运算"<<endl;
        InitList(L);
        ListInsert(L,1,'a');
        ListInsert(L,2,'b');
        ListInsert(L,3,'c');
        ListInsert(L,4,'d');
        ListInsert(L,5,'e');
        cout<<"Disp"<<endl;
        DispList(L);
        cout<<"Length is :"<<ListLength(L)<<endl;
        GetElem(L,3,e);
        cout<<"Index 3 is :"<<e<<endl;
        ListDelete(L,4,e);
        cout<<"Delete Index 4"<<endl;
        DispList(L);
        cout<<"Destory"<<endl;
        DestoryList(L);
        return 0;
    }
    

      运算结果

  • 相关阅读:
    线性同余方程模板( A+C*x=B(mod D) )
    线性同余方程组模板( x=r0(mod m0) )
    poj2891(线性同余方程组)
    BestCoder Round #63 (div.2)
    16.实现多个具有相同方法的接口和父类与实现接口有相同方法
    14.Java中的StringTokenizer类的使用方法
    13.Java为什么不支持多继承
    bootstrap使用基础
    Js 百分比进度条
    Js更改样式导致hover效果消失
  • 原文地址:https://www.cnblogs.com/ygsworld/p/10029321.html
Copyright © 2011-2022 走看看