zoukankan      html  css  js  c++  java
  • 挂个数据结构代码

    //循环单链表基本运算算法
    #include <stdio.h>
    #include <malloc.h>
    typedef int ElemType;
    typedef struct LNode        //定义单链表结点类型
    {
        ElemType data;
        struct LNode *next;
    } LinkNode;
    void CreateListF(LinkNode *&L,ElemType a[],int n)
    //头插法建立循环单链表
    {
        LinkNode *s;int i;
        L=(LinkNode *)malloc(sizeof(LinkNode));      //创建头结点
        L->next=NULL;
        for (i=0;i<n;i++)
        {    
            s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点
            s->data=a[i];
            s->next=L->next;            //将结点s插在原开始结点之前,头结点之后
            L->next=s;
        }
        s=L->next;    
        while (s->next!=NULL)            //查找尾结点,由s指向它
            s=s->next;
        s->next=L;                        //尾结点next域指向头结点
    
    }
    void CreateListR(LinkNode *&L,ElemType a[],int n)
    //尾插法建立循环单链表
    {
        LinkNode *s,*r;int i;
        L=(LinkNode *)malloc(sizeof(LinkNode));      //创建头结点
        L->next=NULL;
        r=L;                    //r始终指向终端结点,开始时指向头结点
        for (i=0;i<n;i++)
        {    
            s=(LinkNode *)malloc(sizeof(LinkNode));//创建新结点
            s->data=a[i];
            r->next=s;            //将结点s插入结点r之后
            r=s;
        }
        r->next=L;                //尾结点next域指向头结点
    }
    void InitList(LinkNode *&L)        //初始化线性表
    {
        L=(LinkNode *)malloc(sizeof(LinkNode));    //创建头结点
        L->next=L;
    }
    void DestroyList(LinkNode *&L)         //销毁线性表
    {
        LinkNode *p=L,*q=p->next;
        while (q!=L)
        {
            free(p);
            p=q;
            q=p->next;
        }
        free(p);
    }
    bool ListEmpty(LinkNode *L)         //判断线性表是否为空表
    {
        return(L->next==L);
    }
    int ListLength(LinkNode *L)         //求线性表的长度
    {
        LinkNode *p=L;int i=0;
        while (p->next!=L)
        {
            i++;
            p=p->next;
        }
        return(i);
    }
    void DispList(LinkNode *L)        //输出线性表
    {
        LinkNode *p=L->next;
        while (p!=L)
        {
            printf("%d ",p->data);
            p=p->next;
        }
        printf("
    ");
    }
    bool GetElem(LinkNode *L,int i,ElemType &e)  //求线性表中第i个元素值
    {
        int j=0;
        LinkNode *p;
        if (L->next!=L)        //单链表不为空表时
        {
            if (i==1)
            {
                e=L->next->data;
                return true;
            }
            else            //i不为1时
            {
                p=L->next;
                while (j<i-1 && p!=L)
                {
                    j++;
                    p=p->next;
                }
                if (p==L)
                    return false;
                else
                {
                    e=p->data;
                    return true;
                }
            }
        }
        else                //单链表为空表时
            return false;
    }
    int LocateElem(LinkNode *L,ElemType e)   //查找第一个元素为e的元素符号
    {
        LinkNode *p=L->next;
        int n=1;
        while (p!=L && p->data!=e)
        {
            p=p->next;
            n++;
        }
        if (p==L)
            return(0);
        else
            return(n);
    }
    bool ListInsert(LinkNode *&L,int i,ElemType e)  //插入第i个元素
    {
        int j=0;
        LinkNode *p=L,*s;
        if (p->next==L || i==1)        //原单链表为空表或i==1时
        {
            s=(LinkNode *)malloc(sizeof(LinkNode));    //创建新结点s
            s->data=e;                                
            s->next=p->next;        //将结点s插入到结点p之后
            p->next=s;
            return true;
        }
        else
        {
            p=L->next;
            while (j<i-2 && p!=L)
            {
                j++;
                p=p->next;
            }
            if (p==L)                //未找到第i-1个结点
                return false;
            else                    //找到第i-1个结点p
            {
                s=(LinkNode *)malloc(sizeof(LinkNode));    //创建新结点s
                s->data=e;                                
                s->next=p->next;                        //将结点s插入到结点p之后
                p->next=s;
                return true;
            }
        }
    }
    bool ListDelete(LinkNode *&L,int i,ElemType &e)//删除第i个元素
    {
        int j=0;
        LinkNode *p=L,*q;
        if (p->next!=L)                    //原单链表不为空表时
        {
            if (i==1)                    //i==1时
            {
                q=L->next;                //删除第1个结点
                e=q->data;
                L->next=q->next;
                free(q);
                return true;
            }
            else                        //i不为1时
            {
                p=L->next;
                while (j<i-2 && p!=L)
                {
                    j++;
                    p=p->next;
                }
                if (p==L)                //未找到第i-1个结点
                    return false;
                else                    //找到第i-1个结点p
                {
                    q=p->next;            //q指向要删除的结点
                    e=q->data;
                    p->next=q->next;    //从单链表中删除q结点
                    free(q);            //释放q结点
                    return true;
                }
            }
        }
        else return false;
    }
    实验4
    //循环双链表基本运算算法
    #include <stdio.h>
    #include <malloc.h>
    typedef int ElemType;
    typedef struct DNode        //定义双链表结点类型
    {
        ElemType data;
        struct DNode *prior;    //指向前驱结点
        struct DNode *next;        //指向后继结点
    } DLinkNode;
    void CreateListF(DLinkNode *&L,ElemType a[],int n)
    //头插法建立循环双链表
    {
        DLinkNode *s;int i;
        L=(DLinkNode *)malloc(sizeof(DLinkNode));      //创建头结点
        L->next=NULL;
        for (i=0;i<n;i++)
        {    
            s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点
            s->data=a[i];
            s->next=L->next;            //将结点s插在原开始结点之前,头结点之后
            if (L->next!=NULL) L->next->prior=s;
            L->next=s;s->prior=L;
        }
        s=L->next;    
        while (s->next!=NULL)            //查找尾结点,由s指向它
            s=s->next;
        s->next=L;                        //尾结点next域指向头结点
        L->prior=s;                        //头结点的prior域指向尾结点
    
    }
    void CreateListR(DLinkNode *&L,ElemType a[],int n)
    //尾插法建立循环双链表
    {
        DLinkNode *s,*r;int i;
        L=(DLinkNode *)malloc(sizeof(DLinkNode));  //创建头结点
        L->next=NULL;
        r=L;                    //r始终指向尾结点,开始时指向头结点
        for (i=0;i<n;i++)
        {    
            s=(DLinkNode *)malloc(sizeof(DLinkNode));//创建新结点
            s->data=a[i];
            r->next=s;s->prior=r;    //将结点s插入结点r之后
            r=s;
        }
        r->next=L;                //尾结点next域指向头结点
        L->prior=r;                //头结点的prior域指向尾结点
    }
    void InitList(DLinkNode *&L)
    {
        L=(DLinkNode *)malloc(sizeof(DLinkNode));      //创建头结点
        L->prior=L->next=L;
    }
    void DestroyList(DLinkNode *&L)
    {
        DLinkNode *p=L,*q=p->next;
        while (q!=L)
        {
            free(p);
            p=q;
            q=p->next;
        }
        free(p);
    }
    bool ListEmpty(DLinkNode *L)
    {
        return(L->next==L);
    }
    int ListLength(DLinkNode *L)
    {
        DLinkNode *p=L;
        int i=0;
        while (p->next!=L)
        {
            i++;
            p=p->next;
        }
        return(i);
    }
    void DispList(DLinkNode *L)
    {
        DLinkNode *p=L->next;
        while (p!=L)
        {
            printf("%d ",p->data);
            p=p->next;
        }
        printf("
    ");
    }
    bool GetElem(DLinkNode *L,int i,ElemType &e)
    {
        int j=0;
        DLinkNode *p;
        if (L->next!=L)        //双链表不为空表时
        {
            if (i==1)
            {
                e=L->next->data;
                return true;
            }
            else            //i不为1时
            {
                p=L->next;
                while (j<i-1 && p!=L)
                {
                    j++;
                    p=p->next;
                }
                if (p==L)
                    return false;
                else
                {
                    e=p->data;
                    return true;
                }
            }
        }
        else                //双链表为空表时
            return 0;
    }
    int LocateElem(DLinkNode *L,ElemType e)
    {
        int n=1;
        DLinkNode *p=L->next;
        while (p!=NULL && p->data!=e)
        {
            n++;
            p=p->next;
        }
        if (p==NULL)
            return(0);
        else
            return(n);
    }
    bool ListInsert(DLinkNode *&L,int i,ElemType e)
    {
        int j=0;
        DLinkNode *p=L,*s;
        if (p->next==L)                    //原双链表为空表时
        {    
            s=(DLinkNode *)malloc(sizeof(DLinkNode));    //创建新结点s
            s->data=e;
            p->next=s;s->next=p;
            p->prior=s;s->prior=p;
            return true;
        }
        else if (i==1)                    //原双链表不为空表但i=1时
        {
            s=(DLinkNode *)malloc(sizeof(DLinkNode));    //创建新结点s
            s->data=e;
            s->next=p->next;p->next=s;    //将结点s插入到结点p之后
            s->next->prior=s;s->prior=p;
            return true;
        }
        else
        {    
            p=L->next;
            while (j<i-2 && p!=L)
            {    j++;
                p=p->next;
            }
            if (p==L)                //未找到第i-1个结点
                return false;
            else                    //找到第i-1个结点*p
            {
                s=(DLinkNode *)malloc(sizeof(DLinkNode));    //创建新结点s
                s->data=e;    
                s->next=p->next;    //将结点s插入到结点p之后
                if (p->next!=NULL) p->next->prior=s;
                s->prior=p;
                p->next=s;
                return true;
            }
        }
    }
    bool ListDelete(DLinkNode *&L,int i,ElemType &e)
    {
        int j=0;
        DLinkNode *p=L,*q;
        if (p->next!=L)                    //原双链表不为空表时
        {    
            if (i==1)                    //i==1时
            {    
                q=L->next;                //删除第1个结点
                e=q->data;
                L->next=q->next;
                q->next->prior=L;
                free(q);
                return true;
            }
            else                        //i不为1时
            {    
                p=L->next;
                while (j<i-2 && p!=NULL)        
                {
                    j++;
                    p=p->next;
                }
                if (p==NULL)                //未找到第i-1个结点
                    return false;
                else                        //找到第i-1个结点p
                {
                    q=p->next;                //q指向要删除的结点
                    if (q==NULL) return 0;    //不存在第i个结点
                    e=q->data;
                    p->next=q->next;        //从单链表中删除q结点
                    if (p->next!=NULL) p->next->prior=p;
                    free(q);                //释放q结点
                    return true;
                }
            }
        }
        else return false;                    //原双链表为空表时
    }
    实验5
  • 相关阅读:
    【剑指Offer】34、第一个只出现一次的字符
    【剑指Offer】33、丑数
    【剑指Offer】32、把数组排成最小的数
    linux精彩收集
    shell-总结【摘录】
    linux -特殊符号
    linux --mount
    linux--lsof
    linux--find
    Linux之rsync数据同步服务
  • 原文地址:https://www.cnblogs.com/yeluozhiqiumax/p/12579805.html
Copyright © 2011-2022 走看看