美文网首页
线性表链式存储

线性表链式存储

作者: northw1nd | 来源:发表于2018-11-13 12:12 被阅读0次

    参考书籍:《大话数据结构》
    环境:VS2017

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    
    #define OK 1
    #define ERROR 0
    #define TRUE 1
    #define FALSE 0
    
    #define MAXSIZE 20
    
    typedef int Status;
    typedef int Elemtype;
    
    Status visit(Elemtype c)
    {
        printf("%d ", c);
        return 0;
    }
    
    typedef struct Node
    {
        Elemtype data;
        struct Node *Next;
    }Node;
    typedef struct Node *LinkList;
    
    Status InitList(LinkList *L)
    {
        *L = (LinkList)malloc(sizeof(Node));
        if (!(*L))
        {
            return ERROR;
        }
        (*L)->Next = NULL;
        return OK;
    }
    
    Status ListEmpty(LinkList L)
    {
        if (L->Next)
            return TRUE;
        else
            return FALSE;
    }
    
    Status ClearList(LinkList *L)
    {
        LinkList p, q;
        p = (*L)->Next;
        while (p)
        {
            q = p->Next;
            free(p);
            p = q;
        }
        (*L)->Next = NULL;
        return OK;
    }
    
    int ListLength(LinkList L)
    {
        int i = 0;
        LinkList p = L->Next;
        while (p)
        {
            p = p->Next;
            i++;
        }
        return i;
    }
    
    Status GetElem(LinkList L, int i, Elemtype *e)
    {
        int j;
        LinkList p;
        p = L->Next;
        j = 1;
        while (p && j<i)
        {
            p = p->Next;
            ++j;
        }
        if (j > i || !p)
        {
            return ERROR;
        }
        *e = p->data;
        return OK;
    }
    
    Status 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 ListInsert(LinkList *L, int i, Elemtype e)
    {
        int j;
        LinkList p, s;
        p = *L;
        j = 1;
        while (p && j<i)
        {
            p = p->Next;
            ++j;
        }
        if (!p || j > i)
        {
            return ERROR;
        }
        s = (LinkList)malloc(sizeof(Node));
        s->data = e;
        s->Next = p->Next;
        p->Next = s;
        return OK;
    }
    
    Status ListDelete(LinkList *L, int i, Elemtype *e)
    {
        int j;
        LinkList p, q;
        p = *L;
        j = 1;
        while (p->Next && j < i)
        {
            p = p->Next;
            ++j;
        }
        if (!(p->Next) || j > i)
        {
            return ERROR;
        }
        q = p->Next;
        p->Next = q->Next;
        *e = q->data;
        free(q);
        return OK;
    }
    
    Status ListTraverse(LinkList L)
    {
        LinkList p = L->Next;
        while (p)
        {
            visit(p->data);
            p = p->Next;
        }
        printf("\n");
        return OK;
    }
    
    void CreateListHead(LinkList *L, int n)//头插法
    {
        LinkList p;
        int i;
        srand(time(0));
        *L = (LinkList)malloc(sizeof(Node));
        (*L)->Next = NULL;
        for (i = 0; i < n; i++)
        {
            p = (LinkList)malloc(sizeof(Node));
            p->data = rand() % 100 + 1;
            p->Next = (*L)->Next;
            (*L)->Next = p;
        }
    }
    
    void CreateListTail(LinkList *L, int n)
    {
        LinkList p, r;
        int i;
        srand(time(0));
        *L = (LinkList)malloc(sizeof(Node));
        r = *L;
        for (i = 0; i < n; i++)
        {
            p = (Node *)malloc(sizeof(Node));
            p->data = rand() % 100 + 1;
            r->Next = p;
            r = p;
        }
        r->Next = NULL;
    }
    
    int main()
    {
        LinkList L;
        Elemtype e;
        Status i;
        int j, k;
        i = InitList(&L);
        printf("初始化L后:ListLength(L)=%d\n", ListLength(L));
        for (j = 1; j <= 5; j++)
            i = ListInsert(&L, 1, j);
        printf("在L的表头依次插入1~5后:L.data=");
        ListTraverse(L);
    
        printf("ListLength(L)=%d \n", ListLength(L));
        i = ListEmpty(L);
        printf("L是否空:i=%d(1:是 0:否)\n", i);
    
        i = ClearList(&L);
        printf("清空L后:ListLength(L)=%d\n", ListLength(L));
        i = ListEmpty(L);
        printf("L是否空:i=%d(1:是 0:否)\n", i);
    
        for (j = 1; j <= 10; j++)
            ListInsert(&L, j, j);
        printf("在L的表尾依次插入1~10后:L.data=");
        ListTraverse(L);
    
        printf("ListLength(L)=%d \n", ListLength(L));
    
        ListInsert(&L, 1, 0);
        printf("在L的表头插入0后:L.data=");
        ListTraverse(L);
        printf("ListLength(L)=%d \n", ListLength(L));
    
        GetElem(L, 5, &e);
        printf("第5个元素的值为:%d\n", e);
        for (j = 3; j <= 4; j++)
        {
            k = LocateElem(L, j);
            if (k)
                printf("第%d个元素的值为%d\n", k, j);
            else
                printf("没有值为%d的元素\n", j);
        }
    
    
        k = ListLength(L); /* k为表长 */
        for (j = k + 1; j >= k; j--)
        {
            i = ListDelete(&L, j, &e); /* 删除第j个数据 */
            if (i == ERROR)
                printf("删除第%d个数据失败\n", j);
            else
                printf("删除第%d个的元素值为:%d\n", j, e);
        }
        printf("依次输出L的元素:");
        ListTraverse(L);
    
        j = 5;
        ListDelete(&L, j, &e); /* 删除第5个数据 */
        printf("删除第%d个的元素值为:%d\n", j, e);
    
        printf("依次输出L的元素:");
        ListTraverse(L);
    
        i = ClearList(&L);
        printf("\n清空L后:ListLength(L)=%d\n", ListLength(L));
        CreateListHead(&L, 20);
        printf("整体创建L的元素(头插法):");
        ListTraverse(L);
    
        i = ClearList(&L);
        printf("\n删除L后:ListLength(L)=%d\n", ListLength(L));
        CreateListTail(&L, 20);
        printf("整体创建L的元素(尾插法):");
        ListTraverse(L);
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:线性表链式存储

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