美文网首页
单链表实现链式线性表(C语言)

单链表实现链式线性表(C语言)

作者: Recalcitrant | 来源:发表于2019-05-07 15:32 被阅读0次

    单链表实现链式线性表

    /*
    单链表实现链式线性表
    */
    #include <stdio.h>
    #include <stdlib.h>
    #define ElemType int        //线性表数据类型
    
    #define OK 0                //操作成功执行
    #define ERROR -1            //操作失败
    #define OVERFLOW -2         //溢出
    typedef int Status;
    
    typedef struct LNode {
        ElemType data;          //数据域
        struct LNode *next;     //指针域
    }LNode, *LinkList;
    
    LinkList InitList_L();                              //1.创建
    Status DestroyList_L(LinkList L);                   //2.销毁
    Status ClearList_L(LinkList L);                     //3.清空
    Status ListEmpty_L(LinkList L);                     //4.判空
    int ListLength_L(LinkList L);                       //5.求长
    ElemType GetElem_L(LinkList L, int i);              //6.访问元素
    int LocateElem_L(LinkList L, ElemType e);           //7.求元素位序
    ElemType PriorElem_L(LinkList L, ElemType *cur_e);  //8.求前驱
    ElemType NextElem_L(LinkList L, ElemType *cur_e);   //9.求后继
    Status ListInsert_L(LinkList L, int i, ElemType e); //10.插入元素
    ElemType ListDelete_L(LinkList L, int i);           //11.删除元素
    Status ListTraverse_L(LinkList L);                  //12.遍历
    
    
    
    LinkList InitList_L()       //1.创建
    {
        LinkList L;
        L = (LinkList)malloc(sizeof(LNode));
        L->data = 0;        //头结点数据域存储表长
        L->next = NULL;
        return L;
    }
    Status DestroyList_L(LinkList L)        //2.销毁
    {
        LNode *p, *q;
        p = L;
        while (!p)
        {
            q = p->next;
            free(p);
            p = q;
        }
        L = NULL;
        return OK;
    }
    Status ClearList_L(LinkList L)      //3.清空
    {
        LNode *p, *q;
        p = L->next;
        while (!p)
        {
            q = p->next;
            free(p);
            p = q;
        }
        L->data = 0;
        L->next = NULL;
        return OK;
    }
    Status ListEmpty_L(LinkList L)      //4.判空
    {
        if (L->data == 0) return 1;
        else return 0;
    }
    int ListLength_L(LinkList L)        //5.求长
    {
        return L->data;
        /*
        LNode *p;
        p = L->next;
        int j = 0;
        while(p)
        {
            p = p->next;
            j++;
        }
        return j;
        */
    }
    ElemType GetElem_L(LinkList L, int i)      //6.访问元素
    {
        LNode *p;
        p = L;
        int j = 0;
        while (p && j < i)
        {
            p = p->next;
            j++;
            if (p && j == i)return p->data;
        }
        if (!p || j > i)return ERROR;
    }
    int LocateElem_L(LinkList L, ElemType e)            //7.求元素位序
    {
        LNode *p;
        p = L;
        int j = 0;
        p = p->next;
        while (p)
        {
            if (p->data == e)return j;
            p = p->next;
            j++;
        }
        if (!p || j > L->data)return ERROR;
    }
    Status ListInsert_L(LinkList L, int i, ElemType e)  //10.插入元素
    {
        LNode *p, *s;
        p = L;
        int j = 0;
        while (p && j < i - 1)
        {
            p = p->next;
            j++;
        }
        if (!p || j > i - 1)return ERROR;
        s = (LinkList)malloc(sizeof(LNode));
        if (!s)return OVERFLOW;
        s->data = e;
        s->next = p->next;
        p->next = s;
        L->data++;
        return OK;
    }
    ElemType ListDelete_L(LinkList L, int i)            //11.删除元素
    {
        ElemType e;
        LNode *p, *q;
        p = L;
        int j = 0;
        while (p && j < i - 1)
        {
            p = p->next;
            j++;
        }
        if (!p || j > i - 1)return ERROR;
        q = p->next;
        p->next = q->next;
        e = q->data;
        free(q);
        L->data--;
        return e;
    }
    Status ListTraverse_L(LinkList L)           //12.遍历
    {
        LNode *p;
        p = L;
        p = p->next;
        while (p)
        {
            printf("%d ", p->data);
            p = p->next;
        }
        return OK;
    }
    
    
    
    int main(int argc, char *argv[])
    {
        LinkList L;
        L = InitList_L();
        ListInsert_L(L, 1, 14);
        ListInsert_L(L, 1, 15);
        ListInsert_L(L, 2, 1);
        ListInsert_L(L, 3, 2);
        printf("1:%d\n",GetElem_L(L,1));
        printf("值为14的元素位序为:%d\n", LocateElem_L(L, 14));
        ListTraverse_L(L);
        ListDelete_L(L, 2);
        printf("\n删除后:");
        ListTraverse_L(L);
    
    
        DestroyList_L(L);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:单链表实现链式线性表(C语言)

          本文链接:https://www.haomeiwen.com/subject/iucloqtx.html