The Double Linknode for Linear table | Data

简介: The some code in Data book (5th Edition) from the 54 page to 55 page

The some code in Data book (5th Edition) from the 54 page to 55 page

typedef int ElemType;
typedef struct DNode {
    ElemType data; //Data fields
    struct DNode *prior; //Point to the precursor node
    struct DNode *next; //Point to the successor node
}DLinkNode;//Declare the circular DLinknode type

//Create DLinknode using Head-inserting
static void CreateListF(DLinkNode *&L, ElemType a[], int n) { //Reference to pointer
    DLinkNode *s;
    L = (DLinkNode *)malloc(sizeof(DLinkNode)); //Create Head-inserting
    L->prior = L->next = NULL;
    for(int i = 0; i < n; i++) {
        s = (DLinkNode *)malloc(sizeof(DLinkNode)); //Create new node
        s->data = a[i];
        s->next = L->next; //Insert nodes before the original start node, after the head node
        if(L->next != NULL)
            L->next->prior = s;
        L->next = s;
        s->prior = L;
    }
    s = L->next;
    while(s->next != NULL) //Find the end node, which is pointed at by s
        s = s->next;
    s->next = L;
    L->prior = s; //Head-inserting point next domain points to head node
}

//Create DLinknode using Tail-insertion
static void CreateListR(DLinkNode *&L, ElemType a[], int n) {
    DLinkNode *s, *r;
    L = (DLinkNode *)malloc(sizeof(DLinkNode)); //Create Head-inserting
    r = L; //r always point to Tail-insertion
    for(int i = 0; i < n; i++) {
        //Create new node
        s = (DLinkNode *)malloc(sizeof(DLinkNode));
        s->data = a[i];
        r->next = s; //Insert new nodes s after node r
        s->prior = r;
        r = s;
    }
    r->next = NULL; //Tail knot point next domain points to head node
}

//Initialization Linknode
static void InitList(DLinkNode *&L) { //Reference pointer
    L = (DLinkNode *)malloc(sizeof(DLinkNode)); //Create Head-inserting
    L->prior = L->next = L;
}

//Destroyed Linknode
static void DestroyList(DLinkNode *&L) { //Reference pointer
    DLinkNode *pre = L, *p = pre->next;
    while(p != L) {
        free(pre);
        pre = p;
        p = pre->next;
    }
    free(pre);//Release pre
}

//Determine if a linear table is empty
static bool list_empty(DLinkNode *L) {
    return (L->next == L);
}

//Calculation length of the Linknode
static int list_length(DLinkNode *L) {
    int i = 0;
    DLinkNode *p = L;
    while(p->next != L) {
        i++;
        p = p->next; //p point to Tail-inserting
    }
    return i;
}

//Output Linknode
static void display_list(DLinkNode *L)
{
    DLinkNode *p = L->next;
    while(p != L) {
        printf("%c ", p->data);
        p = p->next;
    }
    printf("\n");
}


//Find a data element value
static bool get_elem(DLinkNode *L, int i, ElemType &e)
{
    int j = 1;
    DLinkNode *p = L->next; //p point to first node
    if(i <= 0 || L->next == L) //When i wrong or L is null return false
        return false;
    if(i == 1) {
        e = L->next->data;//Get data
        return true;
    } else {
        while(j <= i - 1 && p != L) { //Find node p for i 
            j++;
            p = p->next;
        }
        if(p == L)
            return false;
        else {
            e = p->data; //Get data
            return true;
        }
    }
}

//Find by element value
static int locate_elem(DLinkNode *L, ElemType e)
{
    int i = 1;
    DLinkNode *p = L->next;
    while(p != NULL && p->data != e) {
        i++;
        p = p->next;
    }
    if(p == NULL) //When node e not exist, return 0
        return 0;
    else //When exist node e of value is 1, return logic number i
        return i;
}
如有侵权,请联系作者删除
目录
相关文章
|
6月前
GEE错误——Layer error: Image.connectedPixelCount: Segment size calculation on floating point bands is n
GEE错误——Layer error: Image.connectedPixelCount: Segment size calculation on floating point bands is n
77 0
HalconDotNet.HTupleAccessException:“‘Cannot convert to double array‘ when accessing ‘HalconDotNet.HT
HalconDotNet.HTupleAccessException:“‘Cannot convert to double array‘ when accessing ‘HalconDotNet.HT
成功解决but is 0 and 2 (computed from start 0 and end 9223372 over shape with rank 2 and stride-1)
成功解决but is 0 and 2 (computed from start 0 and end 9223372 over shape with rank 2 and stride-1)
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
686 0
|
机器学习/深度学习 PyTorch 算法框架/工具
解决Pytorch中RuntimeError: expected scalar type Double but found Float
解决Pytorch中RuntimeError: expected scalar type Double but found Float
2701 0
The Sqlist for Linear table | Data
The Code in Data book (5th Edition) from the 35 page to 37 page
99 0
The Linknode for Linear table | Data
The Code in Data book (5th Edition) from the 49 page to 52 page
81 0
Julia:报错 no method matching increment_deriv!(::Float64, ::Float64)
描述是 `no method matching increment_deriv!(::Float64, ::Float64)` ,找不到该方法。
110 0
|
索引 Python
成功解决ValueError: column index (256) not an int in range(256)
成功解决ValueError: column index (256) not an int in range(256)
成功解决ValueError: column index (256) not an int in range(256)
|
数据库
Data truncation: Out of range value for column ‘estimate_score‘
Data truncation: Out of range value for column ‘estimate_score‘