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;
}
如有侵权,请联系作者删除
目录
相关文章
|
4月前
|
索引 Python
row[i] = col[j] = TrueIndexError: list assignment index out of range
row[i] = col[j] = TrueIndexError: list assignment index out of range
HalconDotNet.HTupleAccessException:“‘Cannot convert to double array‘ when accessing ‘HalconDotNet.HT
HalconDotNet.HTupleAccessException:“‘Cannot convert to double array‘ when accessing ‘HalconDotNet.HT
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解决方法
644 0
成功解决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)
|
关系型数据库
PG:INT4 VS. FLOAT4 VS. NUMERIC
PG:INT4 VS. FLOAT4 VS. NUMERIC
129 0
The Sqlist for Linear table | Data
The Code in Data book (5th Edition) from the 35 page to 37 page
80 0
The Linknode for Linear table | Data
The Code in Data book (5th Edition) from the 49 page to 52 page
74 0
|
Python
You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.conca
You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.conca
731 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)