• 单链表


    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #define TRUE 1
    #define FALSE 0
    #define OK 1
    #define ERROR 0
    #define INFEASIBLE -1
    #define OVERFLOW   -2
    
    typedef int Status;
    typedef int ElemType;
    
    typedef struct LNode
    {
        ElemType data;
        struct LNode *next;
    }LNode, *LinkList;
    
    void InitList(LinkList &L)
    {
        L = (LinkList)malloc(sizeof(LNode));
        if(!L)
            exit(OVERFLOW);
        L->next = NULL;
    }
    
    void DestroyList(LinkList &L)
    {
        LinkList q;
        while(L)
        {
            q = L->next;
            free(L);
            L = q;
        }
    }
    
    void ClearList(LinkList L)
    {
        LinkList p,q;
        p = L->next;
        while(p)
        {
            q = p->next;
            free(p);
            p = q;
        }
        L->next = NULL;
    }
    
    bool ListEmpty(LinkList L)
    {
        if(L->next)
            return false;
        else
            return true;
    }
    
    int ListLength(LinkList L)
    {
        int i = 0;
        LinkList p = L->next;
        while(p)
        {
            i++;
            p = p->next;
        }
        return i;
    }
    
    Status GetElem(LinkList L,int i,ElemType &e)
    {
        int j = 1;
        LinkList p = L->next;
        while(p && j < i)
        {
            p = p->next;
            j++;
        }
        if(!p || j > i)
            return ERROR;
        e = p->data;
        return OK;
    }
    
    int LocateElem(LinkList L,ElemType e)
    {
        int i = 0;
        LinkList p = L->next;
        while(p)
        {
            i++;
            if(p->data == e)
                return i;
            p = p->next;
        }
        return 0;
    }
    
    Status PriorElem(LinkList L, ElemType cur_e, ElemType &pre_e)
    {
        LinkList q, p = L->next;
        while(p->next)
        {
            q = p->next;
            if(q->data == cur_e)
            {
                pre_e = p->data;
                return OK;
            }
            p = q;
        }
        return INFEASIBLE;
    }
    
    Status NextElem(LinkList L,ElemType cur_e,ElemType &next_e)
    {
        LinkList p = L->next;
        while(p->next)
        {
            if(p->data == cur_e)
            {
                next_e = p->next->data;
                return OK;
            }
            p = p->next;
        }
        return INFEASIBLE;
    }
    
    Status ListInsert(LinkList L,int i,ElemType e)
    {
        int j = 0;
        LinkList p = L,s;
        while(p && j < i - 1)
        {
            p = p->next;
            j++;
        }
        if(!p || j > i - 1)
            return ERROR;
        s = (LinkList)malloc(sizeof(LNode));
        s->data = e;
        s->next = p->next;
        p->next = s;
        return OK;
    }
    
    Status ListDelete(LinkList L,int i,ElemType &e)
    {
        int j = 0;
        LinkList p = L,q;
        while(p->next && j < i - 1)
        {
            p = p->next;
            j++;
        }
        if(!p->next || j > i - 1)
            return ERROR;
        q = p->next;
        p->next = q->next;
        e = q->data;
        free(q);
        return OK;
    }
    
    void ListTraverse(LinkList L)
    {
        LinkList p = L->next;
        while(p)
        {
            cout<<p->data<<" ";
            p = p->next;
        }
        cout<<endl;
    }
    
    int main()
    {
        LinkList L;
        InitList(L);
        cout<<ListLength(L)<<endl;
        for(int i = 1; i <= 10; i++)
            ListInsert(L,i,i);
        ListTraverse(L);
        return 0;
    }
  • 相关阅读:
    Java 字符串总结
    782B The Meeting Place Cannot Be Changed(二分)
    初学hash
    Codeforces Round #395 C. Timofey and a tree
    Java集合之ArrayList
    CA Loves GCD (BC#78 1002) (hdu 5656)
    hdu 5661 Claris and XOR
    hdu 5945 Fxx and game
    pay包注释(二)
    编程风格
  • 原文地址:https://www.cnblogs.com/chenyang920/p/5002495.html
Copyright © 2020-2023  润新知