zoukankan      html  css  js  c++  java
  • 3.线性表-cursor

    fatal.h

    #include <stdio.h>
    #include <stdlib.h>
    
    #define Error(Str)        FatalError(Str)
    #define FatalError(Str)   fprintf(stderr, "%s
    ", Str), exit(1)
    

    cursor.h

    typedef int ElementType;
    #define SpaceSize 100
    
    #ifndef _Cursor_H
    #define _Cursor_H
    
    typedef int PtrToNode;
    typedef PtrToNode List;
    typedef PtrToNode Position;
    
    void InitializeCursorSpace(void);
    
    List MakeEmpty(List L);
    int IsEmpty(const List L);
    int IsLast(const Position P, const List L);
    Position Find(ElementType X, const List L);
    void Delete(ElementType X, List L);
    Position FindPrevious(ElementType X, const List L);
    void Insert(ElementType X, List L, Position P);
    void DeleteList(List L);
    Position Header(const List L);
    Position First(const List L);
    Position Advance(const Position P);
    ElementType Retrieve(const Position P);
    
    #endif
    

    cursor.c

    #include "cursor.h"
    #include <stdlib.h>
    #include "fatal.h"
    
    /* Place in the interface file */
    struct Node
    {
        ElementType Element;
        Position    Next;
    };
    
    struct Node CursorSpace[SpaceSize];
    
    static Position CursorAlloc(void)
    {
        Position P;
    
        P = CursorSpace[0].Next;
        CursorSpace[0].Next = CursorSpace[P].Next;
    
        return P;
    }
    
    static void CursorFree(Position P)
    {
        CursorSpace[P].Next = CursorSpace[0].Next;
        CursorSpace[0].Next = P;
    }
    
    void InitializeCursorSpace(void)
    {
        int i;
    
        for (i = 0; i < SpaceSize; i++)
            CursorSpace[i].Next = i + 1;
        CursorSpace[SpaceSize - 1].Next = 0;
    }
    
    List MakeEmpty(List L)
    {
        if (L != 0)
            DeleteList(L);
        L = CursorAlloc();
        if (L == 0)
            FatalError("Out of memory!");
        CursorSpace[L].Next = 0;
        return L;
    }
    
    /* Return true if L is empty */
    int IsEmpty(const List L)
    {
        return CursorSpace[L].Next == 0;
    }
    
    /* Return true if P is the last position in list L */
    /* Parameter L is unused in this implementation */
    int IsLast(const Position P, const List L)
    {
        return CursorSpace[P].Next == 0;
    }
    
    /* Return Position of X in L; 0 if not found */
    /* Uses a header node */
    Position Find(ElementType X, const List L)
    {
        Position P;
    
        P = CursorSpace[L].Next;
        while (P && CursorSpace[P].Element != X)
            P = CursorSpace[P].Next;
    
        return P;
    }
    
    /* Delete from a list */
    /* Assume that the position is legal */
    /* Assume use of a header node */
    void Delete(ElementType X, List L)
    {
        Position P, TmpCell;
    
        P = FindPrevious(X, L);
    
        if (!IsLast(P, L))  /* Assumption of header use */
        {                      /* X is found; delete it */
            TmpCell = CursorSpace[P].Next;
            CursorSpace[P].Next = CursorSpace[TmpCell].Next;
            CursorFree(TmpCell);
        }
    }
    
    /* If X is not found, then Next field of returned value is 0 */
    /* Assumes a header */
    Position FindPrevious(ElementType X, const List L)
    {
        Position P;
    
        P = L;
        while (CursorSpace[P].Next &&
            CursorSpace[CursorSpace[P].Next].Element != X)
            P = CursorSpace[P].Next;
    
        return P;
    }
    
    /* Insert (after legal position P) */
    /* Header implementation assumed */
    /* Parameter L is unused in this implementation */
    void Insert(ElementType X, List L, Position P)
    {
        Position TmpCell;
    
        TmpCell = CursorAlloc();
        if (TmpCell == 0)
            FatalError("Out of space!!!");
    
        CursorSpace[TmpCell].Element = X;
        CursorSpace[TmpCell].Next = CursorSpace[P].Next;
        CursorSpace[P].Next = TmpCell;
    }
    
    
    /* Correct DeleteList algorithm */
    void DeleteList(List L)
    {
        Position P, Tmp;
    
        P = CursorSpace[L].Next;  /* Header assumed */
        CursorSpace[L].Next = 0;
        while (P != 0)
        {
            Tmp = CursorSpace[P].Next;
            CursorFree(P);
            P = Tmp;
        }
    }
    
    Position Header(const List L)
    {
        return L;
    }
    
    Position First(const List L)
    {
        return CursorSpace[L].Next;
    }
    
    Position Advance(const Position P)
    {
        return CursorSpace[P].Next;
    }
    
    ElementType Retrieve(const Position P)
    {
        return CursorSpace[P].Element;
    }
    

    testcurs.c

    #include <stdio.h>
    #include "cursor.h"
    
    void PrintList(const List L)
    {
        Position P = Header(L);
    
        if (IsEmpty(L))
            printf("Empty list
    ");
        else
        {
            do
            {
                P = Advance(P);
                printf("%d ", Retrieve(P));
            } while (!IsLast(P, L));
            printf("
    ");
        }
    }
    
    int main()
    {
        List L;
        Position P;
        int i;
    
        InitializeCursorSpace();
        L = MakeEmpty(0);
        P = Header(L);
        PrintList(L);
    
        for (i = 0; i < 10; i++)
        {
            Insert(i, L, P);
            PrintList(L);
            P = Advance(P);
        }
        for (i = 0; i < 10; i += 2)
            Delete(i, L);
    
        for (i = 10; i < 15; i++)
        {
            Insert(i, L, P);
            PrintList(L);
            P = Advance(P);
        }
        
        for (i = 0; i < 10; i++)
            if (Find(i, L) == 0)
                printf("Element %d Find fails
    ", i);
    
        printf("Finished deletions
    ");
    
        PrintList(L);
    
        DeleteList(L);
    
        return 0;
    }
    

    函数调用关系图(Call graph)

  • 相关阅读:
    testDecoration
    python装饰器详解
    开闭原则, 对扩展开放、对修改关闭
    使用元类 编写ORM
    Python 实现累加计数的几种方法
    python 查找目录下 文件名中含有某字符串的文件
    android应用程序的混淆打包规范
    自定义Tabs
    android-Service
    Loader异步装载器
  • 原文地址:https://www.cnblogs.com/typewriter/p/6209731.html
Copyright © 2011-2022 走看看